You are on page 1of 4

Experiment No.

:10
Aim: To Write Java Program on Abstract class and abstract methods.

Input Specification: string


Output Specification:. string

Objectives:-Student/we will learn to use the abstract class and abstract methods in the java
program.

.
Theory:

Abstract class in Java

A class which is declared as abstract is known as an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

o An abstract class must be declared with an abstract keyword.


o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body of the
method.

Example of abstract class


abstract class A
{}

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an
abstract method.

Example of abstract method


abstract void printStatus(); //no method body and abstract
logic of the program.
In this program , super class Bike is created and defined as abstract. Bike() constructor is
defined. Changegear() method is defined .rn() method is declared as abstract.subclass Honda is
extended from Bike class . run() method is defined completely in this class.In main method class
twowheeler ,object of Bike class is created.run() and Changegear() method is called for this
object.

Algorithm:

Step 1: Start

Step 2: Create a abstract class named Bike.

Step 3: Define Changegear() method..

Step 4: Declare run() method as abstract.

Step 5: create subclass Honda extending Bike class.

Step 6: Define run() method

Step 7.Create a main method class twowheeler Create object of Bike class. Call run() and

Changegear() methods for this object.

Step 8: Stop.
Program(Code):

// program no.11 : program on Abstract class and Abstract method.

abstract class Bike


{
Bike()
{
System.out.println("bike is created");
}
abstract void run();
void changeGear()
{
System.out.println("gear changed");
}
}

//Creating a subclass class which inherits Abstract class


class Honda extends Bike
{
void run()
{
System.out.println("running safely..");
}
}

//Creating a twowheeler class which calls abstract and non-abstract methods

class twowheeler
{
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}

/*

Output:

D:\programs>javac twowheeler.java
D:\programs>java twowheeler
bike is created
running safely..
gear changed

D:\programs>

*/

Outcome: with this we/student will able to create user defined package and import it in the java
program.

You might also like