Abstraction

Author: Ravi Poswal

Data abstraction hides the information with implementation and only shows essential services to the user. For example bike

Abstractions are used in two ways in the class

  1. Abstract class

  2. Interface

Abstract class: A class may or may not include abstract methods, but if a class does, it is automatically referred to be an abstract class. An abstract class is not compulsory to declare an abstract method.

Some important rules:-

  • It must be inherited from another class in the same package.

  • It must be declared abstract using the abstract keyword.

  • An abstract method that you write must be implemented in another class.

  • It can have a constructor.

Code Example

abstract class B
{
abstract void run();
}
class A extends B 
{
void run()
{ 
System.out.println("running safely"); 
}
public static void main(String arg[])
{
B obj=new A();
obj.run();
}
   }