Interfaces and Abstract classes
abstract / abstract class
abstraction
Abstraction refers to the act of representing essential features without including the background details or explanations.
abstract
- A Java keyword used in a class definition to specify that a class is not to be instantiated, but rather inherited by other classes. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
- They are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. If even a single method is abstract, the whole class must be declared abstract.
- An abstract class can have abstract methods that are not implemented in the abstract class, but in subclasses.
- You can’t mark a class as both abstract and final.
abstract class
A class that contains one or more abstract methods, and therefore can never be instantiated. Abstract classes are defined so that other classes can extend them and make them concrete by implementing the abstract methods. There can be an abstract class without abstract methods.
abstract method
A method that has no implementation. other nonabstract methods can access a method that you declare as abstract.
instantiate abstract class
An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).
difference between abstraction and encapsulation
- Abstraction focuses on the outside view of an object (i.e. the interface).
- Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.
- Abstraction solves the problem in the design side while Encapsulation is the Implementation.
- Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.
abstract model / conceptual model / conceptual schema
In the most general sense, a model is anything used in any way to represent anything else. Some models are physical objects, for instance, a toy model which may be assembled, and may even be made to work like the object it represents. They are used to help us know and understand the subject matter they represent. The term conceptual model may be used to refer to models which are represented by concepts or related concepts which are formed after a conceptualization process in the mind. Conceptualization from observation of physical existence and conceptual modeling are the necessary means human employ to think and solve problems.
interface
A Java keyword used to define a collection of method definitions and constant values. It can later be implemented by classes that define this interface with the implements
keyword.
An interface is a description of a set of methods that conforming implementing classes must have.
Note:
- You can’t mark an interface as final.
- Interface variables must be static.
- An Interface cannot extend anything but another interfaces
instantiate an interface
You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.
create an object for an interface
Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.
Do interfaces have member variables?
Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.
What modifiers are allowed for methods in an Interface?
Only public and abstract modifiers are allowed for methods in interfaces.
Difference between abstract class and interface
When to use an abstract class
- An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
- An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
- If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
- If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new interface.
- Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, we cannot change it; if they use an abstract class, we can still add behavior without breaking the existing code.
- If we want to provide common, implemented functionality among all implementations of our component, use an abstract class. Abstract classes allow us to partially implement our class, whereas interfaces contain no implementation for any members.
- If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
- When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
- Abstract classes are an excellent way to create planned inheritance hierarchies. They’re also a good choice for nonleaf classes in class hierarchies.
When to use an interface
- If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.
- Interfaces are a good choice when we think that the API will not change for a while.
- Interfaces are also good when we want to have something similar to multiple inheritances since we can implement multiple interfaces.
- If we are designing small, concise bits of functionality, use interfaces. If we are designing large functional units, use an abstract class.
- You see that something in your design will change frequently.
- If various implementations only share method signatures then it is better to use Interfaces.
- you need some classes to use some methods which you don’t want to be included in the class, then you go for the interface, which makes it easy to just implement and make use of the methods defined in the interface.
Research
TODO https://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java