Application introduction based on inheritance of Java subclasses

  • 2020-04-01 01:47:46
  • OfStack

1. Definition of inheritance

Some of the members of a subclass are defined by the subclass itself and some are inherited from its parent. Subclasses inherit the parent class's member variables as one of their own, as if they were directly in the subclass

Declaration can be manipulated by any of its own instance methods in a subclass. That is, a member of a subclass inherited should be a full member of the class if the instance method declared in the subclass cannot operate on the parent class

The subclass inherits the methods of the parent class as methods in the subclass, just as they are directly in the subclass, can be declared by the subclass itself

Method invocation.

2. Subclass parent class in a package

A subclass can inherit from its parent class as its own member variables and methods in addition to private. The access rights of inherited member variables and methods are unchanged.

/ test/SRC/com/b510 / Parent. Java


package com.b510;

 
 public class Parent {
     private int numbera = 10;
     protected int numberb = 20;

     
     void sum() {
         numberb = numbera + numberb;
         System.out.println(numberb);
     }

     
     private int getNumbera() {
         System.out.println(numbera);
         return numbera;
     }

     
     public void print() {
         System.out.println("numberb+numbera=" + (numberb + numbera));
     }

     
     protected void say() {
         System.out.println("Hello,i'm parent class!");
     }
 }

/ test/SRC/com/b510 / ParentAndSubClass. Java

package com.b510;

 
 public class ParentAndSubClass {
     public static void main(String[] args) {
         SubClass sub = new SubClass();
         //Inherits the methods in the parent class
         sub.say();
         sub.sum();
         sub.print();
         //Subclass its own method
         sub.proFun();
         sub.youYuan();
     }

 }

/ test/SRC/com/b510 / ttf_subclass. Java

package com.b510;

 
 public class SubClass extends Parent {

     
     void youYuan() {
         System.out.println(" Subclasses cannot inherit numbera But can inherit numberb=" + numberb);
         System.out.println(" This is the friend method in the subclass ");
     }

     
     private void priFun() {
         System.out.println(" This is a private methods ");
     }

     
     protected void proFun() {
         System.out.println(" You can inherit in subclasses numberb=" + numberb);
     }

 }

Operation results:

Hello,i'm parent class!
 30
 numberb+numbera=40
  You can inherit in subclasses numberb=30
  Subclasses cannot inherit numbera But can inherit numberb=30
  This is the friend method in the subclass 

Summary: when subclasses and superclasses are in the same package, subclasses cannot inherit private variables and methods from their superclasses.

3. If the child class and the parent class are not in the same package
/ test/SRC/com/Parent. Java


package com;

 
 public class Parent {
     private int numbera = 10;
     protected int numberb = 20;

     
     void sum() {
         numberb = numbera + numberb;
         System.out.println(numberb);
     }

     
     private int getNumbera() {
         System.out.println(numbera);
         return numbera;
     }

     
     public void print() {
         System.out.println("numberb+numbera=" + (numberb + numbera));
     }

     
     protected void say() {
         System.out.println("Hello,i'm parent class!");
     }
 }

/ test/SRC/com/b510 / ParentAndSubClass. Java

package com.b510;

 
 public class ParentAndSubClass {
     public static void main(String[] args) {
         SubClass sub = new SubClass();
         //Inherits the methods in the parent class
         sub.say();
         sub.print();
         //Subclass its own method
         sub.proFun();
         sub.youYuan();
     }

 }

/ test/SRC/com/b510 / ttf_subclass. Java

package com.b510;

 import com.Parent;

 
 public class SubClass extends Parent {

     
     void youYuan() {
         System.out.println(" Subclasses cannot inherit numbera But can inherit numberb=" + numberb);
         System.out.println(" This is the friend method in the subclass ");
     }

     
     private void priFun() {
         System.out.println(" This is a private methods ");
     }

     
     protected void proFun() {
         System.out.println(" You can inherit in subclasses numberb=" + numberb);
     }

     @Override
     protected void say() {
         System.out.println(" This overrides the superclass protected Method, say(), You can also get numberb=" + numberb);
     }
 }

Operation results:

1  This overrides the superclass protected Method, say(), You can also get numberb=20
2 numberb+numbera=30
3  You can inherit in subclasses numberb=20
4  Subclasses cannot inherit numbera But can inherit numberb=20
5  This is the friend method in the subclass 

Summary: subclasses and superclasses are not in the same package. Subclasses can inherit protected, public variables and methods. The access rights of inherited members or methods are unchanged.


Related articles: