The concept of inheritance
Inheritance is a cornerstone of Java object-oriented programming because it allows the creation of hierarchal classes.
Inheritance is the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class field and method, or the child class inherits from the parent class, so that the child class has the same behavior as the parent class.
Inheritance in life:
Herbivores and carnivores are also animals.
Therefore, the relationship that inheritance needs to meet is: is-a, the parent class is more generic, and the child class is more specific.
Although both herbivores and carnivores are animals, there are differences in the properties and behaviors of the two, so the subcategories will have the characteristics of their own characteristics.
Class inheritance format
Using the extends keyword in Java can state that a class is inherited from another class. The general form is as follows:
Class inheritance format
class parent class {
}
class subclass extends parent class {
}
}
class subclass extends parent class {
}
Why do you need to inherit
Next we illustrate this requirement through examples.
Development of animal species, including penguins and rats, are as follows:
- Penguin: attribute (name, id), method (eat, sleep, introduce myself)
- Mouse: Attribute (Name, id), Method (Eat, Sleep, Introduce)
Penguins:
public class Penguin {
private String name;
private int id;
public Penguin(String myName, int myid) {
name = myName;
id = myid;
}
public void eat() {
System . out . println ( name + " being eaten " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) {
System . out . println ( " Hello everyone! I am "+ id + " sign " + name + " . " ) ; }
}
System . out . println ( name + " being eaten " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) {
System . out . println ( " Hello everyone! I am "+ id + " sign " + name + " . " ) ; }
}
Mouse class:
public class Mouse {
private String name;
private int id;
public Mouse(String myName, int myid) {
name = myName;
id = myid;
}
public void eat(){
System . out . println ( name + " is eating " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) { System . out . println ( "Hello everyone! I am " + id + " sign " + name + " . " ) ; }
}
System . out . println ( name + " is eating " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) { System . out . println ( "Hello everyone! I am " + id + " sign " + name + " . " ) ; }
}
From these two codes, we can see that the code is duplicated. The result is that the code is large and bloated, and the maintenance is not high (maintenance is mainly when the later needs to be modified, it needs to modify a lot of code, and it is easy to make mistakes). Therefore, to fundamentally solve the problem of these two pieces of code, you need to inherit, extract the same part of the two pieces of code to form a parent class:
Common parent class:
public class Animal {
private String name;
private int id;
public Animal(String myName, int myid) {
name = myName;
id = myid;
}
public void eat(){
System . out . println ( name + " is eating " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) {
System . out . println ("Hello everyone! I am "+ id + " sign " + name + " . " ) ; }
}
System . out . println ( name + " is eating " ) ; } public void sleep ( ) {
System . out . println ( name + " sleeping " ) ; }
public void introduction ( ) {
System . out . println ("Hello everyone! I am "+ id + " sign " + name + " . " ) ; }
}
The Animal class can be used as a parent class. After the Penguin class and the Mouse class inherit this class, they have the properties and methods of the parent class. The subclasses will not have duplicate code, maintainability will improve, and the code will be more concise. , to improve the reusability of the code (reusability can be used multiple times, do not have to write the same code many times) After the inheritance of the code:
Penguins:
public class Penguin extends Animal {
public Penguin(String myName, int myid) {
super(myName, myid);
}
}
Mouse class:
public class Mouse extends Animal {
public Mouse(String myName, int myid) {
super(myName, myid);
}
}
Inherited characteristics
- Subclasses have non-private properties, methods.
- Subclasses can have their own properties and methods, ie subclasses can extend parent classes.
- Subclasses can implement parent methods in their own way.
- Java inheritance is single inheritance, but multiple inheritance, single inheritance is a subclass can only inherit a parent class, multiple inheritance is, for example, A class inherits B class, B class inherits C class, so according to the relationship is C class is B The parent class of the class, class B, is the parent of class A. This is a feature of java inheritance that distinguishes it from inheritance from C++.
- Improve the coupling between classes (deficiencies of inheritance, coupled with a high degree of coupling between the code).
Inheriting keywords
Inheritance can be implemented using both the extends and implements keywords, and all classes inherit from java.lang.Object. When a class does not inherit two keywords, it inherits from object by default (this class is in java. Lang package, so no import ) ancestors are needed .
Extends keyword
In Java, class inheritance is single inheritance, that is, a subclass can only have one parent class, so extends can only inherit one class.
Extends keyword
public class Animal {
private String name; private int id; public Animal(String myName, String myid) { //Initialize property value } public void eat() { //concrete implementation of eating method } public void sleep() { //concrete implementation of sleep method } } public class Penguin extends Animal{ }
private String name; private int id; public Animal(String myName, String myid) { //Initialize property value } public void eat() { //concrete implementation of eating method } public void sleep() { //concrete implementation of sleep method } } public class Penguin extends Animal{ }
Implements keyword
The use of the implements keyword can make java have multiple inheritance features in a disguised form. The scope of use is that the class inherits an interface. It can inherit multiple interfaces at the same time (the interface and interface are separated by commas).
Implements keyword
public interface A {
public void eat();
public void sleep();
}
public interface B {
public void show();
}
public class C implements A,B {
}
Super and this keyword
Super keyword: we can use the super keyword to achieve access to the parent class member, used to refer to the parent class of the current object.
This keyword: A reference to yourself.
Examples
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void eatTest() {
this.eat(); // this calls its own method
super.eat(); // super calls the parent method
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eatTest();
}
}
The output is:
Animal : eat: eat Dog : eat: eat Animal : eat: eat
Final keyword
The final keyword declaration class can be defined as a class that cannot be inherited, that is, the final class; or it can be used to decorate methods that cannot be overridden by subclasses:
- Declaration class:
Final class class name {//class body} Class class name { // body type}
- Declarative method:
Modifiers (public/private/default/protected) final Return Value Type Method Name () {//Method Body}Public / private / default / protected ) final return value type method name () { // method body}
Note : The instance variable can also be defined as final, and the variable defined as final cannot be modified. Methods declared as final are automatically declared final but instance variables are not final
Constructor
Subclasses cannot inherit the constructor (constructor or constructor) of the parent class. If the constructor of the parent class has parameters, the constructor of the parent class must be explicitly invoked through the super keyword in the constructor of the child class. With a suitable list of parameters.
If the parent constructor has no arguments, the superclass constructor does not need to use the super keyword to call the parent constructor, and the system will automatically call the parentless constructor.
Examples
class SuperClass {
private int n;
SuperClass(){
System.out.println("SuperClass()");
}
SuperClass(int n) {
System.out.println("SuperClass(int n)");
this.n = n;
}
}
class SubClass extends SuperClass{
private int n;
SubClass(){
super(300);
System.out.println("SubClass");
}
public SubClass(int n){
System.out.println("SubClass(int n):"+n);
this.n = n;
}
}
public class TestSuperSub{
public static void main (String args[]){
SubClass sc = new SubClass();
SubClass sc2 = new SubClass(200);
}
}
The output is:
SuperClass(int n)( int n ) SubClassSubClass SuperClass()SuperClass () SubClass(int n):200SubClass ( int n ): 200



Comments
Post a Comment