Abstraction in Java, Hiding the class implementation is known as abstraction. Abstraction can be achieved by defining class behavior in an Interface and implementing it in a class. Instead of creating a class reference, referring to the class object through an interface reference is known as abstraction. The advantage of abstraction is if the implementation of the class is changed, that will not affect the usage of the class, hence one can provide enhancement modification easily.
Description
Abstraction in Java, All abstract methods must be declared using “abstract” keyword. If a class containing at least one abstract method, then the class must be declared as abstract. If a class declared as abstract as it is not mandatory to develop abstract methods.
The class can contain:
Only Abstract Methods.
Only Concrete Methods.
Or Both
Conceptual
figure
Example
The following is an example for abstract that has abstract method.
[java]
package com;
abstract class Tutorial{
abstract void run();
}
class Java extends Tutorial{
void run(){System.out.println("It's a java tutorial");}
public static void main(String args[]){
Tutorial obj = new Java ();
obj.run();
}
}
[/java]
In the above example, Tutorial is the abstract class that has abstract method run() and implementation for this method will be provided by the class Java .
Output:
When compile code output will be as follows.
[c]
It's a java tutorial
[/c]
More Info
An instance of abstract class cannot be created, hence non-static member of abstract class cannot be referred.
Abstraction in Java, If an abstract class contains static members, class name can be used to refer it.
Subclass are treated as abstract until unless it overrides all the members of abstract base class or provides definitions for all abstract methods of the super class.
If a class has to build Is - A relation with abstract class or if a class has become a type of abstract class, then the class has to fulfill the contract of the abstract class.
The contract specifies that the subclass should provide definition for all the abstract methods of the abstract class. Otherwise subclass becomes abstract.
Example
[java]
package com.spl.abstract1;
public class Abstract1 {
public static void main(String[] args) {
System.out.println("Program starts");
B dRef1=new B();
dRef1.test1();
dRef1.test2();
System.out.println("Program ends");
}
}
abstract class Sample1
{
abstract int test1();
abstract void test2();
}
abstract class A extends Sample1
{
int test1()
{
System.out.println("test1() is defined in class A");
return 12;
}
}
class B extends A
{
void test2()
{
System.out.println("test2() is defined in class B");
}
}
[/java]
Above example class A preaches or violets the contract of sample 1 abstract class because class A is not defining test 2() method which is still abstract in the class A.
Class B is a concrete class even though it is not overriding all the abstract methods because class B contains only test 2()method as an abstract that method is defined in class B).
Where as test 1() is already made concrete in the super class A.
Output:
When compile the code result will be as follows.
[c]
Program starts
test1() is defined in class A
test2() is defined in class B
Program ends[/c]
Example
[java]
package com.spl.abstract2;
public class Abstract2 {
public static void main(String[] args) {
System.out.println("Program starts");
A dRef1=new A();
dRef1.test1();
System.out.println("Program ends");
}
}
abstract class Sample1
{
int test1()
{
System.out.println("test1() is defined in class Sample1");
return 12;
}
}
class A extends Sample1
{
}
[/java]
Output:
When compile the code following is the result will be generated.
[c]
Program starts
test1() is defined in class Sample1
Program ends[/c]
In the above example the class B is becoming concrete before fulfilling the contract of abstract class because the abstract class doesn’t have any abstract method that has to be overridden by subclass. Hence, abstract class is not 100% abstract.
The following is an example for abstract class which will have data member, constructor and method.
[java]package com;
abstract class Tutorial1{
Tutorial1(){System.out.println("My tutorial");}
abstract void run();
void myblog(){System.out.println("This is my blog");}
}
class Java1 extends Tutorial1{
void run(){System.out.println("It's java tutorial");}
}
class TestAbstraction{
public static void main(String args[]){
Tutorial1 obj = new Java1();
obj.run();
obj.myblog();
}
} [/java]
Output:
The result will be as follows.
[java]
My tutorial
It's java tutorial
This is my blog
[/java]
Summary
Key Points
To achieve 100% abstract, develop all the methods in abstract class as abstract or we can use interfaces.
Abstraction in Java, Abstraction will be achieved by abstract class.
One can declare abstract keyword before the class name and method name.