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

Software Engg Lab 4

Uploaded by

scrpionff332005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Software Engg Lab 4

Uploaded by

scrpionff332005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Implementing inheritance and polymorphism in a software project.

Implementing inheritance and polymorphism in a software project can be a powerful way to structure your
code, improve reusability, and maintain flexibility. Let’s go over both concepts and create an example.
Inheritance:
Inheritance allows a class (called the child class or subclass) to inherit attributes and methods from another
class (called the parent class or superclass). This enables code reuse and the extension of functionality without
rewriting everything.

Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It also
allows methods to be overridden so that different classes can implement methods in a way specific to their
needs, even though they share the same name.

In this Experiment we are implementing Inheritance and polymorphism in a single program.

Algorithm for the program :

Step 1 : start
Step 2 : Define the Base Class: Shape
Step 2.1: Create a class named Shape with a string variable name.
Step 2.2: Define a constructor for Shape that initializes the name.
Step 2.3: Define a method area() that returns 0 and prints a message indicating that this method should be
Overridden.
Step 2.4: Define a method printName() that prints the name of the shape.
Step 3 : Define the Derived Class: Circle:
Step 3.1: Create a class named Circle that extends Shape.
Step 3.2: Add a double variable radius to store the radius of the circle.
Step 3.3: Define a constructor for Circle that calls the Shape constructor with the name "Circle" and
initializes the radius.
Step 3.4: Override the area() method to calculate and return the area of the circle using the formula: π *
radius^2.
Step 4 : Define the Derived Class: Rectangle
Step 4.1: Create a class named Rectangle that extends Shape.
Step 4.2: Add double variables length and width to store the dimensions of the rectangle.
Step 4.3: Define a constructor for Rectangle that calls the Shape constructor with the name "Rectangle"
and initializes length and width.
Step 4.4: Override the area() method to calculate and return the area of the rectangle using the formula:
length * width.
Step 5: Define the Derived Class: Square:
Step 5.1: Create a class named Square that extends Rectangle.
Step 5.2: Define a constructor for Square that calls the Rectangle constructor with equal values for
length and width.
Step 5.3: Set the name of the shape to "Square".
Step 6 : Create main class:
Step 6.1: Instantiate Circle, Rectangle, and Square objects.
Step 6.2: Call printName() and area() for each object.
Step 7 : Display output: Name and area for each shape.
Step 8 : End.

Flowchart of inheritance and polymorphism

start

Define Base Class (Shape)

Define subclass (Circle)

Define Subclass (Rectangle)

Define Subclass (Square)

Define Main class

Invoke methods for each object

End
We are building a simple example to model different shapes and calculate their areas. We’ll use inheritance to
create a base Shape class and polymorphism to define how different shapes (like Circle and Rectangle) calculate
their area.
We are writing this program in Notepad and compiling and running part of the program is done in command
prompt with the help of JDK(Java Development Kit) software.
This is the complete code without any errors.
To get the output we Write the following commands in the command prompt :
javac Main.java
To compile this source code into bytecode, which the Java Virtual Machine (JVM) can execute.
java Main
To actually run the compiled bytecode.

The output of the code is:

You might also like