Java Abstract Class
Java Abstract Class
For example,
An abstract class can have both the regular methods and abstract
methods.
For example,
// abstract method
abstract void method1();
// regular method
void method2() {
System.out.println("This is regular method");
}
}
// error
// class should be abstract
class Language {
// abstract method
abstract void method1();
}
Example: Java Abstract Class and Method
Output
obj.display();
Here, obj is the object of the child class Main. We are calling the
method of the abstract class using the object obj.
class Main {
public static void main(String[] args) {
Bark bark
I can eat.
We have inherited a subclass Dog from the superclass Animal. Here, the
subclass Dog provides the implementation for the abstract method
makeSound().
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. Visit Java super keyword to learn more.
Java Abstraction
The major advantage of hiding the working of the brake is that now the
manufacturer can implement brake differently for different motorbikes,
however, what brake does will be the same.
Example 3:
Java Abstraction
MountainBike Brake
SportsBike Brake
Rule2:
If a class has an abstract method it should be declared as abstract by
using the keyword abstract else it leads to CE: class is not abstract and
does not override the abstract method in the class.
public class Example
{
abstract void m1();
}
CE: Example is not abstract and does not override abstract method
m1() in Example. The correct syntax is given below.
abstract class Example
{
abstract void m1();
}
Rule3:
If a class is declared as abstract it cannot be instantiated violation leads
to compile-time Error.
abstract class Example
{
abstract void m1();
public static void main(String args[])
{
Example e = new Example();
}
}
CE: Example is abstract, cannot be instantiated
Rule4:
The subclass of an abstract class should override all abstract methods or
it should be declared as abstract else it leads to CE:
For example
abstract class Example
{
static abstract void m1(); //CE: illegal combination of modifier abstract
abstract void m2(); // and static
}
For example:
abstract class Example
{
final abstract void m1(); //CE: illegal combination of modifier abstract
abstract void m2(); // and final
}
For example:
abstract class Example
{
private abstract void m1(); //CE: illegal combination of modifier
abstract
abstract void m2(); // and private
}
To ensure our class non-static members are only accessible via sub-class
objects we should declare the concrete class as abstract.
Consider a classic “Bank”, the base type is “Bank” and each bank has a
Rate Of Interest, etc.
Note: What we will do here is, first we will create a real-time example
without using Abstraction, then we will identify the problem, and finally
we will see how to overcome the problem using abstraction in java.
The idea is that such changes are not supposed to have any impact on
client code since they involve no difference in abstract behavior.
You might have used ATM machines many times for cash withdrawal
and bank statements.
The ATM Machine has given a card swipe slot, a touch screen, and a
keyboard, you can use these features to withdraw the money but you
never know what exact actions or operations takes place internally in
the machine-like after a card swipe the machine checks whether the
card is valid or not, then the PIN will be verified if it is correct then only
we can withdraw the money but the withdraw money should not be
greater than the available balance.
class ATMMachine
{
public void Enter_Card()
{
System.out.println ("Card Verification");
}
As you can see, in the above code, now the user of our ATM Machine
class can call the methods and perform the necessary actions as shown
in the below code.
public class Main
{
public static void main (String[]args)
{
ATMMachine am = new ATMMachine ();
//Accessing the Public Properties and methods
am.Enter_Card();
am.Enter_Pin();
am.Cash_Withdrawal();
am.Validate_Withdraw_Amount();
am.Update_Amount();
am.Cash_Dispose();
}
}
Output:
As you can see in the above image, you get the output as expected.
Now, let us move and analyze the code. Do, we really need to call the
following three methods manually.
Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();
The way we have implemented the code in our ATMMachine class if the
withdrawal amount is less than the available balance, then also it is
going to perform the withdrawal operation. At the same time, the user
of the ATM Machine class may forget to call some of the methods as
shown in the below code.
public class Main
{
public static void main (String[]args)
{
ATMMachine am = new ATMMachine ();
//Accessing the Public Properties and methods
am.Enter_Card();
am.Enter_Pin();
am.Cash_Withdrawal();
am.Cash_Dispose();
}
}
Output:
As you can see in the above code, the necessary methods and
properties are exposed by using the “public” access modifier whereas
the unnecessary methods and properties are hidden from outside the
world by using the “private” access modifier.
We made the following methods private, so they are not exposed to the
user of the ATMMachine class.
Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();
Output:
Now, from the Main class (or the user of the ATMMachine class), if you
try to call the following methods then you will get a compile-time error.
Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();
Now, we are exposing the necessary methods (Enter_Card, Enter_Pin,
and Cash_Withdrawal) to the outside world or to the user of the
ATMMachine class by hiding its implementation details or the
Complexity (Validate_Withdraw_Amount, Update_Amount, and
Cash_Dispose). This is called abstraction in Java.