HTML Java

Java Abstraction


Abstraction

We don’t apply abstraction on data we only hiding data and abstraction perform on funtionlaity Show functionality hide complexity Or show function hide implementation

abstract is a keyword as well as modifier and it use before class and member function but you can’t use before datamember and constructor.

abstract class A {
   abstract void show();
}

abstract method should be inside abstract class and abstract method is only declaration and its omplemention in another class.

Note: Abstract method can’t be static, final, private.



What is abstract class?

Class A {
   --normal class
}

abstract class A {

}


Abstract class may and may not have abstract method.abstract class can't be instanciated .

Class A extends B{
   --normal class
}

abstract class A {
   abstract void hi();
   abstract void hello() {}
}


When class A extends class B then class must have define all the method inside B.

class A extends B {
   void hi() {}
   void hello() {}
}


Or make this class also abstract then no need to define the method in class A. like this,

Abstract class A extends B{ }


We can create references of Abstract class

A a=new B();


Here object type is B and reference variable type is A and its an also example of upcasting.

We can hold the child class object into parent class Reference variable.This is very important use.

NOTE: Can we have a constructor inside abstract class? Yes we have but you think that we can’t create an object of abstract class and you then there is no constructor inside abstract class but constructor is there whether we define or not compiler automatically provide constructor in our class. Because when Base class extends by some other class and this class is not have constructor then rull will be break because when child class extending parent class then 1 parent class constructor run after that child class that’s why there must be constructor in abstract class.