The inheritance of Java classes is mainly accomplished by extends keywords. Once inherited, the subclass gets the member property of the parent class and the member method. Basic syntax:
Class Name extends parent class name
{
}
Example: Fruti.java
public class Fruit
{
public void GetColor (String color)
{
SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);
}
}
Pear.java
public class Pear extends Fruit
{
}
Note: Only single inheritance is allowed in Java, and multiple inheritance is not allowed, that is, a subclass can have only one parent class, but in Java it allows multiple layers of inheritance such as: Pear can inherit fruit, Then p can inherit the Pearjavasuper keyword super The main function is to complete the subclass call the content in the parent class, that is, call the property or method in the parent class, here to distinguish the This keyword basic structure:
Super. Attribute names (all refer to the parent class)
Super. Method name (all refers to the parent class)
For example:
Fruit.java
public class Fruit
{
String name;
public void GetColor (String color)
{
SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);
}
}
Pear.java
public class Pear extends Fruit
{
Public Pear ()
{
Super.name= "Crazy software Java Training"; Call the Name property of the parent class and assign a value.
}
}
Java class rewrite (overwrite) Everyone should know now, once the subclass inherits the parent class, then even the method of the corresponding inheritance, but because each subclass also has its own personality, where there is a Java class to rewrite when a subclass inherits a parent class, When a method in a subclass is exactly the same as the name of a method in the parent class, the number of arguments, and the type, the method in the subclass is called to replicate the methods in the parent class, for example:
Fruit.java
public class Fruit
{
String name;
public void GetColor (String color)
{
SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);
}
}
Pear.java
public class Pear extends Fruit
{
Public Pear ()
{
Super.name= "Crazy software Java Training"; Call the Name property of the parent class and assign a value.
}
Overriding the parent class method
public void GetColor (String color)
{
System.out.println ("Here is the method in the subclass, I rewrite its contents");
}
}
Java class overloading compared to the above, the Java class overrides, overloading refers to the method name consistent, but the parameter or return value is not the same. For example:
Fruit.java
public class Fruit
{
String name;
public void GetColor (String color)
{
SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);
}
}
Pear.java
public class Pear extends Fruit
{
Public Pear ()
{
Super.name= "Guangzhou java Training"; Call the Name property of the parent class and assign a value.
}
Overloading of the parent class method
public void GetColor (String color,stringname)
{
System.out.println ("Here is a method in a subclass, I overload its contents");
}
}
Java object-oriented-inheritance