Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SUBJECT: Java TOPIC: Abstract classes

WRITTEN BY: SRINIVAS YADLAPATY, MTech,(PHD) in Computer


Science Engineering
---------------------------------------------------------------------------------
Abstract class:
A class with 0 or more abstract methods is called as an ‘abstract class’.
Example:
// abstract class
abstract class Car
{
….
….
}
Abstract method:
A method without body is an abstract method.
Example:
// abstract class
abstract class Car
{
// abstract methods
abstract void steering();
abstract void braking();
}
Concrete method:
A method with body is a concrete method.
Example:
// abstract class
abstract class Car
{
// abstract methods
abstract void steering();
abstract void braking();
// concrete method
void fillTank()
{
System.out.println(“Open the lid and fill with fuel.”);
}
}
An abstract class will have complete methods, instance variables, constructors
and abstract methods also.
An abstract class and abstract method should be declared as ‘abstract’.
We cannot create an object to abstract class but, we can create a reference
variable to abstract class.
Do: Car c;
Donot: Car c=new Car();
All the abstract methods of the abstract class should be implemented in its sub
classes.
If any method is not implemented, then that sub class should be declared as
‘abstract’.
Abstract class reference variable can be used to refer to the objects of its sub
classes.
A class cannot be both abstract and final.
abstract final class A------>invalid
final abstract class A------>invalid
Important interview question:
Q. Can we refer to individual methods of sub classes?
A. Abstract class reference cannot refer to the individual methods of sub classes.
Program:
// Application on abstract class.
abstract class Car
{
int regno;
Car(int regno)
{
this.regno=regno;
}
void fillTank()
{
System.out.println("Open the lid and fill with fuel.");
}
abstract void steering();
abstract void braking();
}
Save as: Car.java
Compile: javac Car.java
// Sub class-Maruthi
class Maruthi extends Car
{
Maruthi(int regno)
{
super(regno);
System.out.println("Reg no:"+regno);
}
void steering()
{
System.out.println("Maruthi car has manual steering. Take the key and start the
car.");
}
void braking()
{
System.out.println("Maruthi car has hydraulic braking. Apply brakes to stop the
car.");
}
}
Save as: Maruthi.java
Compile: javac Maruthi.java
// Sub class-Santro
class Santro extends Car
{
Santro(int regno)
{
super(regno);
System.out.println("Reg no:"+regno);
}
void steering()
{
System.out.println("Santro car has power steering. Take the key and start the
car.");
}
void braking()
{
System.out.println("Santro car has gas braking. Apply brakes to stop the car.");
}
}
Save as: Santro.java
Compile: javac Santro.java
// Executable class.
class UseCar
{
public static void main(String args[])
{
Maruthi m=new Maruthi(9999);
Santro s=new Santro(7777);
Car c;
c=m;
c.fillTank();
c.steering();
c.braking();
}
}
Save as: UseCar.java
Compile: javac UseCar.java
Run: java UseCar
Compile: javac UseCar.java
Run: java UseCar
Output:
Reg no:9999
Reg no:7777
Open the lid and fill with fuel.
Maruthi car has manual steering. Take the key and start the car.
Maruthi car has hydraulic braking. Apply brakes to stop the car.
Important interview questions:
1. Abstract class must have only abstract methods. True or false?
A. False. Abstract methods can also have concrete methods.
2. Is it compulsory for a class which is declared as abstract to have at least one
abstract method?
A. Not necessarily. Abstract class may or may not have abstract methods.
3. Can we use “abstract” keyword with constructor, Instance Initialization
Block and Static Initialization Block?
A. No. Constructor, Static Initialization Block, Instance Initialization Block and
variables can not be abstract.
4. Why final and abstract can not be used at a time?
A. Because, final and abstract are totally opposite in nature. A final class or
method can not be modified further where as abstract class or method
must be modified further. “final” keyword is used to denote that a class or
method does not need further improvements. “abstract” keyword is used
to denote that a class or method needs further improvements.
5. Can we instantiate a class which does not have even a single abstract
methods but declared as abstract?
A. No, We can’t instantiate a class once it is declared as abstract even though
it does not have abstract methods.
6. Can we declare abstract methods as private? Justify your answer?
A. No. Abstract methods can not be private. If abstract methods are allowed
to be private, then they will not be inherited to sub class and will not get
enhanced.
7. We can’t instantiate an abstract class. Then why constructors are allowed in
abstract class?
A. It is because, we can’t create objects to abstract classes but we can create
objects to their sub classes. From sub class constructor, there will be an
implicit call to super class constructor. That’s why abstract classes should
have constructors. Even if you don’t write constructor for your abstract
class, compiler will keep default constructor.
8. Can we declare abstract methods as static?
A. No, abstract methods can not be static.

You might also like