Java Abstract Class and Method: Definition and Examples
Table of Content:
Abstraction is the process of separating ideas from their action. A class that is declared with abstract keyword, is known as abstract class in java. also if a class contain any abstract method then the class is declared as abstract class. It can have abstract and non-abstract methods (method with body).
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user. for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send.
Likewise in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be visible to the user. Abstraction lets you focus on what the object does instead of how it does it.i.e the user will have the information on what the object does instead of how it does it.
Ways to achieve Abstaction
Here are two ways to achieve abstraction in java
- Abstract class (partial abstraction)
- Interface (full abstraction)
Abstract class in Java
A class that is declared as abstract is known as abstract class.
-
Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
-
But, if a class contain at least one abstract method, then the class must be declared abstract.
-
If a class is declared abstract, it cannot be instantiated.
-
To utilize an abstract class, you have to inherit it from another class, provide implementations for the abstract methods in it.
It needs to be extended and its method implemented. -
If you inherit an abstract class, you have to provide implementations for all the abstract methods in it.
Syntax:
abstract class class_name { }
Abstract method
Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass.Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the super class.
-
The abstract keyword is used to declare the method as abstract.
-
You have to mention the abstract keyword before the method name in the method declaration.
-
An abstract method has a method signature, but no method body.
-
Instead of curly braces, an abstract method will have a semicolon (;) at the end.
Syntax:
abstract return_type function_name (); // No definition
Example of Abstract class
Program:abstract class ClassA { abstract void callme(); } class ClassB extends ClassA { void callme() { System.out.println("this is call me inside child."); } public static void main(String[] args) { ClassB b = new ClassB(); b.callme(); } }Output:
this is call me inside child. Press any key to continue . . .
Abstract class with concrete(normal) method.
Program:Abstract classes can also have normal methods with definitions, along with abstract methods.
abstract class ClassA { abstract void callme(); public void normal() { System.out.println("this is concrete method"); } } class ClassB extends ClassA { void callme() { System.out.println("this is callme."); } public static void main(String[] args) { ClassB b = new ClassB(); b.callme(); b.normal(); } }Output:
this is callme. this is concrete method Press any key to continue . . .
Interesting Points about Abstract Classes
The following points about abstract classes are worth noting:
? An abstract method cannot be contained in a nonabstract class. If a subclass of an
abstract superclass does not implement all the abstract methods, the subclass must be
defined as abstract. In other words, in a nonabstract subclass extended from an
abstract class, all the abstract methods must be implemented. Also, note that abstract
methods are nonstatic.
? An abstract class cannot be instantiated using the new
operator, but you can still
define its constructors, which are invoked in the constructors of its subclasses. For
instance, the constructors of GeometricObject
are invoked in the Circle
class
and the Rectangle
class.
? A class that contains abstract methods must be abstract. However, it is possible to
define an abstract class that doesn’t contain any abstract methods. In this case, you
cannot create instances of the class using the new operator. This class is used as a
base class for defining subclasses.
? A subclass can be abstract even if its superclass is concrete. For example, the Object
class is concrete, but its subclasses, such as GeometricObject
, may be abstract.
? A subclass can override a method from its superclass to define it as abstract. This is
very unusual, but it is useful when the implementation of the method in the superclass
becomes invalid in the subclass. In this case, the subclass must be defined as abstract.
? You cannot create an instance from an abstract class using the new
operator, but an
abstract class can be used as a data type. Therefore, the following statement, which
creates an array whose elements are of the GeometricObject
type, is correct.
GeometricObject[] objects = new GeometricObject[10];
You can then create an instance of GeometricObject and assign its reference to
the array like this:
objects[0] = new Circle();