Functions are also called methods!
Inheritance: Use extends keyword in java to represent inheritance relationships. Super is used to inherit parent class methods and parameters.
Inheritance means that the child class inherits the characteristics and behavior of the parent class, so that the child class has the same behavior as the parent class.
Notes:
1. When a class does not inherit any class, the system inherits Object by default.
2. The parent class is also called the base class, super class, and super class, and the subclass is also called the derived class. This is caused by translation problems.
3. Java inheritance is single.
4. Subclasses cannot inherit the constructor method of the parent class, but they can inherit the parameters of the constructor method class.
5. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class. However, subclasses cannot inherit the properties and methods modified by the parent class private.
Syntax format:
System default inheritance
class class name extends Object{/*code block*/}
Correct inheritance syntax
class subclass name extends parent class name {/*code block*/}
Error inheritance syntax
class subclass name extends parent class name, parent class name {/* does not support multiple inheritance*/}
Create a parent class with the class name Father:
public class Father {int a;int b;int addSum;Father(int a,int b){ //The constructor of the parent class this.a=a;this.b=b;}void Cal(){ //The parent class's own method addSum=a+b;System.out.println(addSum);}public static void main(String[] args){Father f=new Father(2,2); //Create an object to initialize f.Cal(); //The parent class calls the parent class's method}}Parent class run result: 4
Create a subclass with the subclass named Son:
public class Son extends Father{Son(int a, int b) { //The constructor of the subclass super(a, b); //Inherit the parameters from the parent class}void son(){ //The subclass's own method super.Cal(); //The subclass calls the parent class's method}public static void main(String[] args){Son s=new Son(3,3); //Create an object to initialize s.son(); //The subclass calls the subclass's method}}Subclass run result: 6