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

Java Assignment

The document contains two Java programs demonstrating polymorphism, inheritance, packages, and interfaces. The first program shows polymorphism and inheritance by creating classes X and Y, with Y extending X and overriding the fun() method to print "Child" instead of "Parent". It creates an object of class Y and calls fun(), displaying "Child". The second program implements a package named p1 containing class A with a fact() method. It defines an interface number with a constant and show() method. Class B implements the interface and calls the fact() method from show() when an object is created.

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)
34 views

Java Assignment

The document contains two Java programs demonstrating polymorphism, inheritance, packages, and interfaces. The first program shows polymorphism and inheritance by creating classes X and Y, with Y extending X and overriding the fun() method to print "Child" instead of "Parent". It creates an object of class Y and calls fun(), displaying "Child". The second program implements a package named p1 containing class A with a fact() method. It defines an interface number with a constant and show() method. Class B implements the interface and calls the fact() method from show() when an object is created.

Uploaded by

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

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();
}
}

OUTPUT

Inside super class parameterized constructor


Parent
Inside child class parameterized constructor
Child
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