-1

What is the need of Abstract class?Abstract Class - is a class which cannot be instantiated & can include abstract or instance methods, constructors etc.

Instance methods can be used for implementing common functionality for all derived classes which inherits this abstract class.If my base class can solve the implementing of common functionality, then why do we need an abstract class for common functionality???Can anybody help me.

shikha
  • 15
  • 2
  • This a googling question . You can find a lot of answers on google.Try to use google. – Human Being Sep 23 '13 at 07:11
  • 1
    possible duplicate of [Understanding the purpose of Abstract Classes in Java](http://stackoverflow.com/questions/9197521/understanding-the-purpose-of-abstract-classes-in-java) – default locale Sep 23 '13 at 09:46

2 Answers2

1

An abstract Class can enforce a particular design pattern for the Classes that are extending it.For example, there is method for which implementation for each Child class will be different but method has to be there,then that method can be declared as abstract in the abstract class and at the same time the common methods whose implementation remain same can be inherited by the Child class from the abstract parent Class.

Simple Diagram explains the Abstract Class concept below:

enter image description here

Source

Nargis
  • 4,627
  • 1
  • 26
  • 44
0

Sometimes it doesn't make sense to instansiate a base class. Take for example

public abstract class Vehicle {

    public int getMaxSpeed();

With sub classes

public class Car extends Vehicle {

    public void getMaxSpeed {
       return CAR_MAX_SPEED;

public class Train extends Vehicle {

    public void getMaxSpeed {
       return TRAIN_MAX_SPEED;

Now if you do this

Vehicle v = new Vehicle(); // Not a car nor a train nor a bus...

What should v.getMaxSpeed() return if it doesn't represent any actual real world kind of vehicle but just an abstraction?

Simon
  • 6,093
  • 2
  • 27
  • 34