java 继承示例

Prerequisite: Inheritance and its implementation in Java

先决条件: 继承及其在Java中的实现

(Type of inheritance in Java)

In Java programming, there are following types of the inheritances,

几种类型的继承

  1. Single Inheritance
    单继承
  2. Multiple Inheritances (Through Interface)
    多重继承(通过接口)
  3. Multilevel Inheritance
    多级继承
  4. Hierarchical Inheritance
    层次继承

(1) Single Inheritance)

If a class extends another class (i.e. the only one class).

如果一个类扩展了另一个类(即唯一一个类)。

Syntax:

句法:

class Parent {
        // Fields and Methods
    }

    class Child extends Parent {
        // Fields and Methods
    }

Example of Single Inheritance:

单一继承的示例:

/*Java program to demonstrate the  
 concept of inheritance */

// Parent class 
class Parent {
    // The Parent class has one method
    // displayParentMessage() method to print message of Parent Class
    public void displayParentMessage() {
        System.out.println("Hello, we are in parent class method");
    }
}

// Sub class or derived class
class Child extends Parent {
    // The Child subclass adds one more method
    // displayChildMessage() method to print message of Parent Class
    public void displayChildMessage() {
        System.out.println("Hello, we are in child class method");
    }
}

// Main class in this class we will create 
//object of parent and child class 
class Main {
    public static void main(String[] args) {

        // Creation of Parent class object
        Parent p = new Parent();

        // Calling Parent class method by Parent class object
        p.displayParentMessage();

        // Creation of Child class object
        Child c = new Child();

        // Calling Child class method by Child class object
        c.displayChildMessage();

        // Calling Parent class method by Child class object
        c.displayParentMessage();
    }
}

Output

输出量

D:\Programs>javac Main.java
D:\Programs>java Main
Hello, we are in parent class method
Hello, we are in child class method
Hello, we are in parent class method

(2) Multiple Inheritance (Through Interface))

If we extend more than one class. Java doesn’t support multiple inheritances directly but with the help of interface we can implement but it is similar to multiple inheritance.

如果我们扩展多个类。 Java不直接支持多重继承,但是借助我们可以实现的接口的帮助,它类似于多重继承。

Syntax:

句法:

interface interface1 {
        // Field and Method declaration
    }
    interface interface2 {
        // Field and Method declaration
    }
    Class class_name implements interface1, interface2 {}

Example of Multiple Inheritance:

多重继承的例子:

/*Java program to demonstrate the  
 concept of multiple inheritance */

interface Print {
    void print();
}
interface Show {
    void show();
}

class Main implements Print, Show {
    public void print() {
        System.out.println("Hello");
    }
    public void show() {
        System.out.println("World");
    }

    public static void main(String args[]) {
        Main obj = new Main();
        obj.print();
        obj.show();
    }
}

Output

输出量

D:\Programs>javac Main.java

D:\Programs>java Main
Hello
World

(3) Multilevel Inheritance)

If a classA extends by classB and classB extends by classC

如果将classA扩展为classB ,将classB扩展为classC,则称为多级继承。

Syntax:

句法:

class classA {
        // Fields and Methods
    }

    class classB extends classA {
        // Fields and Methods
    }
    class classC extends classB {
        // Fields and Methods
    }

Example of Multilevel Inheritance:

多级继承的示例:

/*Java program to demonstrate the  
 concept of multilevel inheritance */

// ClassA class 
class ClassA {
    // The ClassA class has one method
    // displayClassAMessage() method to print message of ClassA Class
    public void displayClassAMessage() {
        System.out.println("Hello, we are in ClassA class method");
    }
}

// ClassB class
class ClassB extends ClassA {
    // The ClassB class adds one more method
    // displayClassBMessage() method to print message of ClassB Class
    public void displayClassBMessage() {
        System.out.println("Hello, we are in ClassB class method");
    }
}

// ClassC class
class ClassC extends ClassB {
    // The ClassC class adds one more method
    // displayClassCMessage() method to print message of ClassC Class
    public void displayClassCMessage() {
        System.out.println("Hello, we are in ClassC class method");
    }
}

// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
    public static void main(String[] args) {

        // Creation of ClassA class object
        ClassA ca = new ClassA();

        // Calling ClassA class method by ClassA class object
        ca.displayClassAMessage();

        // Creation of ClassB class object
        ClassB cb = new ClassB();

        // Calling ClassB class method by ClassB class object
        cb.displayClassBMessage();

        // Calling ClassA class method by ClassB class object
        cb.displayClassAMessage();

        // Creation of ClassC class object
        ClassC cc = new ClassC();

        // Calling ClassC class method by ClassC class object
        cc.displayClassCMessage();

        // Calling ClassB class method by ClassC class object
        cc.displayClassBMessage();

        // Calling ClassA class method by ClassC class object
        cc.displayClassAMessage();

    }
}

Output

输出量

D:\Programs>javac Main.java

D:\Programs>java Main
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method

(4) Hierarchical Inheritance)

If more than one class is inherited from the base class is called hierarchical inheritance.

如果从基类继承多个类,则称为层次继承。

Syntax:

句法:

class classA {
        // Fields and Methods
    }

    class classB extends classA {
        // Fields and Methods
    }
    class classC extends classA {
        // Fields and Methods
    }

Example of Hierarchical Inheritance:

层次继承的示例:

/*Java program to demonstrate the  
concept of hierarchical inheritance */

//ClassA
class ClassA {
    // The ClassA class has one method
    // displayClassAMessage() method to print message of ClassA Class
    public void displayClassAMessage() {
        System.out.println("Hello, we are in ClassA class method");
    }
}


// ClassB class
class ClassB extends ClassA {
    // The ClassB class adds one more method
    // displayClassBMessage() method to print message of ClassB Class
    public void displayClassBMessage() {
        System.out.println("Hello, we are in ClassB class method");
    }
}

// ClassC class
class ClassC extends ClassA {
    // The ClassC class adds one more method
    // displayClassCMessage() method to print message of ClassC Class
    public void displayClassCMessage() {
        System.out.println("Hello, we are in ClassC class method");
    }
}

// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
    public static void main(String[] args) {

        // Creation of ClassA class object
        ClassA ca = new ClassA();

        // Calling ClassA class method by ClassA class object
        ca.displayClassAMessage();

        // Creation of ClassB class object
        ClassB cb = new ClassB();

        // Calling ClassB class method by ClassB class object
        cb.displayClassBMessage();

        // Calling ClassA class method by ClassB class object
        cb.displayClassAMessage();

        // Creation of ClassC class object
        ClassC cc = new ClassC();

        // Calling ClassC class method by ClassC class object
        cc.displayClassCMessage();

        // Calling ClassA class method by ClassC class object
        cc.displayClassAMessage();

    }
}

Output

输出量

D:\Programs>javac Main1.java

D:\Programs>java Main1
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassA class method

翻译自: https://www.includehelp.com/java/types-of-inheritance-in-java-with-examples.aspx

java 继承示例