Abstract Classes and Interfaces for Java Object Oriented Fundamentals

  • 2021-12-11 07:35:39
  • OfStack

Abstract class (abstract):

An abstract class cannot create an instance, it can only be inherited as a parent class. Abstract class is a parent class abstracted from multiple concrete classes, which has a higher level of abstraction. An abstract class is abstracted from many classes with the same characteristics, and this abstract class is used as the template of its subclass, thus avoiding the randomness of subclasses.

(1) Abstract methods are only declarations and do not contain implementations, so they can be regarded as virtual methods without implementation bodies

(2) Abstract classes cannot be instantiated

(3) An abstract class can, but does not have to, have abstract properties and abstract methods, but once it has abstract methods, it must be declared as an abstract class

(4) The concrete derived class must override the abstract methods of the base class

(5) An abstract derived class can override or not override the abstract methods of the base class. If not, their specific derived classes must override them

Interface (interface):

(1) Interface cannot be instantiated

(2) Interfaces can only contain method declarations

(3) The members of an interface include methods, properties, indexers, events

(4) The interface cannot contain constants, fields (fields), constructors, destructors, static members

Differences between abstract classes and interfaces:

(1) Abstract classes can have constructors, but interfaces cannot have constructors.

(2) There can be ordinary member variables in abstract classes, but no ordinary member variables in interfaces

(3) Static methods can be included in abstract classes, but not in interfaces

(4) A class can implement multiple interfaces, but it can only inherit one abstract class.

(5) Interfaces can be implemented multiple times, and abstract classes can only be inherited by single 1

(6) If an abstract class implements an interface, the method in the interface can be mapped to the abstract class as an abstract method without having to implement it, and the method in the interface can be implemented in a subclass of the abstract class

Similarities between abstract classes and interfaces:

(1) All can be inherited

(2) Neither can be instantiated

(3) Both can include method declarations

(4) Derived classes must implement unimplemented methods

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: