DEV Community

Prasanth
Prasanth

Posted on

Abstraction

What is Abstraction ?

  • Abstraction means hiding internal details and showing only the essential features to the user.

(or)

  • Abstraction in java is process of hiding implementation details and showing only functionality.

  • abstract is a keyword ,it is non-access modifier , used classes and methods.

  • It tells what an object does, not how it does it.

Achieved using:

  • Abstract classes.( 0% to 100% partial Abstraction and can have both abstract and normal methods )
  • Interface(100% Abstraction -> all methods are abstract by default until java 7). use interface for multiple inheritance.

  • From java 8 onward, interface can have(Interface gives 100% abstraction (before Java 8).):

  • default methods(with body)

  • static methods, so interface not always 100% abstract anymore.

Abstract Class:-

  1. A class with the abstract keyword is called an abstract class.
  2. It can have:
  • Abstract method(without body)
  • Concrete methods(with body)

Abstract classes can have:

  • Abstract methods
  • Concrete (regular) methods
  • Constructors (including parameterized constructors
    
  • If a class has even one abstract method , that class must be abstract.

  • abstract class restricted to Cannot create objects of an abstract class. but subclass extends the abstract class, and you create an object of subclass, the constructor of the abstract class will still run. because sub class object is created ,so the abstract class constructor run first after own class constructor.

  • If child class(any class) inherit(extend) super of abstract class must to implement its abstract methods otherwise that class also abstract class and that class can not create object.
    abstract class can have static and final methods.

  • Abstraction class can contain fields(variable) it helps to initialized through constructors.

Abstract Method:-

  • Declared with the abstract keyword.
  • Abstract method no body , just the method name and a semicolon.
  • That class that contain abstract method it must also be abstract.
  • Any class that inherits the abstract class: must implement the abstract method (must override abstract method )other wise declare the class also abstract.
  • you can not create an object of an abstract class.
  • A subclass must override all abstract methods of an abstract class.
  • Abstract method can have access modifiers like public, protected ,or default(no modifier).
  • But not private ,because the subclass needs access to override it.

Example:-

abstract class Father
{
abstract void  skill(); //abstract method
}


class Child extends Father

void skill(){
System.out.println("I Drawing well the picture ");
}
}

Enter fullscreen mode Exit fullscreen mode

Example:-

abstract class Father
{
abstract void  skill(); //abstract method
}


class Child extends Father
{
    // Did NOT implement skill()
}
// compile time error came 
Enter fullscreen mode Exit fullscreen mode

Example:-

when you send an email , you don not know what happens behind the scenes(protocols,servers). you just type, enter an address, and click send.

  • Abstraction is -> you see what it does, not how it works.

How many abstract methods can an abstract class have?

  • 0,1 or many in abstract class.
  • A abstract method may or may not abstract method even 0 abstract method allowed.

Can we use non-access modifiers like static, final, abstract together?

  • you can combine abstract with public ,protected.

can not use final and abstract and static method can to be abstract.

Can abstract methods have parameters and return types?

yes.

Can we override an abstract method with the same parameters?

yes. that is required , method signature must match.

Can an abstract class have an inner class?

Yes. Abstract classes can have inner classes

Why Interface provides 100% Abstraction (before Java 8)

Why Abstract Class Cannot Achieve 100% Abstraction?

Above 2 question ans in command section

package abstraction;

abstract public class Vehicle {

    // you can use fields private use to getter/setter method (abstraction +Encapsulation)
    String brand;
    int wheels =4;
    final int speedLimit =120;



    //constructor in abstract class
    Vehicle(String brand)
    {
        this.brand = brand;
        System.out.println("Vehicle constructor called");
    }

    // abstract method (must be implemeted in child class)

    // abstract method can use public ,default(no modifer),proctecte
    // can not use abstract method in private,static,final
    abstract void start();

    abstract void carWashing();

    //concreate method

    void stop()
    {
        System.out.println("Vehicle stopped");
    }
    final void fueltype()
    {
        System.out.println("car Fuel type Petrol or Diesel");
    }

    static void info()
    {
        System.out.println("Vehicle are used for transport");
    }
    public static void main(String[] args) {

        //Vehicle vehicle1 = new Vehicle();  object can not created in abstract class in core concept java incomplemetd  method in class can not create object .
    }
}



// child class



package abstraction;

abstract public class Vehicle {

    // you can use fields private use to getter/setter method (abstraction +Encapsulation)
    String brand;
    int wheels =4;
    final int speedLimit =120;



    //constructor in abstract class
    Vehicle(String brand)
    {
        this.brand = brand;
        System.out.println("Vehicle constructor called");
    }

    // abstract method (must be implemeted in child class)

    // abstract method can use public ,default(no modifer),proctecte
    // can not use abstract method in private,static,final
    abstract void start();

    abstract void carWashing();

    //concreate method

    void stop()
    {
        System.out.println("Vehicle stopped");
    }
    final void fueltype()
    {
        System.out.println("car Fuel type Petrol or Diesel");
    }

    static void info()
    {
        System.out.println("Vehicle are used for transport");
    }
    public static void main(String[] args) {

        //Vehicle vehicle1 = new Vehicle();  object can not created in abstract class in core concept java incomplemetd  method in class can not create object .
    }
}



Enter fullscreen mode Exit fullscreen mode

Top comments (0)