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

Assign 3

The document discusses different types of inheritance in object-oriented programming including single, multilevel, hierarchical, and multiple inheritance. It also covers polymorphism through method overloading and method overriding. Examples are provided to illustrate inheritance and polymorphism in Java.

Uploaded by

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

Assign 3

The document discusses different types of inheritance in object-oriented programming including single, multilevel, hierarchical, and multiple inheritance. It also covers polymorphism through method overloading and method overriding. Examples are provided to illustrate inheritance and polymorphism in Java.

Uploaded by

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

4.

Inheritance – Single, Multilevel, Hierarchical, and


Multiple
Inheritance is the process by which one class inherits the functions and
properties of another class. The main function of inheritance is the
reusability of code. Each subclass only has to define its features. The rest
of the features can be derived directly from the parent class.
Single Inheritance – Refers to a parent-child relationship where a child
class extends the parent class features. Class Y extends Class X.
Multilevel Inheritance – Refers to a parent-child relationship where a child
class extends another child’s class. Class Y extends Class X. Class Z
extends Class Y.
Hierarchical Inheritance – This refers to a parent-child relationship where
several child classes extend one class. Class Y extends Class X, and Class
Z extends Class X.
Multiple Inheritance – Refers to a parent-child relationship where one child
class is extending from two or more parent classes. JAVA does not support
this inheritance.
Example Program of Inheritance in Java
class Animal
{
void habit()
{
System.out.println("I am nocturnal!! ");
}
}

class Mammal extends Animal


{
void nature()
{
System.out.println("I hang upside down!! ");
}
}

class Bat extends Mammal


{
void hobby()
{
System.out.println("I fly !! ");
}
}

public class Inheritance


{
public static void main(String args[])
{
Bat b = new Bat();
b.habit();
b.nature();
b.hobby();
}
}

Output
I am nocturnal!!
I hang upside down!!
I fly !!

5. Polymorphism – Static and Dynamic


It is an object-oriented approach that allows the developer to assign and
perform several actions using a single function. For example, “+” can be
used for addition as well as string concatenation. Static Polymorphism is
based on Method Overloading, and Dynamic Polymorphism is based on
Method Overriding.
Example Program of Static Polymorphism with Method Overloading
Method Overloading
class CubeArea
{
double area(int x)
{
return 6 * x * x;
}
}

class SphereArea
{
double area(int x)
{
return 4 * 3.14 * x * x;
}
}

class CylinderArea
{
double area(int x, int y)
{
return x * y;
}
}

public class Overloading


{
public static void main(String []args)
{
CubeArea ca = new CubeArea();
SphereArea sa = new SphereArea();
CylinderArea cia = new CylinderArea();

System.out.println("Surface area of cube = "+ ca.area(1));


System.out.println("Surface area of sphere= "+ sa.area(2));
System.out.println("Surface area of cylinder= "+ cia.area(3,4));
}
}

Output
Surface area of cube = 6.0
Surface area of sphere= 50.24
Surface area of cylinder= 12.0

Example Program of Dynamic Polymorphism with Method Overriding


class Shape
{
void draw()
{
System.out.println("Your favorite shape");
}

void numberOfSides()
{
System.out.println("side = 0");
}
}

class Square extends Shape


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

void numberOfSides()
{
System.out.println("side = 4 ");
}
}

class Pentagon extends Shape


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

void numberOfSides()
{
System.out.println("side= 5");
}
}

class Hexagon extends Shape


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

void numberOfSides()
{
System.out.println("side = 6 ");
}
}

public class Overriding{

public static void main(String []args){


Square s = new Square();
s.draw();
s.numberOfSides();

Pentagon p = new Pentagon();


p.draw();
p.numberOfSides();

Hexagon h = new Hexagon();


h.draw();
h.numberOfSides();
}
}

Output
SQUARE;
side = 4;
PENTAGON;
side= 5
HEXAGON
side = 6

Applications
Real-time System Design – These systems have complexities that make it
very difficult to code and build from scratch. With the help of object-
oriented techniques, it is possible to handle and build these integrated
complex frameworks.
Object-oriented databases – Instead of RDBMS, these systems use Object-
Oriented Database Management Systems (OODBMS). These databases do
not store data such as numbers and strings. Instead, they store everything
as objects. They have mainly two features – attributes and methods.
Office Automation Systems – It includes all modes of electronic
information sharing, which might be business, personal, formal, informal,
that involves the exchange of messages between people and machines.
Simulation and Modelling – Complex modeling systems involve
understanding and modeling interactions as clearly as possible. Object-
oriented methods provide an alternative and simple method for simulating
such processes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy