0% found this document useful (0 votes)
25 views

Week 6 Java Lab Program

The document describes a Java program that implements runtime polymorphism using inheritance. It defines a base Shape class and subclasses for Rectangle, Circle, and Triangle. The main method creates instances of each subclass and calls the draw method on the parent Shape reference, displaying the correct output for each subclass. It also shows a program that implements multi-level inheritance with classes Shape, Rectangle extending Shape, and Cube extending Rectangle, demonstrating inheritance through multiple levels.

Uploaded by

manasyogi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Week 6 Java Lab Program

The document describes a Java program that implements runtime polymorphism using inheritance. It defines a base Shape class and subclasses for Rectangle, Circle, and Triangle. The main method creates instances of each subclass and calls the draw method on the parent Shape reference, displaying the correct output for each subclass. It also shows a program that implements multi-level inheritance with classes Shape, Rectangle extending Shape, and Cube extending Rectangle, demonstrating inheritance through multiple levels.

Uploaded by

manasyogi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

WEEK 6

AIM: a) Write a java program that implements runtime polymorphism


using inheritance.

PROGRAM:

class Shape{

void draw( ){System.out.println("drawing...");}

class Rectangle extends Shape{

void draw(){System.out.println("drawing rectangle...");}

class Circle extends Shape{

void draw(){System.out.println("drawing circle...");}

class Triangle extends Shape{

void draw(){System.out.println("drawing triangle...");}

class TestPolymorphism2{

public static void main(String args[ ])

Shape s;

s=new Rectangle( );

s.draw();

s=new Circle();

s.draw();

s=new Triangle( );
s.draw();

OUTPUT:

drawing rectangle...

drawing circle...

drawing triangle...

WEEK 6

AIM: b) Write a java program to implement multi-level inheritance.

PROGRAM:

class Shape {

public void display() {

System.out.println("Inside display");

class Rectangle extends Shape {

public void area() {

System.out.println("Inside area");

class Cube extends Rectangle {


public void volume() {

System.out.println("Inside volume");

public class Tester {

public static void main(String[ ] arguments) {

Cube cube = new Cube();

cube.display();

cube.area();

cube.volume();

OUTPUT:

Inside display

Inside area

Inside volume

You might also like