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

Java Codes: Write A Program To Implement Polymorphism, Inheritance Using Methods in Java

The document contains two Java code examples demonstrating polymorphism, inheritance, and interfaces. The first example shows a parent and child class with an overridden method to demonstrate polymorphism. The second example implements an interface in a class and calls a method from another package to demonstrate packages and interfaces.

Uploaded by

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

Java Codes: Write A Program To Implement Polymorphism, Inheritance Using Methods in Java

The document contains two Java code examples demonstrating polymorphism, inheritance, and interfaces. The first example shows a parent and child class with an overridden method to demonstrate polymorphism. The second example implements an interface in a class and calls a method from another package to demonstrate packages and interfaces.

Uploaded by

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

JAVA CODES

Write a program to implement polymorphism, inheritance using methods in Java.

class X
{
X()
{
System.out.println(Inside super class parameterized constructor);
fun();
}
void fun()
{
System.out.println(Parent);
}
}
class Y extends X
{
Y()
{
System.out.println(Inside child class parameterized constructor);
}
void fun()
{
System.out.println(Child);
}
}
class Demo extends Y
{
public static void main(String args[])
{
Y d=new Y();
d.fun();
}
}
Write a program to implement packages and interface in Java

package p1;
public class A
{
public void fact(int x)
{
int i, f=1;
for(i=x;i>1;i--);
f=f*i;
System.out.println(The factorial is:+f);
}
}
interface number
{
final int n=5;
void show();
)
import p1.*;
public class B implements number
{
void show()
{
fact(n);
}
public void main(String args[])
{
B ab=new B();
ab.show();
}
}

You might also like