Java core technology inheritance and polymorphism, overloading and rewriting, abstract classes and interfaces

Posted by fredi_bieging on Fri, 10 May 2019 13:50:02 +0200

Class inheritance

As for the definition of inheritance of classes, we can understand it more or less as the relationship between father and son. Sons will inherit the attributes and methods of father. The father is the father and the son is the child. Their relationship is the inheritance relationship.

Xiaobian here is a Java learning material, directly add my live Java learning group: 985331340 free, you dare to come, I dare to send.

Inheritance is an important concept of object-oriented, its role is enormous, it makes the program architecture flexible, reusing some well-defined classes in the program can reduce the development cycle of the software, but also improve the maintainability of the software.

extends and implements keywords

In the Java language, inheritance can be achieved by using the keywords extends and implements (for inheritance interfaces).

Note that all classes in the JAVA language inherit from java.lang.Object. When a class does not inherit two keywords, it defaults to inherit the object (this class is in the java.lang package, so import) ancestor class is not required.

In Java language, class inheritance is single, that is, entends can only inherit one class. The existence of implements is a disguised implementation of multiple inheritance, that is, a class can inherit multiple interfaces. Interfaces will be mentioned later.

Example code

package java02.day_4_12;
import My_tools.Boundlinetool;
import My_tools.FormatTimetool;
/**
 * @outhor xiaoshe
 * @date 2019/4/12  - @time 21:36
 */
public class sty_extends {
	public
	 static void main(String[] args) {
		father father = new father("The father of the cottage", "Worker");
		son son = new son("Little house", "male", "Programmer");
		System.out.println(father.getName());
		father.work();
		Boundlinetool.Minus();
		System.out.println(son.getName());
		son.work();
	}
}
// Constructing parent class
class father{
	private String name;
	private String work;
	// Member method
	public String getName() {
		return name;
	}
	public String getWork() {
		return work;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setWork(String work) {
		this.work = work;
	}
	//Construction method
	public  father(String name,String work){
		this.work=work;
		this.name=name;
	}
	// New method
	public void work(){
		System.out.println(this.work);
	}
}
// Constructing subclasses
class son extends father{
	// Inheritance of parent classes through extends
	private String sex;
	// Add new attributes.
	// Override the construction method, attributes not only inherit the parent attributes, but also add new attributes.
	public son(String name, String sex,String work) {
		super(name, work);
		this.sex=sex;
	}
	@Override
	    // Call the member method of the parent class directly to get and set the member properties.
	public String getName() {
		return super.getName();
	}
	// Override the work method of the parent class
	@Override
	    public void work(){
		System.out.println("Subclass"+super.getName()+"The work is:");
		// Rewrite content
		super.work();
		// Call the parent method directly.
	}
}

 

Override and Overload

Rewrite

That is, the subclass rewrites the implementation process of the method that allows access to the parent class, and the return value and parameters can not be changed. That is, the shell remains unchanged and the core rewrites! The advantage of rewriting is that subclasses can define their own behavior as needed. That is to say, subclasses can implement the methods of their parent classes according to their needs.

In the inheritance of classes, we know that subclasses can not only inherit the attributes and methods of the parent class, but also define their own attributes and methods, and can also rewrite the methods of the parent class to achieve their own needs. Rewriting plays an important role in object-oriented thinking. It makes the program have very good extensibility. It can be said that all problems can be solved by rewriting. It is a characteristic of inheritance.

// Override the work method of the parent class
@Override
    public void work(){
	System.out.println("Subclass"+super.getName()+"The work is:");
	// Rewrite content
	super.work();
	// Call the parent method directly.
}

heavy load

Overloading is in a class where the method names are the same and the parameters are different. The return types can be the same or different.

That is to say, when constructing methods in classes, we can construct methods with the same method name but different parameters. This is the overload of methods, which is very common in class construction methods. We often use some class construction methods, and we will find that there are usually many methods to construct objects through different types of parameters. This is the overload of methods. Carry.

polymorphic

Polymorphism is the ability of the same behavior to have multiple manifestations or forms. Polymorphism is the same interface, using different instances to perform different operations. In fact, inheritance, rewriting is a manifestation of polymorphism, we can achieve different results through different subclasses rewriting the same method of the parent class.

The characteristics of polymorphism:

  • Eliminate coupling between types
  • Replaceability
  • Extensibility
  • Interfacing
  • flexibility
  • Simplification

The necessary conditions for polymorphism are inheritance, rewriting, and parent reference pointing to subclass objects. When using polymorphism to invoke a method, first check whether there is a method in the parent class, if not, compile errors; if there is, then call the method of the same name of the subclass.

Benefits of polymorphism:

