Java Object Orientation (Inheritance) (Day 18)

The concept of inheritance:

The same attributes and methods in multiple classes are extracted and put into a new class (parent class)

There is no need to repeatedly define these properties and methods in multiple classes. You only need to write unique properties and methods in your own class. Use the extends keyword to inherit the parent class to use the properties and methods in the parent class.

Inheritance is one of the important means to achieve code reuse in Java. Only single-root inheritance is supported in Java, that is, a class can only have one direct parent class

How to use inheritance:

Syntax: (parent class)

[access modifier] class Pet {
// public properties and methods
}

Write a subclass, inherit the parent class

[access modifier] class Dog extends Pet {
//Subclass-specific properties and methods

 

Subclass accesses parent class members

Access the parent class constructor:
super();    
super(name);

Access parent class properties:
super.name;

access parent class method
super.print();

(1) Use the super keyword, super represents the parent class object
(2) Called in the subclass constructor and must be the first sentence
(3) Cannot access properties and methods defined as private in the parent class

super keyword

The super keyword to access the members of the superclass:
1) super can only appear in methods and constructors of subclasses
2) When super calls the constructor, it can only be the first sentence
3) super cannot access the private members of the parent class

Invocation of constructors under inheritance conditions

◆Calling rules of constructors under inheritance conditions:

The subclass constructor does not explicitly call the superclass's parameterized constructor through super
The method does not explicitly call its own other constructors through this
The system calls the no-argument constructor of the parent class by default

■The subclass constructor explicitly calls the parameterized constructor of the parent class through super.
Execute the corresponding constructor of the parent class instead of the no-argument constructor of the parent class

The subclass constructor explicitly calls its own other constructors through this,
Apply the above two rules in the corresponding construction method

Which resources can a subclass inherit from the parent class:

1) public modified resources,
2) You can inherit resources modified by default modifiers in the same package as subclasses
3) You can inherit protected modified resources, regardless of whether the parent class and the child class are in the same package

4) Cannot inherit resources that are modified with private in the parent class
5) Cannot inherit the constructor of the parent class

When to use inheritance?

■Inheritance is similar to the real world
●As long as "cats are mammals", many attributes and behaviors of cats are
Self-explanatory
The Tibetan Mastiff is a dog
(The design that conforms to the is-a relationship uses inheritance)
Inheritance is a way of code reuse
(puts properties and behaviors common to subclasses into parent class)

What are the benefits of inheritance:

It is convenient to modify the code and reduce the amount of code

Code:

father

package Exercises;

public class Pet {	
	//Define properties and encapsulate them with the private modifier
	private String name;//name
	private int health;//health value
	private int love;//intimacy
	
	//No-argument constructor
	public Pet() {
	}

	//Argument constructor
	public Pet(String name, int health, int love) {
		super();
		this.name = name;
		this.health = health;
		this.love = love;
	}

	//get, set method
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}

	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
	
	//Define method to output object information
	public void printInfo(){
		System.out.println("Pet Nickname:"+this.getName()+" "+"Health value:"+this.getHealth()+" "+"Intimacy:"+this.getLove());
	}
	
}

Cat subclass 1

package Exercises;

public class Cat extends Pet {
	
	//Define the unique properties of the Cat class
	private String color;

	public Cat() {
		super();//Call the no-argument constructor in the parent class Pet class
	}

	public Cat(String name, int health, int love, String color) {
		super(name, health, love);
		this.color = color;
	}

	
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
}

Dog subclass 2

package Exercises;

public class Dog extends Pet{
	//strain is a unique attribute of the Dog class, other classes do not have it, so it is defined in the Dog class
	private String strain;

	//No-argument constructor
	public Dog() {
	}

	//Argument constructor
	public Dog(String name, int health, int love, String strain) {
		super(name, health, love);
		this.strain = strain;
	}

	//get, set method
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}	
}

Penguin Subclass 3

package Exercises;

public class Penguin extends Pet {

	//Define properties unique to the Penguin class
	private String sex;//gender

	public Penguin() {
		super();//super represents the parent class Pet object, so this place means calling the no-argument constructor of the parent class
	}

	//Argument constructor
	public Penguin(String name, int health, int love, String sex) {
		super(name, health, love);//Indicates that the parameterized constructor of the parent class Pet class is called
		this.sex = sex;
	}

	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void aa(){
		System.out.println("Pet Nickname:"+getName()+" "+" Health value:"+getHealth()+" "+" Intimacy:"+getLove()+" gender:"+getSex());
	}	
}

object

package Exercises;

public class Test {

	public static void main(String[] args) {
		// Create Dog class object
		Dog dog1=new Dog("little black", 89, 78, "Husky");
		//There is no setName( ) method in the Dog class, but the DOg class object dog1 can be called, because the Dog class inherits the Pet class, so dog1 can call the setName( ) method in the parent class Pet class
		dog1.setName("two fools");
		dog1.printInfo(); 
		
		//Create Penguin class object
		Penguin Pen1=new Penguin("Q pet",78,98,"male");
		Pen1.setHealth(100);
		Pen1.setName("penguin");
		Pen1.aa();
		
		//Create a Cat class object
		Cat cat1=new Cat("tom", 99, 97, "White");
		cat1.printInfo();
		
		
	}

}

Tags: Java MyEclipse programming language

Posted by simcoweb on Fri, 23 Sep 2022 21:49:37 +0530