Java - Abstraction
Java - Abstraction
Java - Abstraction
As per dictionary, abstraction is the quality of dealing with ideas rather than events. For
example, when you consider the case of e-mail, complex details such as what happens as soon
as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore,
to send an e-mail you just need to type the content, mention the address of the receiver, and
click send.
Abstract Class
A class which contains the abstract keyword in its declaration 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 has at least one abstract method, then the class must be declared
abstract.
If you inherit an abstract class, you have to provide implementations to all the abstract
methods in it.
Example
This section provides you an example of the abstract class. To create an abstract class, just use
the abstract keyword before the class keyword, in the class declaration.
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
thi b b
https://www.tutorialspoint.com/java/java_abstraction.htm 1/7
15/04/2022, 16:17 Java - Abstraction
this.number = number;
return 0.0;
return name;
return address;
address = newAddress;
return number;
You can observe that except abstract methods the Employee class is same as normal class in
Java. The class is now abstract, but it still has three fields, seven methods, and one constructor.
Now you can try to instantiate the Employee class in the following way −
e.mailCheck();
https://www.tutorialspoint.com/java/java_abstraction.htm 2/7
15/04/2022, 16:17 Java - Abstraction
When you compile the above class, it gives you the following error −
1 error
We can inherit the properties of Employee class just like concrete class in the following way −
Example
setSalary(salary);
return salary;
salary = newSalary;
return salary/52;
https://www.tutorialspoint.com/java/java_abstraction.htm 3/7
15/04/2022, 16:17 Java - Abstraction
Here, you cannot instantiate the Employee class, but you can instantiate the Salary Class, and
using this instance you can access all the three fields and seven methods of Employee class as
shown below.
s.mailCheck();
e.mailCheck();
Output
Constructing an Employee
Constructing an Employee
Abstract Methods
If you want a class to contain a particular method but you want the actual implementation of that
method to be determined by child classes, you can declare the method in the parent class as an
abstract.
You have to place the abstract keyword before the method name in the method
declaration.
Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
https://www.tutorialspoint.com/java/java_abstraction.htm 4/7
15/04/2022, 16:17 Java - Abstraction
Example
Any class inheriting the current class must either override the abstract method or
declare itself as abstract.
Note − Eventually, a descendant class has to implement the abstract method; otherwise, you
would have a hierarchy of abstract classes that cannot be instantiated.
Suppose Salary class inherits the Employee class, then it should implement the computePay()
method as shown below −
return salary/52;
16 Lectures 2 hours
Malhar Lathkar
https://www.tutorialspoint.com/java/java_abstraction.htm 5/7
15/04/2022, 16:17 Java - Abstraction
More Detail
Video
19 Lectures 5 hours
Malhar Lathkar
More Detail
Video
More Detail
Video
Tushar Kale
More Detail
https://www.tutorialspoint.com/java/java_abstraction.htm 6/7
15/04/2022, 16:17 Java - Abstraction
Video
More Detail
Video
76 Lectures 7 hours
Arnab Chakraborty
More Detail
https://www.tutorialspoint.com/java/java_abstraction.htm 7/7