JAVA object-oriented inheritance and polymorphism

Today, let's learn about JAVA's object-oriented inheritance and polymorphism

What is inheritance?

It means that our parents can inherit the property, family name and house of our children······

In java, the keyword used for inheritance is extends.
Subclasses can inherit the properties and methods of the parent class.
In Java, inheritance can only be single inheritance, so a subclass can only inherit one parent class. However, a parent class can have multiple subclasses.
Why use inheritance?

The reuse of code is realized, and the same code does not need to be written repeatedly.
Subclasses inherit from the parent class and can directly enjoy the encapsulated properties and methods in the parent class without redevelopment.
Subclasses can encapsulate their own unique attributes and methods according to their own attributes.

How to use? (format)

public class Fu{
//Attributes
//Method
}

//extends indicates inheritance
public class Zi extends Fu{
//Attributes
//Method
}

Specific learning through cases

1. Create a parent class with attribute name and age. Have the show() method to display your own information.
2. Create subclasses, inherit the properties and methods of the parent class, and have new properties.
3. Create a test class to test.
Inheritance example
Declare a Person class, privatize the attributes name and age, and encapsulate the fields;
Define a talk() method in the Person class to print name and age information;
Declare and instantiate a Person object p in the main method, assign a value to the attribute in p, and call the talk() method to print. I am Zhang San, 18 years old.
Test description
Test input:
I'm Zhang San. I'm 18 years old

/********* begin /
//Declare and instantiate a Person object p
Person p =new Person();
//Assign values to attributes in p
p.setName("Zhang San");
p.setAge(18);
//Call the talk() method in the Person class
p.talk();
/ end *********/

}

}

//Define the Person class here
class Person {
/********* begin *********/
private String name;
private int age;
public void talk(){
System.out.println("I am:" + name + ", this year:" + 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;
 }
/********* end ******

Member access characteristics in polymorphism
Member access characteristics

Member variable

Compile to see the parent class, run to see the parent class

Member method

Compile to see the parent class, run to see the child class

Code demonstration

class Fu {
int num = 10;

public void method(){
    System.out.println("Fu.. method");
}

}

class Zi extends Fu {
int num = 20;

public void method(){
    System.out.println("Zi.. method");
}

}

public class Test2Polymorpic {
/*
Polymorphic member access characteristics:

            Member variable: Look at the left (Parent class), Run and look to the left (Parent class)

            Member method: Look at the left (Parent class), Run and look to the right (Subclass)
 */
public static void main(String[] args) {
    Fu f = new Zi();
    System.out.println(f.num);
    f.method();
}

}

Polymorphic examples
(polymorphic) define Juicer JuiceMachine has a juicing method, makeJuice, which is introduced into the corresponding fruit.
If the input is Apple, the output is "Apple juice flowing out"
The Orange output "Orange juice" is passed in
The input is Banana output "outflow Banana sauce"

abstract class Fruit{
abstract void makeJuice();
}
class JuiceMachine{
public static void makeJuice(Fruit f){
f.makeJuice();
}
}

class Apple extends Fruit {

@Override
public void makeJuice() {
	// TODO Auto-generated method stub
	System.out.println("Outflow of apple juice");
}

}
class Orangle extends Fruit{

@Override
public void makeJuice() {
	// TODO Auto-generated method stub
	System.out.println("Outflow of orange juice");
}

}
class Banana extends Fruit{

@Override
public void makeJuice() {
	// TODO Auto-generated method stub
	System.out.println("Banana jam");
}

}

public class Test1 {

public static void main(String[] args) {
	JuiceMachine.makeJuice(new Apple());
	JuiceMachine.makeJuice(new Orangle());
	JuiceMachine.makeJuice(new Banana());
	
	
}

}

Added by mort on Mon, 07 Feb 2022 10:55:48 +0200