inheritance + polymorphism + final + permission modifier

Directory

inherit

polymorphism

final

permission modifier


Inheritance

  • Inheritance definition:
    • You can create a child-parent relationship between classes and classes
  • Inherited Benefits
    • The repeated code in multiple subclasses can be extracted into the parent class, and the subclasses can be used directly, reducing the redundant code and improving the reusability of the code
  • Subclass inheritance content
    Non-private private
    Construction Method × ×
    Member variable √ (cannot be used)
    member The method can be added to the virtual method table√ cannot be added to the virtual method table×
    • Construction method

Polymorphic

  • Depending on the passed object, different show methods are called
  • Objects of the same type, showing different forms
  • polymorphic premise
    • Inheritance
    • There is a parent class reference pointing to a subclass object
    • There are methods to override
  • the case
    • human
      package com.duotai;
      
      public class Person {
          private String name;
          private String age;
      
          public Person() { }
          public Person(String name, String age) {
              this.name = name;
              this. age = age;
          }
      
          public String getName() { return name; }
          public void setName(String name) { this.name = name; }
          public String getAge() { return age; }
          public void setAge(String age) { this. age = age; }
      
          public void show(){
              System.out.println("Did I execute it");
          }
      }
      
    • Student class
      package com.duotai;
      
      public class Student extends Person {
          public void show(){
              System.out.println("Student:" + getName() + getAge());
          }
      }
      
    • Teacher class
      package com.duotai;
      
      public class Teacher extends Person{
          public void show(){
              System.out.println("Teacher:" + getName() + getAge());
          }
      }
      
    • Test class
      package com.duotai;
      
      public class Test {
          public static void main(String[] args) {
              Student student = new Student();
              student.setName("Xiao Min");
              student.setAge("6");
      
              Teacher teacher = new Teacher();
              teacher.setName("Mr. Wang");
              teacher.setAge("29");
      
              register(student);
              register(teacher);
          }
          public static void register(Person p){
              p. show();
          }
      }
      
    • result
  • Features of polymorphic call member variables
    • Variable call: compile to the left, run to the right
    • Method call: compile to the left, run to the right
  • Case
    Complete the code according to the requirements:
    1. Define the dog class
    \t\tAttributes:
    age, color
    \t\tBehavior:
    eat(String something) (something means something to eat)
    Housekeeping lookHome method (no parameters)
    
    2. Define the cat class
    \t\tAttributes:
    age, color
    \t\tBehavior:
    eat(String something) method (something means something to eat)
    Catch the mouse catchMouse method (no parameters)
    
    3. Define the Person class//breeder
    \t\tAttributes:
    name, age
    \t\tBehavior:
    keepPet(Dog dog, String something) method
    Function: feed the pet dog, something means the thing to feed
    \t\tBehavior:
    keepPet(Cat cat, String something) method
    Function: Feed the pet cat, something means the thing to feed
    Generate empty-parameter and parameter-constructed, set and get methods
    4. Define the test class (complete the following printing effects):
    The keepPet(Dog dog, String somethind) method prints the content as follows:
    Lao Wang, who is 30 years old, has a black 2-year-old dog
    A 2-year-old black dog clung to the bone with its front legs and ate it violently
    The keepPet(Cat cat, String somethind) method prints the content as follows:
    Old Li, who is 25 years old, has a 3-year-old gray cat
    Gray colored cat, 3 years old, squinting and eating fish with his head sideways
    5. Think:
    1. Both Dog and Cat are subclasses of Animal. In the above cases, different keepPet methods are defined for different animals, which is too cumbersome. Can you simplify it and experience the benefits of the simplification?
    2. Although both Dog and Cat are subclasses of Animal, they both have their unique methods. Can you find a way to call the unique methods in keepPet? 
    • Animal class
      package com.duotai.test;
      
      public class animal {
          private int age;
          private String color;
      
          public animal() { }
          public animal(int age, String color) {
              this. age = age;
              this.color = color;
          }
      
          public int getAge() { return age; }
          public void setAge(int age) { this. age = age; }
          public String getColor() { return color; }
          public void setColor(String color) { this. color = color; }
      
          public void eat(String something){
              System.out.println("The animal is eating" + something);
          }
      }
      
    • Cat class
      package com.duotai.test;
      
      public class Cat extends animal{
      
          public Cat() { }
          public Cat(int age, String color) {
              super(age, color);
          }
      
          @Override
          public void eat(String something){
              System.out.println(getAge() + "age of" + getColor() + "color of cat eating" + something);
          }
      
          public void cathMouse(){
              System.out.println("The cat catches the mouse");
          }
      }
      
    • Dog class
      package com.duotai.test;
      
      public class Dog extends animal {
      
          public Dog() { }
      
          public Dog(int age, String color) {
              super(age, color);
          }
      
          @Override
          public void eat(String something){
              System.out.println(getAge() + "Aged" + getColor() + "Color the dog eats" + something);
          }
      
          public void LookHome(){
              System.out.println("The dog is watching the house");
          }
      }
      
    • Person class
      package com.duotai.test;
      
      public class Person {
          private String name;
          private int age;
      
          public Person(){}
          public Person(String name, int age) {
              this.name = name;
              this. age = age;
          }
      
          public String getName() { return name; }
          public void setName(String name) { this.name = name; }
          public int getAge() { return age; }
          public void setAge(int age) { this. age = age; }
      
          public void keepPet(Dog dog,String something){
              System.out.println("The age is" + getAge() + "of" + getName() + "raised a " + dog.getColor() + "colored dog") ;
              dog. eat(something);
          }
      
          public void keepPet(Cat cat,String something){
              System.out.println("The age is" + getAge() + "of" + getName() + "raised a " + cat.getColor() + "colored cat") ;
              cat. eat(something);
          }
      
      
      
      }
      
    • Test class
      package com.duotai.test;
      
      public class Test {
          public static void main(String[] args) {
              Person person = new Person("Lao Wang",25);
              Dog dog = new Dog(2, "black");
              person.keepPet(dog,"bones");
      
      
              Person person1 = new Person("Xiao Li", 15);
              Cat cat = new Cat(1, "yellow");
              person1.keepPet(cat,"fish");
          }
      }
      

final

  • final
    • Method: Indicates that the method is final and cannot be overridden
    • Class: Indicates that the class is a final class and cannot be inherited
    • Variables: called constants, can only be assigned once
      • The final modified variable is a basic type: the data value stored in the variable cannot be changed
      • The variable modified by final is a reference type: the address value stored in the variable cannot be changed, but the object can be changed internally.
  • Case: Safer

Permission Modifier