  • It can make the program have good expansion, and can handle all kinds of objects in common.

The most common way to implement polymorphism is by rewriting or inheriting interfaces and then implementing the methods in the interfaces (in fact, by rewriting the methods in the interfaces to achieve different roles).

Abstract classes and interfaces

abstract class

In the concept of object-oriented, all objects are described by classes, but conversely, not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such classes are abstract classes.
  • Except that abstract classes cannot instantiate objects, other functions of classes still exist. Membership variables, member methods and constructors are accessed in the same way as ordinary classes.
  • Because abstract classes cannot instantiate objects, abstract classes must be inherited in order to be used. It is also for this reason that abstract classes are usually decided at the design stage.
  • Parent classes contain common methods for collections of subclasses, but they cannot be used because the parent class itself is abstract.
  • In Java, abstract classes represent an inheritance relationship. A class can inherit only one abstract class, while a class can implement multiple interfaces.

Example code

package java02.day_4_12;
/**
 * @outhor xiaoshe
 * @date 2019/4/13  - @time 0:55
 * abstract class
 */
public class sty_Polymorphism {
	public static void main(String[] args) {
		woman woman = new woman("female", "yellow");
		// Although an abstract class cannot be instantiated, its subclass objects can be instantiated.
		System.out.println(woman.getSex());
		// The method allowed by the abstract class can be invoked directly.
		System.out.println( woman.getColor());
		// The method allowed by the abstract class can be invoked directly.
	}
}
// Definition of abstract human class
abstract class humanity{
	// The abstact keyword is used to define abstract classes
	private String sex;
	private String color;
	public String getSex() {
		return sex;
	}
	public String getColor() {
		return color;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setColor(String color) {
		this.color = color;
	}
	humanity(String sex,String color){
		//Construction method
		this.color=color;
		this.sex=sex;
	}
	@Override
	    public String toString() {
		//Rewrite toString method
		return("Gender:"+this.sex+"Skin colour:"+this.color);
	}
}
class woman extends humanity{
	// Inheritance Abstract class. woman class can be used as an object of humanity class, which is the upward transition.
	woman(String sex, String color) {
		// There must be a construction method.
		super(sex, color);
	}
}

Interface

Interface, an abstract type in JAVA programming language, is a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract method of the interface by inheriting the interface.
  • It should be noted that interfaces are not classes. The way they are written is similar to classes, but they belong to different concepts. Classes describe the attributes and methods of objects. The interface contains the methods to be implemented by the class.
  • Unless the class implementing the interface is an abstract class, all methods in the interface are defined when the class inherits the interface.
  • Interfaces can also not be instantiated, but can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class.

The difference between interfaces and classes:

  • Interfaces cannot be used to instantiate objects.
  • There is no construction method for the interface.
  • All methods in the interface must be abstract methods.
  • Interfaces cannot contain member variables, except static and final variables.
  • Interfaces are not inherited by classes, but are implemented by classes.
  • Interface supports multiple inheritance.

Characteristics of the interface:

  • Each method in the interface is also implicitly abstract, and the method in the interface is implicitly specified as public abstract.
  • The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables
  • The method in the interface can not be implemented in the interface, but can only be implemented by the class that implements the interface.

The difference between abstract classes and interfaces:

  • Methods in abstract classes can have method bodies, that is, they can realize the specific functions of methods, but methods in interfaces can not.
  • Membership variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type.
  • Interfaces should not contain static code blocks and static methods (methods modified with static), while abstract classes can have static code blocks and static methods.
  • A class can inherit only one abstract class, while a class can implement multiple interfaces.

On Abstract methods:

  • The abstract method is only the method name, but there is no method body to implement the method. It needs to rewrite the method body to implement the method body in the implementation class.
public void work(String work);

Definition of interface:

  • Interfaces similar to id definitions are defined by the inteface keyword.
interface man{
	public void eat();
	public void work();
	public void play();
}

Interface is abstract by default, so it can't be instantiated. The use of interface must be inherited, and all methods must be defined and implemented.

Example code

package java02.day_4_12;
/**
 * @outhor xiaoshe
 * @date 2019/4/13  - @time 1:35
 */
public class sty_interface {
	public static void main(String[] args) {
		// Then its method can be invoked by instantiating the object of the implementation class of the interface.
		achieveman achieveman = new achieveman();
		achieveman.eat();
		achieveman.work();
	}
}
// Define a man interface
interface man{
	public void eat();
	// Abstract method
	public void work();
}
class achieveman implements man {
	// After inheriting the interface, all implementations of methods within the interface must be implemented
	@Override
	    public void eat() {
		System.out.println("Men eat");
	}
	@Override
	    public void work() {
		System.out.println("Men work");
	}
}

Topics: Java less shell Programming