Abstraction in Java (OOP Concept 4)
Hello readers!!! Today I'm going to share some points on our final key OOP concept called Abstraction..What is Abstraction in OOP? How to use it in Java? I will explain you step by step. Be with me..
Still you haven't read my Java article series, Visit the below link first..
Java with TechPool
Abstraction
Abstraction shows only important things to the user and hides the internal details. But this is not Encapsulation. Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding). Implementation of the method is hidden from the user. When we create programs, sometimes we have to create some methods without having a meaningful body. It means that the method exists, but it has no codes in the body. Usually this kind of a class can be in the super class. Then in its sub class, we override it..Such situations should be handled. OOP provides a solution for this situation..It's Abstraction!
Look at this example..
class Animal{
void eat(){
}
}
class Dog extends Animal{
void eat(){
System.out.println("Dog is eating...");
}
}
class Test {
public static void main(String args[]){
Animal dog = new Dog();
dog.eat();
}
}
Here you see the eat method in Animal class doesn't have a body! But when a reference variable is created using Animal class and made object using Dog class, the dynamic method dispatch happens.
There are some important things to be remembered in Abstraction..
KEY Facts:
1. If there is any method in a class without a body, that method should be defined as an abstract method. Use abstract key word. Then there is no curly brackets...Ex: abstract void eat();
2. If there's an abstract method in a class, that class should be abstract class also.
Ex: abstract class Animal(){ }
3. In an abstract class, there can be methods with implementations. See the below example..No errors!
abstract class Animal{
abstract void eat();
public void sleep(){
System.out.println("Animal is sleeping..");
}
}
class Test {
public static void main(String args[]){
}
}
4. Abstract classes can be declared even without an abstract method also.. Comment out the abstract method in the above example and see whether there are errors or not.
5. Attributes can not be abstract in an abstract class..
abstract class Animal{
abstract int age; // This gives errors..
abstract void eat();
}
class Dog extends Animal{
void eat(){
System.out.println("Dog is eating...");
}
}
class Test {
public static void main(String args[]){
}
}
Got errors? It says that attributes can not be abstract!6. Objects can not be created using an abstract class. Check this one! You will get the error..
abstract class Animal{
abstract void eat();
}
class Test {
public static void main(String args[]){
Animal a1 = new Animal(); // This gives errors..
}
}
7. There can be constructors in an abstract java class. But they can not be abstract constructors..Below example carries errors. Remove abstract from constructor. Then no errors.
abstract class Animal{
abstract Animal(){
}
abstract void eat();
}
class Test {
public static void main(String args[]){
}
}
8. We can create sub classes using an abstract class. But only we can use a Supper class reference variable to create an object from sub class..See this!
But, the abstract method in the super class(abstract class) must be overridden inside the sub class. You can not use abstract for overriding. Method should be overridden without abstract key word. See the below one!
abstract class Animal{
abstract void eat();
}
class Dog extends Animal{
void eat(){
System.out.println("Dog is eating...");
}
}
class Test {
public static void main(String args[]){
Animal a = new Dog();
}
}
But, the abstract method in the super class(abstract class) must be overridden inside the sub class. You can not use abstract for overriding. Method should be overridden without abstract key word. See the below one!
abstract class Animal{
// method 1
abstract void eat();
// method 2
abstract void sleep();
// method 3
abstract void move();
}
class Dog extends Animal{
//method 1 overriding
void eat(){
System.out.println("Dog is eating...");
}
// Errors : can not use abstract key word
abstract void sleep();
// Here, method 3 must be overridden
}
class Test {
public static void main(String args[]){
}
}
9. If a method in the abstract class is a static / final or private, they can not be overridden in the sub class.
Including all these key facts, this is the final example base on Abstraction.
abstract class Animal{
private int age;
abstract void eat();
void sleep(){
System.out.println("Animal is sleeping...");
}
}
class Dog extends Animal{
void eat(){
System.out.println("Dog is eating...");
}
}
class Test {
public static void main(String args[]){
Animal a = new Dog();
a.eat(); // Executed with overriding
a.sleep(); // Executed without overriding
}
}
Output:
That's all guys!!!
This is the way to implement Abstraction functionality. Now I have covered all 4 key concepts in OOP using Java. I think all these articles will help you to improve yourself..That's my goal..
So, read this and practice the examples..You will be familiar with OOP concepts. I will come again with more java articles. Till then,
Good Bye guys!
0 Comments