Jaypee Institute of Information Technology
Object Oriented Programming Using Java(24B15CS215)
Lab Assignment -5
Experiment 5: Abstract Class, Interface - Part (I)
Abstract Class: A class that cannot be instantiated and may contain abstract
(unimplemented) methods. It is used when common base functionality is shared,
but some methods must be implemented by subclasses. It includes
o Abstract and non-abstract methods
o Constructors
o Static methods
o Instance variables
Example
abstract class Shape {
double dimension;
abstract double area(); // Abstract method
void setDimension(double d) { this.dimension = d; }
}
Interface: A contract that a class agrees to follow. All methods are implicitly public and
abstract (until Java 8+ allowed default and static). It is used for multiple inheritance and
type-based design.
Example
interface Drawable {
void draw();
}
class Circle implements Drawable
{
public void draw()
{
System.out.println("Drawing circle");
}
}
Program 1: Create a package of shapes with an abstract class Shape that has an abstract
method calculateArea(). Implement classes like Circle and Rectangle that inherit from Shape
and implement the method. Use a Printable interface to print the details.
Program 2: Create an abstract class Employee with a method calculateSalary(). Create
classes FullTimeEmployee and PartTimeEmployee to implement logic. Use the interface
Displayable for showing details.
Program 3: Design an abstract class BankAccount with an abstract method withdraw() and a
concrete method deposit(). Use the interface Taxable to calculate service tax.