Interface and abstract class usage examples in Java

  • 2020-04-01 04:33:17
  • OfStack

This article illustrates the use of interfaces and abstract classes in Java. Share with you for your reference, as follows:

In the concept of object orientation, we know that all objects are depicted by classes, but not all classes are used to depict objects, if a class does not contain enough information to depict a concrete object, such a class is an abstract class.

Abstract classes are often used to represent the abstract concepts that we derive from our analysis and design of the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.

Such as: we want to describe the "fruit", it is an abstract, it has the quality and volume of some common fruits (quality), but the lack of features (apples and oranges are all fruit, they have their own features), we can get not only a kind of representative of the fruit (apple, orange can represent fruits), can be described by an abstract class, it so abstract class cannot be instantiated. When we use a class to describe "apple" in detail, the class can inherit from the abstract class that describes "fruit," which we all know is a "fruit."

In the object-oriented world, abstract classes are mainly used for type hiding. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can be implemented in any concrete way possible. The abstract description is the abstract class, and the set of any possible concrete implementations is represented as all the derived classes of the abstract class.

All abstract methods in interfaces and abstract classes cannot have concrete implementations, but all abstract methods should be implemented in their subclasses (with function bodies, even if {} is empty). Java designers may consider the flexibility of abstract methods, and each subclass can implement abstract methods according to its own needs.

Abstract class is defined as follows:


public abstract class AbstractClass //There is at least one abstract method
{
  public int t; //Normal data member
  public abstract void method1(); //A subclass of an abstract class must implement an abstract method in an abstract class
  public abstract void method2();
  public void method3(); //Nonabstract method
  public int method4();
  publi int method4 (){
     ...  // Can be assigned in an abstract class Nonabstract method The default behavior of a method is its concrete implementation 
  }
  public void method3(){
     ...  // Can be assigned in an abstract class Nonabstract method The default behavior of a method is its concrete implementation 
  } 
}

Interface is defined as follows:


public interface Interface
{
  static final int i; //Interface can not have ordinary data members, only can have static data members that can not be modified,static means global, final means can not be modified, can not be modified without static final modification, will be implicitly declared as static and final
  public void method1(); //Methods in an interface must be abstract methods, so you don't need to abstract them
  public void method2(); //You cannot give a method its default behavior in an interface, that is, you cannot have a concrete implementation of a method
}

In short, an abstract class is an incomplete class, and an interface is simply a collection of abstract method declarations and static data that cannot be modified, neither of which can be instantiated.

In a sense, an interface is a special form of abstract class. In the Java language, abstract class represents an inheritance relationship. A class can inherit only one abstract class, while a class can implement multiple interfaces. In many cases, an interface can indeed replace an abstract class, if you don't want to deliberately express inheritance on properties.

To further understand, about the purpose of introducing abstract classes and interfaces into Java, I consulted the master and got the answer as follows:

1, from the class hierarchy, the abstract class is at the top of the hierarchy, but in the actual design, the abstract class should generally appear later. Why is that? In fact, the acquisition of abstract classes is a bit like the extraction of the common factor in mathematics: ax+bx, x is the abstract class, if you don't have the previous formula, how do you know if x is a common factor? In this respect, it also conforms to the process of people's understanding of the world, first concrete and then abstract. So if you get a lot of concrete concepts in the design process and you find commonalities among them, it should be true that the collection of commonalities is an abstract class.

On the surface, interface looks like an abstract class, but the usage is completely different. Its basic function is to put together a collection of unrelated classes (concepts) to form a new, centrally-operable "new class". A typical example I give to students is "driver". Who can be the driver? Anyone can get a driver's license. So I don't care if you are a student, white collar, blue collar or boss, as long as you have a license is the driver.


interface DriverLicence { 
Licence getLicence(); 
}
class StudentDriver extends Student implements DriverLicence { 
} 
class WhtieCollarEmployeeDriver extends WhtieCollarEmployee implements DriverLicence { 
} 
class BlueCollarEmployeeDriver extends BlueCollarEmployee implements DriverLicence { 
} 
class BossDriver extends Boss implements Driver { 
}

When I have defined the "car" class, I can specify the "driver".


class Car {
setDriver(DriverLicence driver);
}

At this point, the Car object doesn't care what the driver does; the only thing they have in common is a driver license. This should be the most powerful part of the interface that abstract classes can't match.

Conclusion:

Abstract classes extract common factors from concrete classes, while interfaces are designed to "hash" unrelated classes into a common group. Usually we usually form a good habit is multi-purpose interface, after all, Java is a single inheritance, unlike C++, but in the need to use abstract classes must still be used (a little similar to the use of goto), ha ha.

I hope this article has been helpful to you in Java programming.


Related articles: