Abstraction in Java

During learning of Java programming a term comes Abstract and that term attached with several other terms like class, methods and introduced with a new concept called Abstraction. Abstraction is a process to hide certain details from user and only show essential feature of objects.

Abstract class
Abstract class is such type of class that contains abstract methods although Abstract classes may contain abstract and concrete both methods but a class which contains abstract method must be declared as Abstract class.
Abstract method
Abstract terms introduced during Inheritance for hiding unwanted information from user and in case of Inheritance parent and child class terms are introduced during which child class inherit property of parent class and process will be continued but in this case Abstract methods are declared in Abstract class but that does not contains its definition, child class that inherit Abstract class must be contained definition of Abstract method.
Simple Structure of Abstract class

abstract class Parent {
    abstract method1();
    abstract method2();
}
class child extends Parent {
    method1() {}
    method2() {}
}
If a class contain any method abstract then that class must be declared as Abstract class. Abstract class has highly significance for hiding unwanted information from user.
Syntax:

abstract class className{

}
Example:

abstract class Vegetable {

    abstract void color();

    abstract void price();
}

class Potato extends Vegetable {

    void color() {
        System.out.println("Potato color is Brown");
    }

    void price() {
        System.out.println("Potato is 30 rupees/kg");
    }
}

class Tomato extends Vegetable {

    void color() {
        System.out.println("Tomato color is Red");
    }

    void price() {
        System.out.println("Tomato is 40 rupees/kg");
    }
}

public class AbstractDemo {

    public static void main(String a[]) {
        Potato veg1 = new Potato();
        Tomato veg2 = new Tomato();
        veg1.color();
        veg1.price();
        veg2.color();
        veg2.price();

    }

}

OutPut:
Is Abstract variable possible in java or not?
In Java abstract variables are not possible abstract modifiers can be used with class, methods.
Points Regarding Abstract class

  • Abstract class contains abstract method which contains only declaration and doesn’t contain any definition.
  • Abstract classes have constructor, abstract methods, constructors and normal methods also.
  • Abstract class may or may not have any abstract method but if any class contains abstract method then that class must be declared as Abstract class.
  • Concept of Abstraction is used in case of Inheritance hence if any class inherited property of abstract class then it must contains definition of abstract methods.

Comments

Popular posts from this blog

Secure Database Connectivity in node.js with mysql

Export data from mysql db to csv file using java

API (Application Programming Interface)