Abstraction in Java[1]
Abstraction in Java[1]
Abstraction in Java is another OOPs principle that manages complexity. It is a process of hiding complex internal implementation
details from the user and providing only necessary functionality to the users.
In other words, abstraction in Java is a technique by which we can hide the data that is not required to a user.
It hides all unwanted data so that users can work only with the required data. It removes all non-essential things and shows only
important things to users.
KeyPoint
1. Abstract is a non-access modifier in java which is applicable for classes, interfaces, methods, and inner classes.It represents an
incomplete class that depends on subclasses for its implementation. Creating subclass is compulsory for abstract class.
2. A non-abstract class is sometimes called a concrete class.
3. An abstract concept is not applicable to variables.
a.calculate(20, 30);
s.calculate(10, 5);
m.calculate(10, 20);
}
}
create an object of abstract class but the compiler will show a compile-time error.
package com.abstraction;
public abstract class AbsClass
{
// No abstract method here.
}
// Creating a subclass that inherits Abstract class.
public class Subclass extends AbsClass
{
public static void main(String[] args)
{
AbsClass c = new AbsClass(); // Compile-time error.
AbcClass obj;
}
}
where an abstract class Hello contains both abstract method and instance method. The abstract method “msg2” will be
implemented in Test class that extends a class Hello.
package com.abstraction;
public abstract class Hello
{
// Declaration of instance method.
public void msg1()
{
System.out.println("msg1-Hello");
}
abstract public void msg2();
}
public class Test extends Hello
{
// Overriding abstract method.
public void msg2()
{
System.out.println("msg2-Test");
}
public static void main(String[] args)
{
// Creating object of subclass Test.
Test obj = new Test();
obj.msg1();
obj.msg2();
}
}
program where an abstract class can have a data member, constructor, abstract, final, static, and instance method (non-
abstract method).
package com.abstraction;
public abstract class AbstractClass
{
int x = 10; // Data member.
AbstractClass()
{
System.out.println("AbstractClass constructor");
}
final void m1()
{
System.out.println("Final method");
}
void m2()
{
System.out.println("Instance method");
}
static void m3()
{
System.out.println("Static method");
}
abstract void msg();
}
public class AbsTest extends AbstractClass
{
AbsTest()
{
System.out.println("AbsTest class constructor");
}
void msg()
{
System.out.println("Hello Java");
}
public static void main(String[] args)
{
AbsTest t = new AbsTest();
t.msg();
t.m1();
t.m2();
m3();
System.out.println("x = " +t.x);
}
}
Why abstract class has constructor even though we cannot create object?
We cannot create an object of abstract class but we can create an object of subclass of abstract class. When we create an object of
subclass of an abstract class, it calls the constructor of subclass.
This subclass constructor has super in the first line that calls constructor of an abstract class. Thus, the constructors of an abstract
class are used from constructor of its subclass.
If the abstract class doesn’t have a constructor, a class that extends that abstract class will not get compiled.
package com.abstraction;
public abstract class Employee
{
private String name;
private int id;
public Employee(String name, int id)
{
this.name = name;
this.id = id;
}
// Declaration of concrete method.
void m1()
{
System.out.println("Name: " +name);
System.out.println("Id: " +id);
}
}
public class Engineer extends Employee
{
public Engineer(String name, int id)
{
super(name, id); // This statement is used to call super class constructor.
}
public static void main(String[] args)
{
// Creating an object of the subclass of abstract class.
Engineer e = new Engineer("Deep", 10202); e.m1();
}
}
program in which an abstract class reference refers to the subclass objects. Abstract class reference can be used to call
methods of the subclass.
package Abstarctclass;
public abstract class Identity
{
abstract void getName(String name);
abstract void getGender(String gender);
abstract void getCity(String city);
}
public class Person extends Identity
{
void getName(String name)
{
System.out.println("Name : " +name);
}
void getGender(String gender)
{
System.out.println("Gender : " +gender);
}
void getCity(String city)
{
System.out.println("City: " +city);
}