Welcome To Uday Satya Blog

41-50 PROGRAMS



41  Program to Demonstrate Abstract Class

abstract class A
{
abstract void callme();
void callmetoo(){
System.out.println("this is a concrete method.");
}}
class B extends A
{
void callme()
{
System.out.println("B's implementation of callme.");
}}
class AbstractDemo
{
public static void main(String args[]) {
B b=new B();
b.callme();
b.callmetoo();
}
}

OUTPUT :

B's implementation of callme.
this is a concrete method.


42  Program to Demonstrate FINAL Keyword

 class A
 {
final int x =3;
int y;
A()
 {
//x=4;this results in error
y=4;
}
}
 class Finaldemo {
public static void main(String args[]) {
A a = new A();
System.out.println("x = " +a.x);
System.out.println("y = " +a.y);
//a.x=5;this also results in error
a.y=6;
System.out.println("x = " +a.x);
System.out.println("y = " +a.y);
}
}

OUTPUT :

x = 3
y = 4
x = 3
y = 6


43  Program to Demonstrate FINAL Method

class A  {
int i,j;
A(int a,int b) {
i=a;
j=b;
}
void show() {
System.out.println("i and j: " +i + " " +j);
}
final void display() //Cannot be Overriden
{
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 Finalmeth {
public static void main(String args[])
{
B subOb=new B(1,2,3);
System.out.println("Overriden Method call :");
subOb.show();
System.out.println("Final Method call :");
subOb.display();
}}

OUTPUT :

Overriden Method call :
k: 3
Final Method call :
i and j: 1 2

44  Program to Demonstrate FINAL Class

final class Democlass //Cannot be inherited
{
int a=34;
void showa()
{
System.out.println("a = " +a);
}
} 
class A
{
public static void main(String args[])
 {
Democlass obj = new Democlass();
obj.showa();
}
}

OUTPUT :

a=34


45  Program to Demonstrate INTERFACE

 interface Sum // defining Interface
{
int z=2;//final variable declared
int add();//Abstract Method declared
}
class A implements Sum // Class implementing Interface
{
int x,y;
public int add() {
return x+y+z;
}
}
class InterfaceDemo
 {
public static void main(String args[])
 {
A a = new A();
a.x=14;
a.y=18;
System.out.println("Sum : " +a.add());
}
}

OUTPUT :

Sum : 34

 46  Program to access implementation through Interface references

interface figure
{
double area();
}
class rectangle implements figure
{
double dim1,dim2;
rectangle(double a,double b)
{
dim1=a;dim2=b;
}
public double area()
{
return dim1*dim2;
}
}
class triangle implements figure
{
double dim1,dim2;
triangle(double a,double b)
{
dim1=a;dim2=b;
}
public double area()
{
return (dim1*dim2)/2;
}
}//implements through interface
 class demo
{
public static void main(String args[])
{
rectangle r=new rectangle(100,50);
triangle t=new triangle(10,20);
figure f;
f=r;
System.out.println(f.area());
f=t;
System.out.println(f.area());
}
}

OUTPUT :

5000.0
100.0


47  Program to Demonstrate EXTENDING INTERFACE

interface A {
void meth1();
void meth2();
}
interface B extends A {
void meth3();
}
class Myclass implements B {
public void meth1() {
System.out.println("method1");
}
public void meth2() {
System.out.println("method2");
}
public void meth3() {
System.out.println("method3");
}}
class Extint {
public static void main(String args[]) {
Myclass m = new Myclass();
m.meth1();
m.meth2();
m.meth3();
}}

Output :

method1
method2
method3

48  Program to Demonstrate Stack using interface

interface stack
{
void push(int item);
int pop();
}
class A implements stack
{
int s[];
int tos;
A(int size)
{
s = new int[size];
tos = -1;
}
public void push(int item)
{
if(tos == s.length-1)
System.out.println("STACK is FULL");
else
s[++tos]=item;
}
public int pop()
{
if(tos<0)
{
System.out.println("STACK IS EMPTY");
return 0;
}
else
return s[tos--];
}}
class S {
public static void main(String args[]){
int i;
A stack1 = new A(5);
for(i=0;i<5;i++)
stack1.push(i);
System.out.println("Stack elements are : ");
for(i =0;i<5;i++)
System.out.println(stack1.pop());
}
}

OUTPUT :

Stack elements are :
4
3
2
1
0

49  Program to Demonstrate Queue using Interface

interface queue
{
void push(int item);
int pop();
}
class B implements queue
{
int s[];
int f,l;
B(int size)
{
s = new int[size];
f = -1;
l = -1;
}
public void push(int item)
{
if(f == s.length-1)
System.out.println("Q is FULL");
else
s[++f]=item;
}
public int pop()
{
if(f==l)
{
System.out.println(" Q iS EMPTY");
return 0;
}
else
return s[++l];
}}
class Q
 {
public static void main(String args[]){
int i;
B q1=new B(8);
for(i=0;i<8;i++)
q1.push(i);
System.out.println("Queue elements are : ");
for(i =0;i<8;i++)
System.out.println(q1.pop());
}
}


OUTPUT :

Queue elements are :
0
1
2
3
4
5
6
7


50  Program to Demonstrate a SIMPLE PACKAGE

package MyPack;
public class Balance {
String name;
double bal;
public Balance(string n,double b) {
name=n;
bal=b;
}
public void show() {
if(bal<0)
System.out.println("-->");
System.out.println(name+ ": $"+bal);
}}

--------------------------------------------------------------------------------------------------------

import MyPack.*;
class TestBalance
{
public static void main(string args[])
{
BalanceTest=new Balance("Uday Satya",500);
test.show();
}
}

OUTPUT :

Uday Satya : $500

0 comments:

Post a Comment