DEV Community

Mohan Mogi
Mohan Mogi

Posted on

Java Abstract Class Explained with Simple Examples

Abstract:

  • Abstraction is non access modifier.
  • Abstraction is hides internal implementataion details and shows only essential functionallity to the user.
  • Abstract use to two major programming elements 1.Abstract class 2.Abstract methods.
  • An abstract class is declared using the abstract keyword.
  • Abstract class is cannot be used to create object directly.
  • Another child class use extends the keyword to inherit its access properties and methods.
  • Child class if a extends abstract class,it must implement all abstract methods unless child class is also declared abstract.
  • One method is Abstract that class is also abstract class.
  • Abstract class conatains : 1.Abstract method. 2.Normal method. 3.non-static (instance) Variables. 4.Constructor. 5.Static variables.

example:

Abstract classs

Childclass extends:

Output:

Explanation:

  • Abstract class: Animal cannot be instantiated directly.
  • Abstract method: sound() declares behavior but has no implementation.
  • Method implementation: Dog provides the implementation for sound().
  • Inheritance: Dog inherits the eat() method from Animal.
  • Method overriding: Dog overrides the abstract sound() method.
  • Object creation: new Dog() creates an object of the concrete subclass.

Top comments (0)