2. Java Abstract Class
The abstract class in Java cannot be instantiated (we cannot create objects of
abstract classes). We use the abstract keyword to declare an abstract class.
For example,
3. An abstract class can have both the regular methods and
abstract methods. For example,
4. Java Abstract Method
A method that doesn't have its body is known as an
abstract method. We use the same abstract keyword to
create abstract methods. For example,
If a class contains an abstract method, then the class
should be declared abstract. Otherwise, it will generate
an error. For example,
5. Java Abstract Class and Method
Though abstract classes cannot be instantiated, we can create
subclasses from it. We can then access members of the abstract
class using the object of the subclass. For example,
6. Implementing Abstract Methods
If the abstract class
includes any abstract
method, then all the child
classes inherited from the
abstract superclass must
provide the
implementation of the
abstract method. For
example,
7. Accesses Constructor of Abstract Classes
An abstract class can have constructors like the regular class. And,
we can access the constructor of an abstract class from the
subclass using the super keyword. For example,
Here, we have used the super() inside
the constructor of Dog to access the
constructor of the Animal.
Note that the super should always be
the first statement of the subclass
constructor.
8. Java Abstraction
• Abstraction is an important concept of
object-oriented programming that
allows us to hide unnecessary details
and only show the needed
information.
• This allows us to manage complexity
by omitting or hiding details with a
simpler, higher-level idea.
9. Key Points to Remember
•We use the abstract keyword to create abstract classes and methods.
•An abstract method doesn't have any implementation (method body).
•A class containing abstract methods should also be abstract.
•We cannot create objects of an abstract class.
•To implement features of an abstract class, we inherit subclasses from it and create
objects of the subclass.
•A subclass must override all abstract methods of an abstract class. However, if the
subclass is declared abstract, it's not mandatory to override abstract methods.
•We can access the static attributes and methods of an abstract class using the
reference of the abstract class. For example,
10. Interface
An interface is a fully abstract class. It includes a group of abstract
methods (methods without a body).
We use the interface keyword to create an interface in Java. For example,
11. Implementing an Interface
Like abstract classes, we cannot create objects of interfaces.
To use an interface, other classes must implement it. We use the implements keyword
to implement an interface.
13. Advantages of Interface in Java
• Similar to abstract classes, interfaces help us to achieve abstraction in Java.
• Interfaces provide specifications that a class (which implements it) must follow.
• Interfaces are also used to achieve multiple inheritance in Java. For example,
14. default methods in Java Interfaces
• If a large number of classes were implementing this interface,
we need to track all these classes and make changes to them.
This is not only tedious but error-prone as well.
• To resolve this, Java introduced default methods. Default
methods are inherited like ordinary methods.
15. private and static Methods in Interface
• The Java 8 also added another feature to include static
methods inside an interface.
• With the release of Java 9, private methods are also supported
in interfaces.
We cannot create objects of an interface.