Inheritance in java explains what extends extends

   Inheritance:
 Criteria for judging inheritance "IS-a"
 In the subclass, you can add fields and methods, but absolutely cannot delete any inherited fields and methods;
 If the constructor of the subclass does not explicitly call the superclass The constructor of the superclass will automatically call the default (parameterless) constructor of the superclass. If the superclass has no constructor without parameters, and the constructor of the subclass does not explicitly declare other constructor, the Java compiler will report an error. From this sentence, it follows that the subclass will call the superclass's constructor anyway.
That is to say:
    The subclass will definitely call the construction of the superclass : When
     Super (with parameters) is not written in the subclass, Super (without parameters) will be automatically called.
      When Super (parameter) is written, find the structure matching the parent class parameter.
     If the parent class does not have a structure without parameters, and the subclass structure does not explicitly declare to call other structures of the parent class, it cannot be compiled.
class TestSuper{    
  TestSuper(int i){}
}
class TestSub extends TestSuper{
 
}
public class TestAll{    
  public static void main(String args[]){    
  new TestSub();      
  }
}

Cannot compile successfully

Solution 1: Add the following constructor in the subclass TestSub:
public TestSub() {
  super(0);
  }
Solution 2: Add a parameterless constructor TestSuper(){} in the TestSuper class
or: add in TestSub The parameterless constructor TestSub(){}

is simpler to understand: if the base class constructor (TestSuper) in the subclass is not called, the compiler will report that it cannot find a constructor of the form TestSuper(), in addition to , in the subclass constructor, the call to the superclass constructor is the first thing that must be done, the
compiler will force us to set the call to the superclass constructor first in the body of the subclass constructor, which means , nothing can appear before it. So we see: Note: The statement that uses super to call the constructor must be the first statement of the subclass constructor


 There are two keywords in inheritance: this and super
These two keywords have two uses:
This :  Refers to implicit parameters. In this case, whoever calls it, this represents who.
    Call other constructors of this class;
Super:  Call the method of the superclass;
    Call the constructor of the superclass;
it can be seen that Super is related to the superclass, and is called the method or construction of the superclass. And this is only related to this class.

 The condition for overriding is that inheritance must exist and only methods have overrides. Ordinary methods (that is, non-static methods) in subclasses can only override non-static methods in superclasses. Static methods can be overridden, but the methods of parent and child classes must be static methods.
It can be summed up as follows:
 There must be inheritance and only methods have overrides
 Non-static methods can only override non-static methods of the parent class
 Static methods can be overridden, but the methods of parent and child classes must be static methods. Static method overriding does not support polymorphism, and still calls the method of the parent class.


★Rewritten syntax requirements:
The method name, parameter list and return type must be the same.
The access authority cannot be smaller, that is to say, the access authority of the method overridden by the subclass cannot be smaller than that of the parent class.
 Cannot throw a larger exception, that is to say, it cannot throw a larger exception than the parent class.

The running order of the program after inheritance:
    Father first, then child;
    This first, then Super;
    Block first, then constructor;
★What can be inherited:
  Attributes can be inherited, but only Public attributes can be called
  Only Public methods can be inherited, private methods cannot be inherited
  Construct cannot be inherited, but must be called by subclasses.

★Inheritance and design:
  Why do you need inheritance? What is the benefit of inheritance?
  If you have the same attributes in multiple classes, you can extract those same attributes as the parent class, and those other classes inherit the parent class and the child class Will have all the non-private properties and methods of the parent class, which can reduce the redundancy of the code and enhance the readability and robustness of the code

   "Inheritance" is a concept in object-oriented software technology. If a class A inherits from another class B, call this A "subclass of B" and call B the "parent class of A". Inheritance allows subclasses to have various properties and methods of the parent class without having to write the same code again. When the subclass inherits the parent class, it can redefine some properties and override some methods, that is, override the original properties and methods of the parent class, so that it can obtain different functions from the parent class.

The basic concept of
inheritance 1. Inheritance is one of the three major characteristics of object-oriented. (encapsulation, inheritance, polymorphism)
2. The inherited class is the parent class, and the class that inherits the parent class is the subclass
3. Inheritance enables an object to directly use the properties and methods of another object
4. Inheritance enables code reuse


Inheritance Restrictions
1. Java can only display single inheritance, that is, a class can only have one parent class
2. Multiple inheritance is allowed
3. Inheritance can only inherit non-private properties and methods
4. Constructors cannot be inherited

Let ’s see an example

Case 1:

Java code collection code
public class Mystring {  
      
    public static void main(String[] args) {  
          
          student ok=new student("xiaoming");  
              ok.say();//The say method is also inherited  
    }  
  
}  
class person  
{  
    public String name;   
    //Construction method  
    public person()  
    {  
       System.out.println("I am the person constructor");  
    }  
    public void say()  
    {  
        System.out.println("Name is: "+name);  
    }  
}  
class student extends person//inherit the person class  
{  
    public student(String name)  
    {  
          this.name=name;//person's name property is inherited  
    }     
}  

The output results are:



Conclusion 1: When the subclass is instantiated, the constructor of the parent class will be called first, and then the instantiation operation will be performed by itself.

Case 2:



    At this time, the keyword super is introduced. super represents the reference of the parent class and can call the methods and properties of the parent class. If you call the say method of the parent class, you can use it. super.say() call.

Conclusion 2: When the parent class has no default constructor, the subclass must explicitly call the constructor of the parent class


. Overriding

   concept of inherited methods: In java, the subclass can inherit the methods in the parent class, but sometimes the subclass does not If you don't want to use the method of the parent class as it is, but want to make some modifications, you need to use method rewriting, also known as method overriding.

Features that need to be paid attention to in method overriding
1. The return value, function name, and parameter list of the two methods of the overridden parent class and subclass must be exactly the same
2. The exception thrown by the subclass cannot exceed that thrown by the corresponding method of the parent class 3.
The access level of the subclass method cannot be lower than the access level of the corresponding method of the parent class (for example, the parent class method is protected, and the subclass overrides must use protected or public)

such as:
Java code Collection code
public class Mystring {  
      
    public static void main(String[] args) {  
          
          student ok=new student("xiaoming");  
          ok.say();//The say method is also inherited  
    }  
  
}  
class person  
{  
    public String name;   
     
    public void say()  
    {  
        System.out.println("Name is: "+name);  
    }  
}  
class student extends person//inherit the person class  
{  
    public student(String name)  
    {  
        this.name=name;//person's name property is inherited  
    }     
      
    //say method override  
    public void say()  
    {  
        System.out.println("The rewritten method name is: "+name);  
      
    }  
}  


Output result:


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933645&siteId=291194637