Implementation code of Java abstract class inheritance and polymorphism and adapter

  • 2021-09-20 20:16:19
  • OfStack

Java inheritance

Method overrides are characteristic of Java language polymorphism and must satisfy the following conditions

In a subclass, the method name is exactly the same as the parent method name Method has exactly the same number of parameters and type, and the return type is exactly the same The access level of the method is not lower than the access level of the parent class method with the same name Add an @ override annotation to the method, and if an error is reported, it means that it is not overridden

Method override limit

final-decorated parent class methods cannot be overridden in subclasses static-decorated parent class methods cannot be overridden in subclasses and can only be overridden

super keyword

The super keyword is similar to this, and super modifies the object of the parent class, such as super (); The default parameterless constructor of the parent class is called

Java abstract class

Abstract class characteristics

Abstract classes should usually contain abstract methods, and can also contain non-abstract methods Abstract classes cannot be decorated with the final keyword An abstract class itself cannot be instantiated Abstract classes are used to be inherited

Inheritance of abstract classes

Subclasses must implement all abstract methods of the parent class If the subclass already override implements the parent's abstract method, the indirect subclass may not implement the method.

Adapter

Define 1 adapter, class name + Adapator. For example, MamalAdapator. Inherit an empty implementation of all methods of the parent class. Classes of later instances inherit this adapter and can optionally override methods that implement some of the parent classes without reporting errors. The abstract class provides only one abstract method for subclasses to inherit and override override, Then, especially this function of override can be achieved, such as Dog. eat, Cat. eat. Different classes have different methods, but the method names are all the same, which provides great flexibility for development. When later programmers take over the development, for example, they need to add Tiger. eat, and only need to write another Tiger class to inherit the parent class and then override his eat method.

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,  Customize 1 Individual class inheritance Counter  Class ,
 *  And defined according to understanding in Counter The abstract method defined in the class implements the abstract method of the parent class , Experience the inheritance of ordinary classes 
 *  What an abstract class must do ?  Different subclasses are inheriting Counter What is related to the algorithm when abstracting classes and implementing abstract methods .
 */

public class Test {
    public static void main(String[] args) {
        // Use the type of the parent class to new1 Subclass 
        Counter cou = new Add();
        // Use the type of the parent class to call the addition Method and implement 
        System.out.println("3+9 Yes and yes "+cou.addition(3,9));
    }
}

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter Class 
 *  The abstract method defined implements the abstract method of the parent class , Experience the abstraction of ordinary class inheritance 
 *  What must the class do ?  Different subclasses are inheriting Counter Abstract classes and implement abstract methods 
 *  What is the algorithm related to .
 */

public class Add extends Counter{

    int num1 = 0;
    int num2 = 0;

    // Override the addition in the parent class and implement the addition 
    @Override
    public int addition(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
        return num1+num2;

    }

    public Add(){
        System.out.println(" Call Add Subclass constructor ");
    }
}

-----------------------------------------------------------------


/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.test;

import tool.abstracts.Construct;
import tool.abstracts.Tool;
import tool.abstracts.Traffic;
import tool.imp.Car;
import tool.imp.Hammer;

public class Test {
    public static void main(String[] args) {
        // Instantiating top-level tool class objects , Invoke the functional method in it 
        Tool tool1 = new Traffic() {
            @Override
            public void constructTool() {

            }
        };
        tool1.trafficTool();

        Tool tool2 = new Construct() {
            @Override
            public void trafficTool() {

            }
        };
        tool2.constructTool();

        // Instantiate a special tool class object , Invoke the functional method in it 
        Traffic traffic = new Car();
        traffic.trafficTool();

        Construct construct = new Hammer();
        construct.constructTool();


    }
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.abstracts;

// Definition 1 A tool Abstract class 
public abstract class Tool {
    // Definition 1 A vehicle abstraction method 
    public abstract void trafficTool();
    // Definition 1 Abstract method of building tools 
    public abstract void constructTool();
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.abstracts;

// Definition 1 A vehicle abstract class inherits from a tool class 
public abstract class Traffic extends Tool {

    public void trafficTool(){
        System.out.println(" I am a collection of all means of transportation ");
    };
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.abstracts;

// Definition 1 Building tool abstract classes inherit from tool classes 
public abstract class Construct extends Tool {

    public void constructTool(){
        System.out.println(" I am a collection of all building tools ");
    };
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.adaptor;

import tool.abstracts.Construct;

// Definition 1 A Construct Adapter of 
public class ConstructAdaptor extends Construct {

    // The method of transportation is carried out 1 Rewrite and null implementation 
    @Override
    public void trafficTool() {

    }
    // The construction tool method is carried out 1 Rewrite and null implementation 
    @Override
    public void constructTool() {

    }
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.adaptor;

import tool.abstracts.Traffic;

// Definition 1 A Traffic Adapter of 
public class TrafficAdaptor extends Traffic {

