31 Program to Demonstrate Static Variables
class Test
{
static int count;
static
{
System.out.println("Welcome");
count=0;
}
Test()
{
count++;
}
}
class Teststaticvariable
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
System.out.println(Test.count + " objects are created");
}
}
OUTPUT :
Welcome
3 objects are created
class Mymath
{
static int add(int x,int y)
{
return(x+y);
}
static int subtract(int x,int y)
{
return (x-y);
}
static int square(int x)
{
return (x*x);}
}
class demostaticmethod
{
public static void main(String args[])
{
System.out.println(Mymath.add(100,100));
System.out.println(Mymath.subtract(200,100));
System.out.println(Mymath.square(5));
}
}
OUTPUT :
200
100
25
33 Program to demonstrate an Inner class
class Outer
{
int outer_x=100;
void test()
{
Inner inner=new Inner();
inner.display();
}
class Inner
{
void display()
{
System.out.println("display: outer_x= "+outer_x);
}
}
}
class InnerClassDemo
{
public static void main(String args[])
{
Outer outer=new Outer();
outer.test();
}
}
OUTPUT :
display: outer_x= 100
34 Program to Demonstrate Type Wrapping
class Wrap {
public static void main(String args[])
{
Integer obj = new Integer(34);
int x = obj.intValue();
System.out.println("x = " +x);
System.out.println("obj = " +obj);
}
}
OUTPUT :
x = 34
obj = 34
35 Program to Demonstrate Single Inheritance
class A
{
int i,j;
void showij()
{
System.out.println("i and j : " +i+ " " +j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k : "+k);
}
void sum()
{
System.out.println("i+j+k: "+(i+j+k));
}
}
class SimpleInheritance
{
public static void main(String args[])
{
A superob = new A();
B subob = new B();
superob.i=10;
superob.j=20;
System.out.println("Contents of superob: ");
superob.showij();
System.out.println();
subob.i=7;
subob.j=8;
subob.k=9;
System.out.println("Contents of subob: ");
subob.showij();
System.out.println();
System.out.println("Sum of i,j & k in subob: ");
subob.sum();
}
}
OUTPUT :
Contents of superob:
i and j : 10 20
Contents of subob:
i and j : 7 8
Sum of i,j & k in subob:
i+j+k: 24
36 Program to Demonstrate Multilevel Inheritance
class Student
{
int rollno;
void getroll(int a)
{
rollno=a;
}
void putroll()
{
System.out.println("Roll number : "+rollno);
}
}
class Test extends Student //first level inheritence
{
int marks1,marks2;
void getmarks(int x,int y)
{
marks1=x;
marks2=y;
}
void putmarks()
{
System.out.println("Marks in subject1 = "+marks1);
System.out.println("Marks in subject2 = "+marks2);
}
}
class Result extends Test //second level derivation
{
float total;
void display()
{
total=marks1+marks2;
putroll();
putmarks();
System.out.println("Total = "+total);
}
}
class Multilevel
{
public static void main(String args[])
{
Result student1 = new Result(); //object of class result is created
student1.getroll(34);
student1.getmarks(40,99);
student1.display();
}
}
OUTPUT :
Roll number : 34
Marks in subject1 = 40
Marks in subject2 = 99
Total = 139.0
37 Program to Demonstrate hierarchical Inheritance
class Student
{
int roll;
void getroll(int x)
{
roll=x;
}
void putroll()
{
System.out.println(roll);
}
}
class Result1 extends Student
{
int marks1;
void getmarks1(int y)
{
marks1=y;
}
void showmarks1()
{
System.out.print("Roll no of 1st student is = ");
putroll();
System.out.println("Marks of 1st student is = "+marks1);
}
}
class Result2 extends Student {
int marks2;
void getmarks2(int z)
{
marks2=z;
}
void showmarks2()
{
System.out.print("Rollno of 2nd student is = ");
putroll();
System.out.println("Marks of 2nd student is = "+marks2);
} }
class Hierarchial
{
public static void main(String args[])
{
Result1 r1 = new Result1();
Result2 r2 = new Result2();;
r1.getroll(36);
r1.getmarks1(99);
r1.showmarks1();
r2.getroll(34);
r2.getmarks2(40);
r2.showmarks2();
}
}
OUTPUT :
Roll no of 1st student is = 36
Marks of 1st student is = 99
Rollno of 2nd student is = 34
Marks of 2nd student is = 40
38 Program to Demonstrate SUPER Class variable to reference a sub class object
class A {
int i,j;
A(int a,int b) {
i=a;
j=b;
}
void show() {
System.out.println("i and j: " +i + " " +j);
}}
class B extends A {
int k;
B(int a,int b,int c) {
super(a,b);
k=c; }
void show() {
System.out.println("k: "+k);
}}
class Supref {
public static void main(String args[]) {
B subOb=new B(1,2,3);
A a;
a=subOb;
System.out.println("Using Sub Class Object :");
subOb.show();
System.out.println("Using Super class Variable : ");
a.show();
}}
OUTPUT :
Using Sub Class Object :
k: 3
Using Super class Variable :
k: 3
39 Program to Demonstrate Method Overriding
class A {
int i,j;
A(int a,int b)
{
i=a;
j=b;
}
void show() {
System.out.println("i and j: " +i + " " +j);
}}
class B extends A {
int k;
B(int a,int b,int c)
{
super(a,b);
k=c;
}
void show()
{
System.out.println("k: "+k);
}}
class Override {
public static void main(String args[])
{
B subOb=new B(1,2,3);
subOb.show();
}
}
OUTPUT :
k: 3
40 Program to Demonstrate Runtime Polymorphism
class figure
{
double dim1;
double dim2;
figure(double a,double b)
{
dim1=a;
dim2=b;
}
double area()
{
System.out.println("area for figure is undefined");
return 0;
}
}
class rectangle extends figure
{
rectangle(double a,double b)
{
super(a,b);
}
double area()
{
System.out.println("Inside Area for rectangle");
return dim1*dim2;
}
}
class triangle extends figure {
triangle(double a,double b)
{
super(a,b);
}
double area()
{
System.out.println("Inside area for triangle");
return (dim1*dim2)/2;
}}
class FindAreas {
public static void main(String args[]) {
figure f=new figure(10,10);
rectangle r=new rectangle(9,5);
triangle t=new triangle(10,8);
figure figref;
figref=r;
System.out.println("Area is " +figref.area());
figref=t;
System.out.println("Area is " +figref.area());
figref=f;
System.out.println("Area is " +figref.area());
}}
OUTPUT :
Inside Area for rectangle
Area is 45.0
Inside area for triangle
Area is 40.0
area for figure is undefined
Area is 0.0
0 comments:
Post a Comment