Java features - polymorphism and abstraction

Posted by chasiv on Thu, 07 Oct 2021 07:26:56 +0200

Polymorphism of characteristics

1, Concept

The image point is that the same behavior is performed by different people or things

2, Realization conditions

  •   It must be under the inheritance system
  • Derived classes must override methods in the base class
  • The overridden method must be called through a reference to the base class

3, Code implementation (taking ticket purchase of different groups as an example)  

Suppose three common groups

  • Full adult ticket
  • Half ticket for students
  • Soldiers are free

1. Prepare ticket buying base class

public class BuyTicket {
    public String name;

    public BuyTicket(String name) {
        this.name = name;
    }

    public void Ticket(){
        System.out.println("Ticket purchase");
    }
}

2. Write derived classes for adults, students and soldiers to buy tickets in turn, and rewrite the method of buying tickets

//Adult ticket derivatives

public class Adult extends BuyTicket{

    public Adult(String name) {
        super(name);
    }

    @Override
    public void Ticket(){
        System.out.println("Adults buy full tickets!");
    }
}

//Student ticket derivatives

public class Student extends BuyTicket{

    public Student(String name) {
        super(name);
    }

    @Override
    public void Ticket(){
        System.out.println("Students buy half price tickets!");
    }

}

//Derived class of military ticket buying

public class Soldier extends BuyTicket{

    public Soldier(String name) {
        super(name);
    }

    @Override
    public void Ticket(){
        System.out.println("Soldiers are free!");
    }
}

  3. Prepare the test method for buying tickets

public class TestBuyTicket {
    public static void Ticket(BuyTicket a){
        a.Ticket();
    }

    public static void main(String[] args) {
        Adult A = new Adult("adult");
        Student B = new Student("student");
        Soldier C = new Soldier("soldier");

        A.Ticket();
        B.Ticket();
        C.Ticket();

    }
}

4. Final operation and results

Summary:  

  Create an object through the derived class new, and call the corresponding method through this object

4, The main difference between rewriting and overloading

Main features of rewriting

  • The name and parameter list should be consistent before and after method rewriting
  • The return type can be different from that of the overridden method, but it must be a derived class of the return value of the base class (related to the jdk version)
  • The access permission cannot be lower than the access permission of the overridden method in the base class
  • Member methods of a base class can only be overridden by its subclasses
  • Methods modified by final and static cannot be overridden
  • Constructor cannot be overridden
  • In the same package, you can override methods except those modified by private and final
  • In different packages, non final methods can only be modified by public and protected again
  • override can be used for more standardized rewriting methods  

Override and reload  

  5, Upward transformation

Upward Transformation: in fact, it is to create a subclass object and use it as a base class object.
Syntax format: base class type object name = new subclass type ()

BuyTicket buyTicket = new Student("Student 1");

Subclass object is a base class object, that is, a subclass object can be applied as a base class object. Therefore, the upward transformation is safe because it is a transformation from a small range to a large range (it can be said that students are a group, but it can not be said that the group is students, but also includes other groups, such as teachers, soldiers, doctors, etc.)

advantage:

  • Reduce some duplicate code

  • When instantiating objects, you can instantiate different objects according to different needs

characteristic:

  • Cannot call a method unique to a subclass

6, Downward transformation

After upward transformation, a subclass object is used as a base class method. You can no longer call the subclass method, but sometimes you may need to call the subclass specific method. At this time, you can restore the parent class reference to a subclass object, that is, downward transformation

Student s1 = new Student("Student 2");

BuyTicket a1 = (BuyTicket) s1;

Disadvantages: if the range from large to small is incorrect, exceptions will be thrown, and the downward transformation is also extremely unsafe

In order to improve the security of downward transformation in Java, instanceof is introduced. If the expression is true, it can be safely transformed

Abstraction of features

1, Concept

If a class does not contain enough information to describe a specific object, such a class is an abstract class

2, Grammar

In Java, if a class is modified by abstract, the class is called abstract class, and the method modified by abstract in abstract class is called abstract method. The abstract method does not need to give a specific implementation  

//The class modified by abstract is an abstract class
public abstract class Animal {

    //The method modified by abstract is from the abstract method without giving the method body
    abstract public void eat();
    abstract void sleep();

    //Abstract classes can also give common methods and properties
    
    public void method(){
        System.out.println("I am an ordinary method of an abstract class!");
    }
    
    public String name;
}

3, Characteristics of abstract classes

1. Cannot instantiate object

2. Abstract methods cannot be private (the default access qualifier in abstract classes is public)

  3. Abstract methods cannot be modified by final and static (abstract methods need to be overridden by subclasses)

 

  4. The abstract class must be inherited, and after inheritance, the subclass must override the abstract method in the parent class, otherwise the subclass is also an abstract class and must         Use abstract modifier

public class Dog extends Animal{

    public void eat(){
        System.out.println("Gnaw a bone!");
    }

    public void sleep(){
        System.out.println("Wang Wang team sleep!");
    }
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.sleep();
    }
}

 

  5. Abstract classes do not necessarily have abstract methods, but those with abstract methods must be abstract classes

4, Role of abstract classes

If you want to instantiate an object in an abstract class, you must do it in a subclass, so let the subclass inherit this abstract class and override all abstract methods in the subclass

Topics: Java JavaSE