Home  >  Article  >  Java  >  Object-oriented (interface, polymorphism)

Object-oriented (interface, polymorphism)

巴扎黑
巴扎黑Original
2017-06-26 11:09:541018browse

Interface

(1) When the methods in the abstract class are abstract, java provides a new form of expression: interface, which is a collection interface of functions. Unable to create object
(2)Format
Parent interface: public interface Tnter{


}
Subclass: public class interImp implements Itner{

}

Use of interface
1. Interface cannot create objects
2. Define implementation class to implement interface
Implementation keyword implements
3. Override the abstract method
4. Create an implementation class object and call the method


Features of the interface
1. Does not need to be modified by abstract
2 .A class implements an interface, which can be implemented singly or in multiple ways
3. An interface can inherit from an interface, either singly or in multiple ways
4. The functions of the interface and the parent class can be repeated, which means they must have a certain function

Member characteristics of interface
Member variables static constants
Fixed modifier public static final
Whether you write it or not, this modifier remains unchanged

Member method
Fixed modifier public abstract
Whether it is written or not, this modifier remains unchanged

Abstract class and Differences in interfaces
1. Differences in members
Abstract class:
Member variable: It can be a variable or a constant
Constructor method: There is a constructor method for instantiation of subclasses Use
Member method: It can be abstract or non-abstract
Interface:
Member variable: It can only be a constant
Default modifier: public static final

Member method : Can only be abstract
Default modifier: public abstract

Recommendation: Please always give the default modifier manually
2. The difference between classes and interfaces
Classes and classes:
Inheritance relationship, only single inheritance, multi-level inheritance is possible

Classes and interfaces:
Implementation relationship, single implementation or multiple implementations are possible
Class also You can inherit a class and implement multiple interfaces at the same time

Interface and interface:
Inheritance relationship, you can have single inheritance or multiple inheritance
3. The concepts reflected are different

What is defined in the abstract class is the common content in an inheritance system
The interface is a collection of functions, an additional function of a system, and the exposed rules


Everything is used Wherever the parent class/interface refers, its subclass/implementation class object can be passed in

Polymorphism
The same object can be reflected at different times Different states
Example: Water (water, ice, water vapor)
Cat (cat, animal)
Prerequisite:
A: There is inheritance or implementation relationship
B: There is method Rewrite
C: A reference from the parent class points to the subclass object
Characteristics of using members in polymorphism
Fu fz=new Zi();
When polymorphic, all expressions are the parent class Expression form
Only when a method is called, the method rewritten by the subclass is run
1. Member variables
Compile and see on the left. Run and run.
2. Member methods
Compile and run. Look at the right

 1         class Fu{ 2             int num=4; 3             void show(){ 4                 system.out.println("showFu") 5             } 6  7         } 8         class Zi extends Fu{ 9 10             int num=5;11             void show(){12                 system.out.println("showZi");13             }14         }15         class T{16             public static void main(String args[]){17                 Fu f=new Zi();18                 system.out.println(f.num);19                 f.show();20             }21         }

Transformation in polymorphism
1. Upward transformation
Assign the subclass object to the parent class (Interface) reference automatic type promotion
int a=0; double b=a;
Fu fz=new Zi();
2. Downcast
Change the parent class (interface) Reference is cast to a subclass object
double b=10.0;
int a=(int)b;

Fu fz=new Zi();
Zi zi=(Zi)fz ;
Note: Fu fu=new Fu() cannot be downcast and will report a ClassCastException type conversion exception

The benefits and drawbacks of polymorphism
1. Benefits
Improved program maintainability and scalability
2. Disadvantages
Unable to apply subclass-specific content
If you want to use it, you must either downcast or re-create the subclass object
Three forms of polymorphism
1. Concrete class polymorphism parent class variable name=new subclass()
2. Abstract class polymorphism parent abstract class variable name=new subclass()
3. Interface polymorphism Interface variable name = new implementation class ()

instanceof keyword
Format: object name instanceof class name
Return value: true false
Function: Determine whether the specified object is an object created by a given class

Animal a1=new Cat();
Animal a2=new Dog();
method(a1)
public static void method(Animal a){

if(a instanceof Cat){
Downward transformation
Call the cat-specific method
 }
}

The above is the detailed content of Object-oriented (interface, polymorphism). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn