Object subclass

Features 1. subclass objects:
When you create a subclass of an object by subclass constructors, not only members of the variable declaration of a subclass is allocated memory, and the parent class member variables are also allocated memory space, but only where the subclass inherits that some members of the variable assigned to the variable as a subclass. But why should not inherit the variables are allocated memory? Because there may be a portion of the method of the subclass inherits from the parent class, which is part of the operation but it can inherit methods variables. So we need.

 class People{
   private int leg = 2;
   int money;
   void action(){
     leg = 5;
     System.out.println("I am a man");
     }
 }

class Man extends People{
   int height;
}

public class human{
 public static void main(String args[]){
   Man you = new Man();   /*这条语句的执行意味着,money,leg,height,
   action都被分配了空间*/
   you.action();//这条语句的执行说明了子类可以通过继承的方法来操作没继承来的变量
  }
}

2.instanceof operator (which is actually a symbol determination)
the instanceof operator is java unique binary operator, which is the object of the left operand, the operand is right class, when the left operand is right when the object class or subclass created, instanceof operation result is true, otherwise it is false.

 class People{
   private int leg = 2;
   int money;
   void action(){
     leg = 5;
     System.out.println("I am a man");
     }
 }

class Man extends People{
   int height;
}

public class human{
 public static void main(String args[]){
   Man you = new Man();   /*这条语句的执行意味着,money,leg,height,
    action都被分配了空间*/
   if(you instanceof Man)
   {
    System.out.println("I am a man");
   }
  
  }
}
结果会输出:I am a man
如果将 if(you instanceof Man)中的Man改为People呢?

3. The members of the hidden variables
as the name of the member variable name declared and inherited from the parent class to the member variables of the same (type declaration can be different), in this case, the subclass hides inherited member variables ( this is not meant to be hidden members can not be used, we can still use the super keyword to use).
It features a subclass hides inherited member variables are as follows:
member variables method (1) subclass object and subclasses own definition of operating with the same name as the parent class refers to the member variable subclass re-declaration.
(2) sub-class object can still invoke operations subclasses hidden member variables inherited from the parent class, that is, the member variable subclass inherits the methods of operation must be member variables inherited by subclasses or hidden.

 class People{
   private int leg = 2;
   int money;
   void action(){
     leg = 5;
     System.out.println("I am a man");
     }
 }

class Man extends People{
   int height;
   int money = 5;
}

public class human{
 public static void main(String args[]){
   Man you = new Man();   /*这条语句的执行意味着,money,leg,height,
   action都被分配了空间*/
   you.action();//这条语句的执行说明了子类可以通过继承的方法来操作没继承来的变量
     System.out.println(you.money);//People里的变量money被隐藏
  }
}
结果为:I am a man
         5

4. The method of rewriting (method overriding)
subclasses to override methods inherited by the hide. If a subclass can inherit a parent class, then have the right to override this method, the subclass method by rewriting can hide inherited methods, by means of a subclass can override the parent class status and behavior change for their own state and behavior.

 class People{
   private int leg = 2;
   int money;
   void action(){
     leg = 5;
     System.out.println("I am a man");
     }
 }

class Man extends People{
   int height;
   int money = 5;
    void action(){
     System.out.println("I am a hunman");
     }
}

public class human{
 public static void main(String args[]){
   Man you = new Man();   /*这条语句的执行意味着,money,leg,height,
   action都被分配了空间*/
   you.action();//这条语句的执行说明了子类可以通过继承的方法来操作没继承来的变量
     System.out.println(you.money);//People里的变量money被隐藏
  }
}
结果为:I am a human
         5

5.super keywords
(1) operating with a super hidden member variables and methods

 class People{
   private int leg = 2;
   int money;
   void action(){
     leg = 5;
     System.out.println("I am a man");
     }
 }

class Man extends People{
   int height;
   int money = 5;
    void action(){
     System.out.println("I am a hunman");
     }
}

public class human{
 public static void main(String args[]){
   Man you = new Man();   /*这条语句的执行意味着,money,leg,height,
   action都被分配了空间*/
   you.action();//这条语句的执行说明了子类可以通过继承的方法来操作没继承来的变量
     System.out.println(super.money);//People里的变量money被隐藏
  }
}
结果为:I am a human
         0

(2) using the super constructor call the parent class
when an object is created with the constructor of a subclass, the subclass constructor is always the first to call a parent class constructor, that is, if the sub-class constructor indicating that no significant use of the constructor of the parent class, subclass calls the parent class constructor with no arguments.
Since the sub-class does not inherit the parent class constructor, so subclasses need to use in its super constructor to call the parent class constructor, but super must be a subclass constructor of the first statement, if the subclass the constructor did not write super statement, the compiler will default there.

6.final keyword
final meaning can not be changed. As the name suggests, with this keyword modified class variables and can not be changed.
So: final class can not be inherited, final variable is constant, due to the constant permitted during operation again changed, so constant in a statement it is necessary to give value.

Above learning summary includes personal, such as the existence unreasonable, welcome to point out, learn from each other.

Published 35 original articles · won praise 0 · Views 1303

Guess you like

Origin blog.csdn.net/c1776167012/article/details/102821037