Abstraction in Java
In a simple
word Abstraction means only key concept, for example we all are using TV
remote and we know that how to on and off the TV by pressing a particular
button but we don’t know how it does but we know it is used to on or off the
TV.
How to create an Abstract class in Java?
See also
Java Inheritance with example
Java Polymorphism with example

In other
words Abstraction is the process of hiding implementation details and
showing only functionality to the users.
So Abstraction
lets us focus on what the objects does instead of how it does it, refer above
real world example.
How to achieve Abstraction in Java?
There are
two ways to achieve abstraction Java.
1. Abstract class (It is known as 0 to
100% abstract class).
2. Interface (It is also known as 100%
abstract class).
How to create an Abstract class in Java?
If a class
declared as abstract keyword then it is known as an abstract class. It contains
abstract and non-abstract method also. If we declare a class as abstract and it
has some methods (may be abstract or concrete) then we need to extend this
class and provide implementation according to requirement.
Important points to remember
1. If you want to create an abstract
class then we need to declare this class as abstract.
2. It can have abstract methods as well
as non-abstract methods too.
3. We can’t instantiate an abstract
class.
4. It can have constructors and method
also.
5. It can have final methods also which
will restrict the child class to do any modification with its original body.
Example of an abstract class
A class
which is declared as abstract known as abstract class.
abstract class X{}
Abstract Method in Java
A method
which is declared as abstract and does not have body i.e., implementation is
known as abstract method.
See this
example.
Abstract void showDetails();
Example
of Abstract class with an abstract method
In this
example we have created an abstract class Cricket
with an abstract method play(),and we have provided its implementation in Sport
class.
abstract class Cricket{
abstract void play();
}
public class Sport extends Cricket{
@Override
void play() {
System.out.println("Playing Cricket");
}
public static void main(String[] args) {
Cricket obj=new Sport();
obj.play();
}
}
Output:
Playing
Cricket
Another example of Abstract class in Java
In this
example we have created an abstract class India with an abstract method state()
and providing its implementation in sub classes i.e., Bihar,Up
and Maharashtra classes.
abstract class India{
abstract void state();
}
class Bihar extends India{
@Override
void state() {
System.out.println("Hello I am from Bihar");
}
}
class Up extends India{
@Override
void state() {
System.out.println("Hello I am from UP");
}
}
class Maharshtra extends India{
@Override
void state() {
System.out.println("Hello I am from Maharashtra");
}
}
public class StateOfIndia{
public static void main(String[] args) {
India obj=new Bihar();
obj.state();
obj=new Up();
obj.state();
obj=new Maharshtra();
obj.state();
}
}
Output:
Hello I am from Bihar
Hello I am from UP
Hello I am from Maharashtra
Abstract class with constructor ,abstract method and
non-abstract method
Here in this
example we have created an abstract class India with constructor
, an abstract method state() and a non-abstract method TestMethod().
We provided the implementation of state() method in sub class Bihar.
abstract class India{
public India() {
System.out.println("India class constructor");
}
abstract void state();
void TestMethod() {
System.out.println("Test Method Invoked");
}
}
class Bihar extends India{
@Override
void state() {
System.out.println("Hello I am from Bihar");
}
}
public class StateOfIndia{
public static void main(String[] args) {
India obj=new Bihar();
obj.state();
obj.TestMethod();
}
}
Output:
India class constructor
Hello I am from Bihar
Test Method Invoked
Note : An abstract class can have data members ,abstract
methods, method body(Non-abstract method) , constructor and even main
method also.
Abstract class with data members and with constructor
In this
class we have created an abstract class MyClass with id, name, parameterized constructor
and main method also, here we are using anonymous inner class to create an
object of this class to access the members.
public abstract class MyClass{
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
MyClass
obj=new MyClass(1,"Matin") {
};
System.out.println(obj.id+" "+obj.name);
}
}
Output:
1 Matin
We can achieve
the same thing by creating a child class and main method in abstract class to
create the object of abstract class see example carefully , here we are not
using constructor.
Since we can
not access the non-static member without creating an object of class, as we can
not create the object of abstract class, so we need to create a concrete child
class and use the this class to access the members of abstract class. For
example,
public abstract class MyClass{
private int id=1;
private String name="Matin";
public static void main(String[] args) {
MyClass
obj=new ChildClass();
System.out.println(obj.id+" "+obj.name);
}
}
class ChildClass extends MyClass{
}
Output: 1 Matin
See also
Java Inheritance with example
Java Polymorphism with example
No comments