Module 4&5
Module 4&5
package b;
import a.*;
class diff
{
int m,n;
void disp(int x,int y)
{
m=x;
n=y;
int k = m-n;
System.out.println("differnce is "+ k);
}
public static void main(String args[])
{
diff obj2= new diff();
obj2.disp(7,2);
mult obj3=new mult();
obj3.display(9,3);
}
}
Output:
C:\Users\Admin\Desktop\java>javac -d . diff.java
C:\Users\Admin\Desktop\java>java b.diff
differnce is 5
result12
Output:
C:\Users\Admin\Desktop\java>javac a.java
C:\Users\Admin\Desktop\java>java a
sum is 15
product is 16.0
d. Develop a java program to implement the concept of method overriding.
Program:
class A
{
static int a;
void sum(int x, int y)
{
a=x+y;
System.out.println(" parent sum is " +a);
}
}
class b extends A
{
static int b;
void sum(int x, int y)
{
b=x+y;
System.out.println(" child sum is " +a);
}
public static void main(String args[])
{
b obj = new b();
obj.sum(8,9);
}
Output:
C:\Users\Admin\Desktop\java>javac b.java
C:\Users\Admin\Desktop\java>java b
child sum is 0
Module 5:
a) develop a java program for Abstract class to find areas of different shapes.
Program:
abstract class shape
{ abstract void area();
}
Output:
oem@INDIA:~/user$ javac a.java
oem@INDIA:~/user$ java a
area of triangle is 36.0
area of triangle is 72
area of triangle is 64
Program:
interface vehicle
{
void specification();
void display();
}
class twowheeler implements vehicle
{
public void specification()
{
System.out.println("herohonda");
}
public void display()
{
System.out.println("price is 60000");
}
}
class fourwheeler implements vehicle
{
public void specification()
{
System.out.println("bmw");
}
public void display()
{
System.out.println("price is 160000");
}
}
class a
{
public static void main(String args[])
{
twowheeler o =new twowheeler();
o.specification();
o.display();
fourwheeler o2 =new fourwheeler();
o2.specification();
o2.display();
}
output
oem@INDIA:~/user$ javac a.java
oem@INDIA:~/user$ java a
herohonda
price is 60000
bmw
price is 160000