java upcast and downcast 1

  In the java inheritance system, it is considered that the parent class (superclass) is in the upper layer, and the subclass is in the lower layer (derived class), and the upward transformation is to convert the subclass object into the parent class object.

1  public  class Father {    
 2      public  void eat(){
 3          System.out.println("I am the method of the parent class to eat..." );
 4      }
 5 }
1  public  class Son extends Father{
 2      
3      public  void eat(){
 4          System.out.println("I am Son's way to eat..." );
 5      }
 6      public  void sleep(){
 7          System.out .println("Son sleeps" );
 8      }
 9 }

 test

1  public  static  void main(String[] args) {
 2             Father f= new Son(); // Subclass            upcast 
3             Person p= new Son(); // Ordinary new 
4             f.eat();    
 5              / / f.sleep(); cannot call this method this method does not exist 
6             p.eat();
 7             p.sleep();
 8      
9          
10      }    
 11  Running result: I am a subclass method eat
 12                  I am a subclass Ways to Eat
 13                  to Sleep

  f.sleep() cannot be called here, that is, after the upward transformation, the methods not defined in the parent class will be lost, and only the methods defined in the parent class can be called. So what is the use of upcasting? Why not directly call the object defined by the subclass itself?

   If you directly call new Son(), you can also call it. This does not reflect the object-oriented abstract programming idea, and the code scalability is poor. 

   If the parent class also has a subclass Son1 

1 public class Son1 extends Father {
2     public void eat(){
3         System.out.println("son1 的eat方法");
4     }
5     public void sleep(){
6         System.out.println("son1 的sleep方法");
7     }
8 
9 }

  See the test code below

 1 public class Test {
 2     public static void main(String[] args) {
 3        
 4         tt(new Son());
 5         tt(new Son1());
 6     }
 7     
 8     
 9     public static void tt(Son son){
10         
11         son.eat();
12     }
13     public static void tt(Son1 son){
14         
15         son.eat();
16     }
17 
18 }

    My tt method needs to call some or all methods of each subclass. The code is as shown above. If I have many subclasses, the code will be particularly heavy in repetition rate.

  Upcasting is the perfect solution to this problem: no matter how many subclasses I have, I can do it with one method. .

    

1  public  class Test {
 2      public  static  void main(String[] args) {
 3         
4          tt( new Son());
 5          tt( new Son1());
 6      }
 7      
8      
9      public  static  void tt(Father son){
 10          // The parent class object is used as an instantiation parameter 
11          son.eat();
 12      }
 13 }

 

 

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346264&siteId=291194637