Abstract Classes - To implement Abstraction we use abstract keyword and define
abstract classes
It is used only for classes and methods and not for data variables
- Abstract classes cannot be instantiated
- Abstract methods do not have a body - meaning, there is no implementation in the
abstract method, derived classes will have to implement the abstract method
- Abstract classes can have both abstract and concrete methods, concrete method has
an implementation defined
- Abstract classes can have constructors
- Abstract classes can contain instance variables
- Abstract classes can implement interfaces
Abstract methods definition
abstract return-type method-name(parameter-list) throws exception;
- Any class that contains one or more abstract methods must also be declared
abstract
- The following are various illegal combinations of other modifiers for methods
with respect to abstract modifiers:
final
abstract native
abstract synchronized
abstract static
abstract private
abstract strict
- When we want to use abstract classes, we are forced to provide implementation for
abstract methods in the abstract class
- When we provide implementation in the derived classes, we are essentially
overriding the method from parent class
Advantages of Abstract Keywords
- Provides a way to define a common interface - By defining common methods and
properties, abstract classes provide a way to enforce consistency and
maintainability across an application.
- Enables polymorphism - enforces method overriding
- Encourages code reuse - overriding provides the implementation
- Provides a way to enforce implementation - again overriding
- Enables late binding - late binding is runtime polymorphism based on the type of
the object calling
Abstract Class having constructor, data member, and methods
Elements abstract class can have -
data member
abstract method
method body (non-abstract method)
constructor
main() method.
Properties of Abstract class -
- an abstract class cannot be created, we can have references to abstract class
type though
- an abstract class can contain constructors in Java. And a constructor of an
abstract class is called when an instance of an inherited class is created
- In Java, we can have an abstract class without any abstract method. This allows
us to create classes that cannot be instantiated but can only be inherited.
- Abstract classes can also have final methods (methods that cannot be overridden)
- Similar to the interface we can define static methods in an abstract class that
can be called independently without an object.
- We can use the abstract keyword for declaring top-level classes (Outer class) as
well as inner classes as abstract
- If the Child class is unable to provide implementation to all abstract methods of
the Parent class then we should declare that Child class as abstract so that the
next level Child class should provide implementation to the remaining abstract
method.
Difference between Abstract class and interface
- class can inherit from only one abstract class but A class can implement multiple
interfaces.
- An interface is a reference type in Java, it is similar to a class, and it is a
collection of abstract methods and static constants.
In Java, data hiding is implemented through getter and setter methods and private
variables
Getter is used to accessing the private data and setter is used to modify the
private data only after authentication
Data Hiding is hiding internal data from outside users. The internal data should
not go directly that is outside person/classes is not able to access internal data
directly. It is achieved by using an access specifier- a private modifier.