Abstract class in Java

Abstract class Definition -

A class that is declared as abstract using abstract modifier is called abstract class in java.

it is partially implemented class used for developing some of the operations of an object which are common for all next level sub classes.  So it contains both abstract methods, concrete methods including variables, blocks and constructors.

It is created by using keywords abstract class. it is always created as super class next to interface in object's inheritance hierarchy for implementing common operations from interface.

In other word you can say that if you don't know the full implementation of methods available inside interface and you want to implement few of the methods of interface then you have to use abstract class in java for partial implementation.

Example -

  • We create Vehicle as interface and
  • We create Bus, Car, Bike as abstract class, because we have different types of Buses, Cars, Bikes with some common operations and individual operations. For example : RedBus, Express, Volvo

//Bus.java

abstract class Bus implements Vehicle {
         public void breaks() {
                System.out.println(" Bus has two breaks ");
         }
 }

At Bus level the method breaks() can only be implemented because it is common for all Bus subclasses. So this class must be declared as abstract.

Concrete class -

It is fully implemented class used for implementing all operations of an object. it is created just using the keyword class. It contains only concrete methods including variables, blocks and constructor.

It is always created as sub class next to abstract class or interface in object's inheritance hierarchy.

Example -

  • We  create Vehicle as interface with all its operations declarations.
  • We create Bus, Car, Bike as abstract class with all common operations implementation, and other individual operations are left as abstract.
  • RedBus, Volvo are created as concrete class by implementing all other operations which are left as abstract in its superclass, in this case engine() method, Check it in the below two application.

// RedBus.java

class RedBus extends Bus {
      public void engine() {
            System.out.println("RedBus engine speed 40KMPH ");
      }
 }

// Volvo.java

class Volvo extends Bus {
      public void engine() {
             System.out.println("Volvo engine speed is 150KMPH");
      }
 }

# Definition and need of concrete method and class ?

A method that has body is called concrete method and a class that has only concrete methods is called concrete class. it is used for implementing operations of an object.

# What are the modifier allowed for a concrete class ?

public, final, abstract, strctfp

# What are the members allowed inside a concrete class ?

static and non-static variables and methods, blocks, constructors, main method are allowed.

 

 

Leave a Comment

%d bloggers like this: