Data Abstraction is one of the concept of Object Oriented Java Programming, By Data Abstraction we can able to show the services provided by the application to the outside by hiding the internal implementation.
1. Abstract Classes (Partial abstraction).
2. Interfaces (complete abstraction).
Abstract Classes : In Java when a class is declared with keyword abstarct then class is declared as an abstract class.
If a class is having at least on abstract method then for sure class will be abstract, that is we have only method defined but not implemented.
Sample Abstract class is as below.
For Example, If you go to Facebook, We chat, We post stuff in the timelines, We like, We share the posts. all these are services provided by the Facebook, but Facebook is hiding the background implementation to the end user. (i.e, what code it is executing if we click on like button, what code is executing we share etc).
In Java, Abstraction can be done in two ways.
1. Abstract Classes (Partial abstraction).
2. Interfaces (complete abstraction).
Abstract Classes : In Java when a class is declared with keyword abstarct then class is declared as an abstract class.
If a class is having at least on abstract method then for sure class will be abstract, that is we have only method defined but not implemented.
Sample Abstract class is as below.
package com.learning.partialAbstraction;
public abstract class SampleAbstractClass {
public void getSampleMethodwithReturnVoid() {
System.out.println("Sample Abstract Class");
}
abstract void getSampleAbstractMethod();
}
From the above class,
abstract void getSampleAbstractMethod();
is the abstract method as there is no method implimentation, and the abstract class can also contain concrete method
public void getSampleMethodwithReturnVoid() {
System.out.println("Sample Abstract Class");
}
We can also declare instance variable to be public,privatc, static, final etc, there are no restrictions on using access modifiers on instance variables in the abstract class,
but abstract methods cannot be final,static and private because, implementation logic of the abstract method should be done by its sub classes,Most importantly you cannot instantiate an abstract class.
abstract methods can have three levels of access modifiers,
1. no access modifier (will be treated as default) -- only (abstract can be extended by the classes of same package.
2. public - you have universal access of this abstract.
3. protected - classes of the same package and outside package of same work space.
Why Abstract Classes? this is the question which is asked by many.
Abstract Classes are introduced because, if your not sure of the complete requirement.
In other words, when we talk about abstract classes, we talk about how an object behaves, we are providing the characteristics of the object type.
For example: Lets take an example of a Man (note:Its not gender biasing on women :))
1. A Man has to Sleep, Its a common feature,
2. A Man has to Work, But we are not sure what kind of work,
so the common feature will be the concrete method, the working feature we are not sure, so we are making it Abstract.
Below is the representation of the example.
package com.learning.partialAbstraction;
public abstract class AbstractManClass {
public String sleepMethod() {
return "on an average a man sleeps for 8 hours";
}
public abstract String typeOfWork();
}
there are different type of work a man can do. lets see how the abstract classes are extended and implement the abstract typeOfWork() method.
Lets take two classes ManA.java and ManB.java
package com.learning.partialAbstraction;
public class ManA extends AbstractManClass{
public String typeOfWork() {
return "Is a JAVA PROGRAMMER";
}
}
package com.learning.partialAbstraction;
public class ManB extends AbstractManClass {
public String typeOfWork() {
return "Is a TESTER";
}
}
Now lets the classes,
package com.learning.partialAbstraction;
public class TestingAbstractClass {
public static void main(String[] args) {
ManA a = new ManA();
System.out.println("------------------------------------");
System.out.println("Man A " + a.sleepMethod());
System.out.println("Man A " + a.typeOfWork());
ManB b = new ManB();
System.out.println("------------------------------------");
System.out.println("Man B " + b.sleepMethod());
System.out.println("Man B " + b.typeOfWork());
System.out.println("------------------------------------");
}
}
Output:
------------------------------------
Man A on an average sleeps for 8 hours
Man A Is a JAVA PROGRAMMER
------------------------------------
Man B on an average sleeps for 8 hours
Man B Is a TESTER
------------------------------------
So in the above example we can see, there two different Man classes, both the classes has one common thing which is sleep, and the other work is different.
One more interesting topic of abstract classes are the,
Generic Abstract Classes, if we want to enforce the return type of the abstract method, we go for generic abstract type.
Lets create an Abstract class GenericAbstractClass.java
package com.learning.inheritance;
public abstract class GenericAbstractClass<Type> {
public abstract Type abstractMethod();
}
Here Type may be return type, so when we extend this class, We have to pass the Return type while extending, such that the implemented method strictly follows the same return type.
Lets take a class Subclass.java extending the GenericAbstractClass
package com.learning.inheritance;
public class SubClass extends GenericAbstractClass<Void>{
@Override
public Void abstractMethod() {
return null;
}
}
Void here is not a keyword, its a class available in java.lang. package, which returns a value null. as in Generics keywords are not accepted.
example for Ineteger gereric type
package com.learning.inheritance;
public class SubClass extends GenericAbstractClass<Integer>{
@Override
public Integer abstractMethod() {
return null;
}
}
These Generic Abstract classes widely used in the Command Pattern (will discuss more on this pattern when we cover design patterns).