Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces

Abstract Classes

with Example Java Programs

In inheritance hierarchy, the superclass is very general and less specific. This class does nothing but only specifies the member functions that can be used in hierarchy. Such a class is called abstract class.

Abstract Classes

• In inheritance hierarchy, the superclass is very general and less specific. This class does nothing but only specifies the member functions that can be used in hierarchy. Such a class is called abstract class.

For example - In the following Java program we have created three classes-class A is a superclass containing two methods, the class B and class C are inherited from class A. The class A is an abstract class because it contains one abstract method fun1(). We have defined this method as abstract because, its definition is overridden in the subclasses B and C, another function of class A that is fun2() is a normal function. Refer Fig. 2.12.1.

Java Program [AbstractClsDemo.java] abstract class A

{

abstract void fun1();.

void fun2()

{

System.out.println("A:Infun2");

}

}

class B extends A

{

void fun1()

{

System.out.println("B:In fun1");

}

}

class C extends A

{

void fun1()

{

System.out.println("C:In fun1");

Subclass

}

}

public class AbstractClsDemo

{

public static void main(String[] args)

{

B b=new B();

C c=new C();

b.fun1(); //invoking the overridden method of class B

b.fun2();

c.fun1();//invoking the overridden method of class C

c.fun2();

}

}

Output

F:\test>javac AbstractClsDemo.java

F:\test>java AbstractClsDemo

B:In fun1

A:In fun2

C:In fun1

A:In fun2

Program Explanation

• In above program, the class A is a superclass. It is an abstract class as well. The name of this class is preceded by the keyword abstract. This class is abstract because it contains an abstract method fun1. The method is called abstract because it does not have any definition body. Note that the abstract method should be declared with the keyword abstract.

 There are two classes B and C which are subclasses of superclass A. The function definition fun1 is overridden in these classes.

In the main function we can access the methods of the subclasses by instantiating their objects. That is why we have created b as an object of class B and c as an object of class C. Using these objects appropriate fun1 can be invoked. Note that the fun2 will always be from class A even if we call it using the object of class B or C.

 If we write a statement A a=new A() i.e. if we instantiate the abstract class then it will generate compiler error. That means the abstract classes can not be instantiated.

Points to Remember about abstract classes and abstract methods

 1. An abstract method must be present in an abstract class only. It should not be present in a non-abstract class.

2. In all the non-abstract subclasses extended from an abstract superclass all the abstract methods must be implemented. An un-implemented abstract method in the subclass is not allowed.

3. Abstract class cannot be instantiated using the new operator.

4. A constructor of an abstract class can be defined and can be invoked by the subclasses.

5. A class that contains abstract method must be abstract but the abstract class may not contain an abstract method. This class is simply used as a base class for defining new subclasses.

6. A subclass can be abstract but the superclass can be concrete. For example the superclass Object is concrete but the subclass class A of it can be abstract.

7. The abstract class cannot be instantiated using new operator but an abstract class can be used as a data type.

Difference between Abstract Class and Concrete Class

Ex. 2.12.1: Create an abstract base class shape with two members base and height, a member function for initialization and a pure virtual function to compute area(). Derive two specific classes triangle and rectangle which override the function area(). Use these classes in a main function and display the area of a triangle and a rectangle.

Sol.:

Test.java

abstract class shape

{

int base,height;

double a;

void initFun()

{

base=5;height=6;

}

abstract void compute_area(); //pure virtual function

}

class triangle extends shape

{

public void compute_area()

{

a=(base*height)/2;

System.out.println("\n Area of triangle is "+a);

}

}

class Rectangle extends shape

{

public void compute_area()

{

a= =(base*height);

System.out.println("\n Area of rectangle is "+a);

}

}

public class Test

{

public static void main(String[] args)

{

triangle obj1=new triangle();

obj1.initFun();

obj1.compute_area();

Rectangle obj2= new Rectangle();

obj2.initFun();

obj2.compute_area();

}

}

Output

D:\test>javac Test.java

D:\test>java Test

Area of triangle is 15.0

Area of rectangle is 30.0

Ex. 2.12.2: What are the conditions to be satisfied while declaring abstract classes?

Sol. Following are the conditions that must be satisfied while declaring the abstract classes –

1. The class name must be preceded by the keyword abstract class.

2. The abstract class must have one abstract method. This abstract method must also be preceded by the keyword abstract and it should not have definition body.

Ex. 2.12.3: Can an abstract class in Java be instantiated? Give the reason.

Sol. The abstract class can not be instatiated (i.e. we can not create the object of this class using new operator) because the abstract class is very much general and less specific. It does nothing and simply lists out only common features of other classes.

Ex. 2.12.4: Write a program to define abstract class, with two methods addition() and subtraction(), addition() is abstract method. Implement the abstract method and call that method using a program(s).

Sol.:

Java Program [AbstractDemo.java]

abstract class A

{

int a = 10;

int b=20;

int c;

abstract void addition(int x, int y);

void subtraction()

{

c=b-a;

System.out.println("Subtraction of 20 and 10: "+c);

}

}

class B extends A

{

void addition(int x, int y)

{

int z=x+y;

System.out.println("addtion function in class B: "+z);

}

}

class C extends A

{

void addition(int x, int y)

{

int z=x+y;

System.out.println("addtion function in class C: "+z);

}

}

public class AbstractDemo

{

public static void main(String[] args)

{

B b=new B();

C c=new C();

System.out.println("----------------------------");

b.addition(10,20);

b.subtraction();

System.out.println("----------------------------");

c.addition(100,200);

c.subtraction();

System.out.println("---------------------------");

}

}

Output

-------------------------------------

addtion function in class B: 30

Subtraction of 20 and 10: 10

--------------------------------------

addtion function in class C: 300

Subtraction of 20 and 10: 10

Review Questions

1. Write briefly on abstract classes with an example.

2. Write in detail about: Abstract classes.

3.What does it mean that a method or class is abstract? Can we make an instance of an abstract class? Explain it with example.

4.What is abstract class? Write a program for abstract example.

5. Explain the concept of abstract class with example.

6.What is an abstract class? Illustrate with an example to demonstrate abstract class.

Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : with Example Java Programs - Abstract Classes