0% found this document useful (0 votes)
10 views3 pages

Java Inheritance and Polymorphism Examples

Uploaded by

shivangidhama02
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)
10 views3 pages

Java Inheritance and Polymorphism Examples

Uploaded by

shivangidhama02
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

Inheritance

class Circle
{
public double radius;
public double area()
{
return [Link]*radius*radius;
}
}
class Cylinder extends Circle
{
public double height;
public double volume()
{
return area()*height;
}
}

public class Inheritance1


{
public static void main(String args[])
{
Cylinder c=new Cylinder();
[Link]=3.4;
[Link]=1.5;
[Link]("Volume="+[Link]());
}
}

Constructor Inheritance
class Parent
{
public Parent()
{
[Link]("Parent Constructor");
}
}
class Child extends Parent
{
public Child()
{
[Link]("Child Constructor");
}
}

class GrandChild extends Child


{
public GrandChild()
{
[Link]("Grand Child Constructor");
}
}

public class InheritConst


{
public static void main(String[] args)
{
GrandChild c=new GrandChild();
}
}

Polymorphism

Method Overloading
class TestPoly {
public int max(int a, int b) {
if(a > b)
return a;
else
return b;
}
public int max(int a, int b, int c) {
if(a > b) {
if(a > c)
return a;
else
return c;
} else {
if(b > c)
return b;
else
return c;
}
}
}
class OverloadingTest {
public static void main(String[] args) {
TestPoly ref = new TestPoly();
[Link]([Link](2, 3));
[Link]([Link](2, 3, 5));
}
}

Method Overriding

class Super
{
public void display()
{
[Link]("Super Class");
}
}
class SubClass extends Super
{
public void display()
{
[Link]("Sub Class");
}
}

class OverRidingTest {
public static void main(String[] args) {
SubClass ref = new SubClass();
[Link]();

}
}

You might also like