Inheritance of Java Review

Keywords: Java

This blog mainly summarizes the knowledge of class inheritance.

1. Basic Concept of Inheritance
(1) Inheritance is one of the three characteristics of object-oriented
(2) Classes inherited are called parent classes, and classes inherited from parent classes are subclasses.
(3) Inheritance refers to the direct use of attributes and methods of one object by another.
(4) Code reuse can be achieved through inheritance

2. Inheritance Grammar
[Access privileges] class subclass name extends parent class name
{
Class Definition Body;
}

3. Restrictions on inheritance
(1)Java can only achieve single inheritance, that is, a class can only have one parent class.
(2) Multi-level inheritance is allowed, that is, a child class can have a parent class, and a parent class can have other parent classes.
(3) Inheritance can only inherit non-private attributes and methods
(4) Constructive methods cannot be inherited

4. Instance process of subclasses
When the subclass is instantiated (that is, when the new keyword new object is used, the constructor is called), the parent class is first instantiated and the default constructor of the parent class is invoked. Subclasses then instantiate themselves.

When there is no default constructor in the parent class, the constructor of the parent class must be displayed in the subclass constructor, using the super() keyword, with the same parameters as the constructor of the parent class.

5. Method rewriting
(1) definition
In java, subclasses can inherit methods from parent classes without rewriting the same methods. But sometimes subclasses do not want to inherit the parent's method unchanged, but want to make some modifications, which requires method rewriting. Method rewriting is also called method coverage.
(2) characteristics
a. The return values, method names and parameter lists of the two methods rewritten by the method must be identical.
b. Exceptions thrown by subclasses should not exceed those thrown by corresponding methods of parent classes (exceptions thrown by subclasses should not exceed those thrown by parent classes)
c. The access level of the subclass method should not be lower than that of the corresponding method of the parent class (the access level of the subclass should not be lower than that of the parent class)

6.super keyword
(1) Use super to call attributes in the parent class
(2) Use super to call methods in the parent class
(3) Use super to call the constructor in the parent class

7.final keyword
(1) Declare a class with final keyword
This class cannot be inherited
(2) Declare a method using final keyword
This method cannot be overridden by subclasses
(3) Declare a constant using the final keyword
The value of the variable cannot be modified, and the final value
Assignment of Constants
(1) Assignment at the same time of declaration
(2) Values can be assigned by constructive methods

ExtendDemo

public class ExtendsDemo {
    public static void main(String args[])
    {
        HomeChicken homeChicken = new HomeChicken();
        homeChicken.desc();
        System.out.println("====================");

        Pheasant pheasant = new Pheasant("Xiao Bai");
        pheasant.desc();
    }
}
//Parent class
class Chicken1{
    String name;
    public Chicken1(String name)
    {
        System.out.println("Chicken:"+name);
    }

    public void desc()
    {
        System.out.println("I am a chicken");
    }
}
//Subcategory (domestic chicken)
class HomeChicken extends Chicken1
{
    public HomeChicken()
        {
            //Representing a reference to a parent class must be the first
            super("Xiao Bai");
            System.out.println("HomeChicken");
        }
}
//Subclass (pheasant)
class Pheasant extends Chicken1
{
    public Pheasant(String name)
    {
        super("Xiao Bai");
        this.name=name;
        System.out.println("Pheasant");
    }
}

SuperKeyWordDemo

public class superKeyWordDemo {
    public static void main(String args[])
    {
        Duck1 duck1 = new Duck1("Small black",100);
        BlackDuck blackDuck=new BlackDuck("Xiao Bai",300);
        blackDuck.desc();
        duck1.count();
    }
}
class Duck1 {
    protected String name;
    protected int price;

    public Duck1(String name,int price)
    {
        this.name=name;
        this.price=price;
    }
    public void count()
    {
        System.out.println("duck price is"+price);
    }
}
class BlackDuck extends Duck1{
    public BlackDuck(String name,int price)
    {
        super(name,price);
    }
    public void desc()
    {
        System.out.println("I am a black duck."+super.name);
        super.count();
    }

}

Posted by greg on Mon, 25 Mar 2019 10:36:28 -0700