Introduction to Java programming tutorial - the characteristics of inheritance

Table of contents

inherit 

Create a subclass

Inherited definition format

property inheritance

error sample program

Subclass variables hide parent class variables


inherit 

       Inheritance is a relationship that exists between two classes in object-oriented programming.
        When a class has all the data and methods of another class, it is said that there is an inheritance relationship between the two classes.
        The inherited class is called the parent class or super class, and the class that inherits the data and methods of the parent class or super class is called the sub class.

 

example

        Inherited relationships are a good description of the connections that exist between things in the real world.

        For example, consider the two classes of ships and passenger ships. Ships have attributes (data) such as tonnage, speed per hour, and waterline, and have behaviors (methods) such as driving and berthing; while passenger ships have all the attributes and behaviors of ships, they also have their own special attributes (such as passenger capacity) and services (such as meals, etc.). If the ship is regarded as the parent class, the passenger ship is the subclass of the ship, and the relationship between this group of parent and child classes is expanded up and down, and the hierarchical relationship diagram shown in the figure below can be obtained.

 Inheritance has the following characteristics:

1.  The inheritance relationship is transitive. If class C inherits class B , and class B inherits class A , it can be considered that class C also inherits class A , that is, class C has the attributes and methods in class A , and also has the unique attributes and methods of class B. Inherited properties and methods, although implicit, are still properties and methods of class C. Inheritance is the most effective means to construct, establish and extend new classes on the basis of some more general classes. Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship between related classes.

2.  Provide software reuse function. If class B inherits class A , only a few features ( data members and member methods ) that are different from the base class ( class A) need to be described when creating class B. This approach can reduce the redundancy of code and data, greatly increasing the reusability of the program.

3.  Enhance consistency. It is used to reduce the interface and interface between modules, greatly increasing the maintainability of the program.

4.  Single inheritance mechanism. For security and reliability considerations, Java only supports single inheritance, but multiple interfaces can be implemented.

Create a subclass

Inherited definition format

The inheritance relationship in        Java is realized through the extends keyword.

       When defining a class, use the extends keyword to indicate the parent class of the newly defined class, and the newly defined class is called a subclass of the specified parent class, thus establishing an inheritance relationship between the two classes.

       The format of the extends keyword application is as follows:        

         class  subClass_name  extends  superClass_name

       The biggest feature of the inheritance mechanism is that it allows subclasses to inherit the properties and methods of the parent class.

         Inheritance relationship sample program. ( The subclass SubClass inherits two member variables a , b and a method fn1() of the parent class SuperClass , and the subclass itself newly defines a member variable c and method fn2() , so the subclass currently has 3 member variables and two methods. )

  

code show as below:

father:

package test;

public class SuperClass {
	int a = 10;
	String b = "test";
	public int fn1(){
		return a;
	}
}

Subclass: 

package test;

public class SubClass extends SuperClass {
	int c = 20;
	public int fn2(){
		return c;
	}
}

Main program output: 

package test;

public class JavaDemo {
	public static void main(String[] args) {
		SuperClass sup = new SuperClass();
		SubClass sub = new SubClass();
		
		System.out.println("父类:"+sup.a+" "+sup.b+" "+sup.fn1());
		System.out.println("子类:"+sub.a+" "+sub.b+" "+sub.c+" "+sub.fn1()+" "+sub.fn2());
	}
}

 

 

property inheritance

       The properties of the parent class (including all member variables and methods) will be inherited by the subclass, but being inherited does not necessarily mean that they can be accessed.
       In Java, only non-private (public, or default) variables and methods can be inherited and directly accessed by subclasses of the same package, while for private (private) attributes in the parent class, subclasses will not have the right to directly access them.


error sample program

Private property inheritance. In the fn3() of the subclass SubClass , the private variable d in the parent class is accessed beyond the authority, resulting in a compilation error.

 

Subclass variables hide parent class variables

       Subclasses can also redefine the attributes (member variables) inherited from the parent class.

       Simply put, subclass variables hide parent class variables. When the subclass executes the method defined by itself, it operates on the variable redefined by itself, hiding the variable inherited from the parent class. When a subclass executes a method inherited from a parent class, if a member variable with the same name needs to be used, the member variable of the parent class is used.

        Hidden variable example program.

 

Guess you like

Origin blog.csdn.net/u010764893/article/details/131124564