    // The method of transportation is carried out 1 Rewrite and null implementation 
    @Override
    public void trafficTool() {
        System.out.println(" I am a collection of all means of transportation ");
    }
    // The construction tool method is carried out 1 Rewrite and null implementation 
    @Override
    public void constructTool() {

    }
}

/**
 *2  Many things in life can be used by people to act as 1 A tool , These tools all have 1 The common function is "use", 
 *  At the same time, there are many tools suitable for a certain field ( Such as , Means of transportation , Building tools, etc  ), They are also within the scope of tools ,  Also useful 
 *  Way function , At the same time have their own 1 Some domain functions ,  When people use these tools, , Which specific can be selected according to the actual situation 
 *  Tool entity of class .
 *  In accordance with that above description , Combined with the knowledge points in this unit, , Designer abstract methods for class definition purposes , On the basis of this class, it is defined here 
 *  Like at least in the category of tool classes 2 Abstract classes of special tools and inherit the above tool classes  ,  According to the actual use of tools, 
 *  A more specific tool class implementation of the 2 A special tool class 1 Inheritance of ,  Instantiate top-level tool class and special tool class objects respectively , Tone 
 *  Use the functional method ,  What problems should we pay attention to when understanding the inheritance between abstract classes and ordinary classes that realize multi-layer inheritance .
 *
 * 3  Successive task 2 Realization ,  When we use inheritance to abstract parent classes, ,  Our intention is only to use a specific method of an abstract class ,
 *  However, it is limited by the fact that ordinary classes inherit abstract classes , All methods in all parent abstract classes must be implemented , Necessary even if those methods are not used 
 *  Providing an empty implementation of an abstract method in a subclass , How to solve this problem ,  Please work in the task 2 Programming on the basis of .
 */

package tool.imp;

import tool.adaptor.TrafficAdaptor;

// Definition 1 General classes and concrete implementation of vehicle methods 
public class Car extends TrafficAdaptor {
    @Override
    public void trafficTool() {
        System.out.println(" I'm a vehicle car ");
    }
}

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
0

--------------------------------------


/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
1

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
2

/**
 * 4  In life , People will choose to use different tools to complete a certain task according to the actual situation .
 *  If I want to travel , Will use cars or planes , I use a printer when I print files , I want to study 
 * java Will use a laptop ,  How to design 1 Application definitions 1 Human beings and among human beings 
 *  Definition 1 Ways to use tools ,  Enable you to use tools when you really use them ,  What tools do you use to do this 
 *  Tools can appear in the process of using tools , Please follow java Linguistic Inheritance and the Characteristics of Polymorphism 
 *  Complete this programming task .
 */
package adaptor;

import abstracts.Tool;

// Definition 1 Adapters that inherit the tool class and provide 3 Empty Implementation of Methods 
public class ToolAdaptor extends Tool {
    @Override
    public void travel() {

    }

    @Override
    public void print() {

    }

    @Override
    public void study() {

    }
}

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
4

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
5

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
6

/**
 * 1  Use Eclipse  Tool establishment 1 Ordinary Java Console application ,
 *  Customize 1 Individual class inheritance Counter  Class , And defined according to understanding in Counter
 *  The abstract method defined in the class implements the abstract method of the parent class , Experience the general class 
 *  What must I do to inherit an abstract class ?  Different subclasses are inheriting Counter Abstract class union 
 *  What is the algorithm for implementing abstract methods related to .
 */

// Definition 1 Abstract parent class Counter
public abstract class Counter {
    // Definition 1 Abstract method addition
    public abstract int addition(int num1, int num2);

    public Counter(){
        System.out.println(" Call Counter Parent class constructor ");
    }
}
7

/**
 * 4  In life , People will choose to use different tools to complete a certain task according to the actual situation .
 *  If I want to travel , Will use cars or planes , I use a printer when I print files , I want to study 
 * java Will use a laptop ,  How to design 1 Application definitions 1 Human beings and among human beings 
 *  Definition 1 Ways to use tools ,  Enable you to use tools when you really use them ,  What tools do you use to do this 
 *  Tools can appear in the process of using tools , Please follow java Linguistic Inheritance and the Characteristics of Polymorphism 
 *  Complete this programming task .
 */

package imp;

import abstracts.Tool;

// Definition 1 A Person Class 
public class Person {
    // Definition 1 Methods of using tools, receiving tool Abstract the type of the class and call the tool Of an abstract class 3 Abstract method 
    public void useTool(Tool tool){
        tool.travel();
        tool.print();
        tool.study();
    }
}

The above is Java abstract class, inheritance and polymorphism and adapter implementation details, more about java abstract class inheritance polymorphism information please pay attention to other related articles on this site!


Related articles: