JAVA inheritance of basic classes abstract classes interface

  • 2020-04-01 01:20:14
  • OfStack

Encapsulation: encapsulating properties and methods into a class.
Inheritance: just as a subclass inherits some properties and methods from its parent class.
Polymorphism: just as a parent class has several distinct subclasses.
I'm not going to go into that, but I'm going to focus on one inheritance. Inheritance is a feature of OOP (object orientation), and Java supports only single inheritance (if you inherit two parent classes that have the same method, you don't know which parent class you inherit to, so Java only supports single inheritance). Inheritance is a feature of Java. All the classes we use inherit from the Objict class, so we need methods of the Object class, such as toString(), getClass(), wait()... So all the classes we build have superclasses.

There are generally three types of Java:

Basic classes: also known as generic classes, are templates for objects and collections of properties and methods. You can inherit from other base classes, abstract classes, and implementation interfaces.
Abstract class: a class that has an abstract method (an abstract method is one that must be implemented by inheritance and is defined, not implemented). An abstract class can have one or more abstract methods, which are transitions between the base class and the interface class.
Interface class: commonly called an interface. All methods in this class are abstract methods whose methods are defined but not implemented.
Abstract class and interface are defined by abstract class + abstract class name and interface + interface name. All methods in the interface are abstract methods, while some methods in the abstract class are implemented by themselves, and some are just defined but not implemented.

Definition of basic classes:

 
public class Fruit { 
public void price() { 
System.out.println(" Fruit prices "); 
} 
public void weight() { 
System.out.println(" Fruit weight "); 
} 
} 

Abstract class definition:
 
public abstract class Fruit { 
public void price() { 
System.out.println(" Fruit prices "); 
} 
public abstract void weight(); 
} 

Definition of interface class:
 
public interface Fruit { 
public void price(); 
public void weight() ; 
} 

From the above, we can see that an interface is an updated version of an abstract class. Since the methods of this class are all abstract methods, we can replace abstract with interface. The methods of the interface must be implemented by subclasses.
inheritance

Inherit the basic class, can inherit the methods of the parent class, can also from some, can also be extended. Here are the classes that implement the interface's basic classes:
 
public class Apple extends Fruit { 

} 

There are two methods in the Fruit base class: price() and weight();

Inheriting an abstract class, you must implement the abstract methods of the abstract class, you can modify the methods of the parent class, and you can add methods. Here are the classes that inherit from abstract classes:
 
public class Apple extends Fruit { 
@Override 
public void weight() { 
System.out.println(" Fruit weight "); 
} 
} 

This class inherits the price() method of the Fruit class and implements the weight() method of the Fruit abstract class.
Inheriting the interface class (and thus implementing the interface), all the abstract classes of the interface class must be implemented and added. Here are the classes that inherit the interface:

 
public class Aple implements Fruit { 
@Override 
public void price() { 
System.out.println(" Fruit prices "); 
} 
@Override 
public void weight() { 
System.out.println(" Fruit weight "); 
} 
} 


This class is all the abstract methods that implement the Fruit interface.

Java only supports single inheritance (inheriting basic and abstract classes), but we can implement it with interfaces (multi-inheritance interfaces)
Public class Apple extends Fruit implements Fruit1, Fruit2{}
Typically we extend the base class and the abstract class with the extends keyword, and implement the implements keyword for the inheritance of the interface class. In fact, inheritance is very simple, can be just not clear the two keywords, when we are clear that is relatively simple.

Interfaces can also inherit from interfaces such as public interface Fruit1 extends Fruit {}, which is multiple inheritance in the interface, as well as abstract and basic classes. If we add final modifiers to the base class, we also define that the class is not inherited, and that the class cannot be a parent class. Meanwhile, methods of the basic class can be decorated with public, private, proptected, and final to prevent inheritance of the method.

Here is just their own understanding, some of the improper words, just think that the words are better understanding, please understand. Some places are not clear because they are not the focus here. I hope you can understand them in other ways.

Related articles: