Object-oriented 3 major features: encapsulation, inheritance, polymorphism - polymorphism (reference type conversion, abstract class, interface Interface)

polymorphism

Multiple images of objects (two classes must be inherited when using polymorphism, inheritance is the basis of polymorphism)

1. Reference polymorphism

  The reference of the parent class can point to the object of this class Animal obj1=new Animal();

  The reference of the parent class can point to the object of the subclass Animal obj2=new Dog(); the subclass cannot point to the object of the parent class

 

public class Initial {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Dog dog=new Dog();
        Animal animal=new Animal();
        Animal dog3 = new Dog(); // A reference to a parent class can point to a child class object
         // Dog dog4=new Animal(); *Error! Subclass references cannot point to superclass objects 
    }
}

 

2. Method polymorphism

  When an object of this class is created, the method called is a method of this class

  When creating a subclass object, the method called is the method overridden by the subclass or the inherited method (if not overridden, the inherited method is used)

tips:

If the method is not inherited from the parent class, but is unique to the subclass, the object of the parent class that refers to the subclass cannot call the method.

public  static  void main(String[] args) {
         // TODO Auto-generated method stub 
        Animal obj= new Dog(); // The reference of the parent class can point to the child class object 
        Animal obj3= new Cat();
        obj.eat(); // Dogs are meat 
        eaters obj3.eat(); // Animals have the ability to eat 
    }

reference type conversion

1. Up type conversion (implicit / automatic type conversion), which is a conversion from a small type to a large type - no risk

2. Down type conversion (forced type conversion) is a conversion from a large type to a small type - risky

3. The instanceof operator is used to solve the type conversion risk of reference objects and avoid the security problem of type conversion

Dog dog= new Dog(); // Create a subclass object 
        Animal animal=dog; // The parent class object points to the subclass object, automatic type promotion, upward type conversion 
        Dog dog2=(Dog)animal; // Down type conversion , cast
         // downcast, cast - this is not acceptable
         // Cat cat=(Cat)animal; // 1. Compile-time Cat type 2. Run-time Dog type so it's not OK
         // Avoid the safety problem of type conversion through the instanceof operator 
        if (animal instanceof Cat){ // If the animal object contains Cat 
            Cat cat= (Cat)animal;
        }else{
            System.out.println( "Cannot convert type" );
        }

abstract class

1. Grammar definition

If an abstract class is modified with the abstract keyword, the class is an abstract class

2. Application scenarios

a . In some cases, a parent class knows what methods its subclasses should contain, but cannot know exactly how these subclasses implement these methods

b . Abstract an abstract class from multiple classes with the same characteristics, and use this abstract class as the template of the subclass, thereby avoiding the randomness of subclass design

3. Function

Restrictions dictate that subclasses must implement certain methods, but do not pay attention to details

4. Rules of use

a , abstract defines an abstract class

b . abstract defines abstract methods, only declarations do not need to be implemented

c . A class that contains an abstract class method is an abstract class

d . The abstract class can contain ordinary methods or no abstract methods

e . Abstract classes cannot be created directly, but reference variables can be defined

public  abstract  class Shape {
     double wide=5.0 ;
     double length=6.0 ;
     double radii=5.3 ;
     double peri;
     double acre;
     // define the method for calculating the perimeter 
    public  abstract  double perimeter();
     // define the method for calculating the area 
    public  abstract  double acreage();
}
public class Rectangle extends Shape {

    @Override
    public double perimeter() {
        // TODO Auto-generated method stub
        return (length+wide)*2;
    }
    @Override
    public double acreage() {
        // TODO Auto-generated method stub
        return length*wide;
    }
}
public class Circle extends Shape {

    @Override
    public double perimeter() {
        // TODO Auto-generated method stub
        return 3.1415*(radii*2);
    }
    @Override
    public double acreage() {
        // TODO Auto-generated method stub
        return 3.1415*(radii*radii);
    }
}
public  class Initail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shape rect=new Rectangle();
        System.out.println(rect.perimeter());
        System.out.println(rect.acreage());
        Shape cir=new Circle();
        System.out.println(cir.perimeter());
        System.out.println(cir.acreage());
    }
}

 

interface in java

[ access modifier ] interface interface name [extends parent interface 1, parent interface 2...] {

Definition of 0 or more constants ...

Definition of 0 or more abstract methods ...

 }

The interface is used to be inherited and implemented, the modifier is generally recommended to use public

Note: You cannot use private and protected to decorate interfaces

The concept of interface:

An interface can be understood as a special class consisting of global constants and public abstract methods

A class is a specific implementation body, and an interface defines the specifications that a certain batch of classes need to abide by. The interface does not care about the internal data of these classes, nor the implementation details of the methods in these classes. It only stipulates that these classes must provide some methods.

constant:

The properties in the interface are constants. Even if the public static final modifier is not added when the definition is made, the system will automatically add them.

method:

The methods in the interface can only be abstract methods. Even if the public static final modifier is not added when the definition is made, the system will automatically add it.

 

Using interface 1 :

A class can implement one or more interfaces using the implements keyword. A class in java can only inherit one parent class, which is not flexible enough. It can be supplemented by implementing multiple interfaces.

The syntax for inheriting a parent class to implement an interface is:

[ Access modifier ] class class name extends parent class implements interface 1, interface 2 ... {

  Class body part // If you inherit an abstract class, you need to implement the inherited abstract method; you need to implement the abstract method in the interface

} // If you want to inherit the parent class, you must inherit the parent class before implementing the interface

public  interface IPlayGame {
     public  abstract  void playGame();
}
public class Smartphone extends Telphone implements IPlayGame{
    @Override
    public  void palyGame() {
         // TODO Auto-generated method stub 
        System.out.println("Intelligence has the function of playing games!" );
    }
}
public class Psp implements IPlayGame {

    @Override
    public  void palyGame() {
         // TODO Auto-generated method stub 
        System.out.println("PSP has the function of playing games!" );
    }
}
public  static  void main(String[] args) {
         // TODO Auto-generated method stub 
        IPlayGame ip1= new Smartphone(); // Use the interface class to point to an object that implements the interface method 
        ip1.palyGame();
        IPlayGame ip2=new Psp();
        ip2.palyGame();
    }

Using interface 2 :

Interfaces are often used in conjunction with anonymous inner classes during use

Anonymous inner classes are inner classes without a name, which are mostly used to focus on implementation rather than the name of the implementation class

syntax format

Interface i=new Interface(){

 public void method(){

 System.out.println( "The way the anonymous inner class implements the interface" ); } }

 

A method in an interface cannot have a method body, and the method's access modifier

cannot be private and protected

 

public  static  void main(String[] args) {
         // TODO Auto-generated method stub 
        IPlayGame ip1= new Smartphone(); // Use the interface class to point to an object that implements the interface method 
        ip1.palyGame();
        IPlayGame ip2=new Psp();
        ip2.palyGame();
        // Use an anonymous inner class method to directly new an interface 
        IPlayGame ip3= new IPlayGame(){
            @Override
            public  void palyGame(){
                 // TODO Auto-generated method stub 
                System.out.println("The way the anonymous inner class implements the interface" );
            }
        }; // You need to use one here; end 
        ip3.palyGame();
         // Directly new a reference to an interface, implement it in it, and then call 
        new IPlayGame(){
            @Override
            public  void palyGame(){
                 // TODO Auto-generated method stub 
                System.out.println("The way the anonymous inner class implements the interface" );
            }
        }.palyGame();
    }

 

 

 

*** END

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738035&siteId=291194637