java object-oriented features (inheritance)

I package

Before understanding inheritance, let's first understand the use of package. Package is the logical organization form of class. The package in which the class is located can be declared in the program. The class name in the same package cannot be repeated. The access right of class can be restricted through package. In addition, package has hierarchy, that is, a package can contain several sub packages.

Custom package

In addition to being stored in various system packages in the system provided by Java for program development, developers can also create custom packages. If there is no package declared in the program, the class will be stored in the default package. The default package has no name, so it is not recommended to use the form of default package. It is recommended that developers create user-defined packages in the program according to business needs, The syntax of the declaration package is as follows:

package < Package name >

Note: the statement declaring a package must be written on the first line of the class

Import of packages

If you want to use the package existing in Java, you can use the import statement to import the package in the program. The format of importing the package in the program is as follows:

import < Package name >.< Class name >

If you want to import multiple classes in a package, you can use "*" to represent all classes in the package, for example:

import java.util.*;   //Import Java All classes in util package
import java.util.ArrayList;   //Import Java ArrayList class in util package

Access rights for packages

Members with default access rights in a package can only be referenced in the same package. If the access rights of members in a package are public, these members can be referenced by classes in other packages.

1.Public access permission members (classes, attributes and methods collectively). Public members can be accessed by classes in other packages, and protected members in public classes can be accessed by clauses derived from them in other packages.
2. Default access permission members. Default access permission members can only be accessed by classes in the same package, not by classes in other packages.
For example, if the access modifier public of the clear() method of the following Tree class is removed, an error prompt will be displayed in the Hero class

inherit

Inheriting one of the three object-oriented features of Java is an important means to realize program code reuse. Inheritance is to derive new classes from existing classes. The new classes contain the data properties and behaviors of existing classes and can expand new capabilities. The rational use of inheritance can improve the importance of code

Overview of inheritance

The inheritance of Java is implemented using the extends keyword. The class that implements the inheritance is called a subclass, and the inherited class is called a parent class, which is also called a base class. The relationship between the parent class and the subclass is general and special, such as the relationship between fruit and apple. Apple inherits fruit, apple is a subclass of water fruit, and apple is a special fruit.
The inheritance syntax format of Java is as follows:

[ Modifier  ] class Subclass name [extends Parent class name] {
    // Class definition part
}

Note: subclasses can inherit member variables and methods with access permissions of public, protected and default in the parent class

Implementation of inheritance

Practice: create a vehicle parent class and; Two sub categories, Car and Truck, display all kinds of relevant information.
(1) Create a parent class named vehicles. This class has two String attributes, brand and color. This class also contains a run method, which outputs the brand of the car + "driving", and displays the car trademark and color on the console by calling the showinfo() method of this class. Initialize its member properties through the class's parameterized constructor.
(2) Create a car class (Car) and inherit the Vehicles class. Add int member attribute seats and showCar() methods in this class. This method is used to display the information of the car on the console.
(3) Write a truck class to inherit the Vehicles class, and add the float member properties load and showtruck0. This method is used to display the information of the truck on the console.
(4) Create a test class VehiclesTest and complete the output of relevant information in the requirements in the main method of this class.
(5) Create com java. Oriented, save the above classes to the package.
The code is as follows:

package com.java.oriented;

public class Vehicles {
	 String brand;
	    String color;
	    public Vehicles(String brand,String color){
	    	this.brand=brand;
	    	this.color=color;
	    }
	 public void run() {
	    	System.out.println("I"+this.brand+"Driving");
	    }
	    public void showinfo() {
	    	System.out.println("brand:"+brand+"\t"+"colour:"+this.color);
	    }	   
}
package com.java.oriented;

public class Car extends Vehicles{

	public Car(String brand, String color) {
		super(brand, color);
		
	}
	public void showCar(int seats) {
		super.showinfo();
		System.out.println("I am a:"+this.brand+"\t My color:"+this.color+"\t I have:"+seats+"Seats");
	}
}
package com.java.oriented;

public class Truck extends Vehicles {

	public Truck(String brand, String color) {
		super(brand, color);
		
	}
	public void showTruck(float load) {
		super.showinfo();
		System.out.println("I am a:"+this.brand+"\t My color:"+this.color+"\t My load:"+load+"Tons of material");
	}

}
package com.java.oriented;

public class Vehiclestest {

	public static void main(String[] args) {
		Car c= new Car("Aston Martin", "green");
		c.showCar(6);
		Truck t= new Truck("Dongfeng Xiaokang","blue");
		t.showTruck((float) 12.0);

	}

}

The operation results are as follows:

Note: subclasses cannot inherit private members of the parent class, and in actual development, it is not recommended that subclasses inherit the properties of the parent class, because subclasses inherit the properties of the parent class will break the encapsulation of the parent class, and Java inheritance is single inheritance, that is, a subclass can only have one direct parent class.

Keywords: Java

Added by mobtex on Sun, 30 Jan 2022 18:15:51 +0200