Java Foundation (Object-Oriented II)

Abstract class:

  Because sometimes inevitable override certain subclasses of the parent class method, the method of the parent class is defined slightly excess body.

  If the parent class method to remove the body, it causes a compilation error.

  If the entire method to remove the parent class and subclass lost the characteristics of the polymorphism (subclasses from the same parent class perform different logic when you call the same method).

  Therefore be used to abstract parent class and the parent class modified, then the method can not write the body of the method is used only to let subclasses inherit and override.

  Subclasses must override the abstract parent class.

  Use remaining the same as those of ordinary class abstract class (constructor).

  An abstract class can inherit the abstract class, and then continue to add an abstract method.

 1 public class Demo {
 2     public static void main(String[] args) {
 3         Person[] arr = {
 4                 new Worker("worker"),
 5                 new Student("student"),
 6 //                new Person()
 7                 new Student1("student1")
 8         };
 9         for(Person i:arr){
10             i.run();
11         }
12     }
13 }
14 
15 //Defining a first abstract class 
16  abstract  class the Person {
 . 17      protected String name;
 18 is      public the Person (String name) {
 . 19          the this .name = name;
 20 is      }
 21 is      public  abstract  void RUN ();
 22 is  }
 23 is  // inherited from the first abstract subclasses, 
24  public  class Student the extends the Person {
 25      public Student (String name) {
 26 is          Super (name);
 27      }
 28      @Override
 29     public  void RUN () {
 30          System.out.println ( the this .name + "RUN." );
 31 is      }
 32  }
 33 is  // subclasses of abstract classes from the first 
34 is  public  class the Worker the extends the Person {
 35      public the Worker (String name) {
 36          Super (name);
 37 [      }
 38 is      @Override
 39      public  void RUN () {
 40          System.out.println ( the this .name + "RUN." );
 41 is      }
 42 is  }
43  // inherited from the first abstract class abstract class 
44 is  abstract  class of Person1 the extends the Person {
 45      public of Person1 (String name) {
 46 is          Super (name);
 47      }
 48      abstract  void Study ();
 49  
50  }
 51 is  // inherited from the second subclass of abstract class 
52 is  public  class student1 the extends of Person1 {
 53 is      public student1 (String name) {
 54 is          Super (name);
 55      }
 56 is      public  void RUN () {
57         System.out.println("Student1.run");
58     }
59     public void study(){
60         System.out.println("Student1.study");
61     }
62 }

 

Guess you like

Origin www.cnblogs.com/nanhua097/p/11248332.html