In Java abstraction is the technique which we come across every day. Abstraction is one of the major pillars of OOP. Using abstraction we can achieve great things. Today in this article we are going to discuss abstraction. Let’s understand what abstraction is.
What is abstraction?
Abstraction is related
to data/details. Data abstraction is the process of hiding details from the
user and showing only the necessary information. This mechanism helps the
developer to hide trivial information from the user. For instance: the user
will see the ATM as an ATM rather than its components.
Let us
understand this by taking the real-life scenario of an ATM. While using the ATM
the user only knows that by pressing the “Withdraw Cash” button he will get the
cash or by pressing the “Get Statement” he will get the bank statement. The user
is completely unaware of how the cash is withdrawn, what calculation the ATM
did to withdraw the cash or how the bank statement is calculated.
Now that we have
the upper level of knowledge on what exactly the abstraction is, let's discuss this
topic in terms of java.
Abstraction in java
In java
abstraction can be achieved using the interface and abstract classes. We can
achieve 100% abstraction using interfaces in java.
There are
abstract classes and abstract methods in java.
Abstract Classes:
These are the restricted classes that cannot be used
to create objects. It only contains abstract methods.
Abstract Methods:
1. An abstract class is declared with abstract keyword.
2. Abstract
methods are those methods which are only declared
they are not defined.
3. An abstract class
is a class which contains only abstract methods
in it.
4. An
abstract class may or may not have abstract methods.
Some of the methods in abstract class can be concrete
methods also.
5. Methods
declared as an abstract need to be implemented in the sub-class thus making overriding compulsory or make the subclass as
abstract.
6. If
a class contains one or more abstract methods then that class must be declared as Abstract.
7. We
cannot create the object of an Abstract class.
8. The
an abstract class can have parameterized constructor. A default constructor is
always present in the abstract class.
Where to use abstract class and abstract methods
There are
situations where you want to define a superclass that declares an abstract
structure without fully implementing each method. That is, sometimes we want to
create a superclass that only defines the generalization form, which is shared
by all its subclasses, leaving each subclass to fill out the description.
Consider the Person class
example. Here the base type is Person and each
person will have colour, height, weight and so on. From this specific type of
person are derived Person1 and Person2, each of which will have specific characters.
Java program to illustrate the concept of Abstraction
abstract class Person | |
{ | |
// these are abstract methods | |
String name; | |
abstract String color(); | |
public abstract Long height(); | |
public abstract Long weight(); | |
// abstract class can have constructor | |
public Person(String name) { | |
System.out.println("Person constructor called"); | |
this. name = name; | |
} | |
// this is a concrete method | |
public String getName() { | |
return name; | |
} | |
} | |
class Person1 extends Person | |
{ | |
String name; | |
public Person1(String name) { | |
// calling Person constructor | |
super(name); | |
System.out.println("Person1 constructor called"); | |
this. name = name; | |
} | |
@Override | |
String color() { | |
System.out.println("Person1 colour is white"); | |
} | |
@Override | |
public Long weight() { | |
return 65L; | |
} | |
@Override | |
public Long height() { | |
return 165L; | |
} | |
} | |
class Person2 extends Person{ | |
String name; | |
public Person2(String name) { | |
// calling Person constructor | |
super(name); | |
System.out.println("Person2 constructor called"); | |
this. name = name; | |
} | |
@Override | |
public Long weight() { | |
return 50L; | |
} | |
@Override | |
public Long height() { | |
return 150L; | |
} | |
public class Test | |
{ | |
public static void main(String[] args) | |
{ | |
Person p1 = new Person1("Ram"); | |
Person p2 = new Person2("Shyam"); | |
System.out.println(p1.toString()); | |
System.out.println(p2.toString()); | |
} | |
} | |
} |
Output:
Person
constructor called.
Person1
constructor called.
Person
constructor called.
Person2
constructor called.
Person1 weight
is 65.
Person1 height
is 165.
Person1 weight
is 50
Person2 height
is 150.
Advantages of Abstraction:
1.
It
increases the reusability of code.
2.
It
helps to increase the security as it exposes only the important functionalities
of the application.
Related Articles:
1.
Core java interview questions.
3.
Internal working of Hashmap in java.
5. Compile time polymorphism in java.
6. Runtime Polymorphism in java.
0 Comments