Java Programs
-
Upload
akanksha-gupta -
Category
Documents
-
view
10 -
download
3
description
Transcript of Java Programs
Program-16 Date: 30-08-12
Aim:- WAP that shows passing object as a parameter
import java.io.*;class abc{
void prn() {
System.out.println("this is class abc"); }}class pass{
static abc obj=new abc(); public static void main(String args[]) {
prnp(obj); }
static void prnp(abc obj) { System.out.println("this is class containing main");
obj.prn(); }}
OUTPUT :
Program-17 Date: 30-08-12
Aim:- WAP to display Simple Inheritance
import java.io.*;class suprcls{ void prn()
{ System.out.println("this is super class");
}}class subcls extends suprcls{
public static void main(String args[]) {
subcls obj=new subcls(); System.out.println("sub class calling function of super class"); obj.prn();
} }
OUTPUT :
Program-18 Date: 30-08-12
Aim:- WAP to display method Overriding
import java.io.*;class suprcls{
void prn() { System.out.println("this is super class");
}}class subcls extends suprcls{
public static void main(String args[]) {
subcls obj=new subcls(); System.out.println("sub class calling function of super class"); obj.prn();
} void prn() {
System.out.println("this is sub class"); }
}
OUTPUT :
Program-19 Date: 30-08-12
Aim:- WAP that illustrates the use of “Super” keyword.
import java.io.*;class supercls{
void prn() {
System.out.println("this is super class"); }
}class subclass extends supercls{ public static void main(String args[])
{ subclass obj=new subclass(); System.out.println("sub class calling function of super class"); obj.prn();
} void prn()
{ System.out.println("this is sub class"); super.prn();
}}
OUTPUT :
Program-20 Date: 30-08-12
Aim:- Create an abstract class “Shape” let rectangle and triangle inherit this Shape class. Add necessary function to it.import java.io.*;abstract class shape{ int a=5,b=6; void prn()
{ System.out.println(" calculating area...."); }}class rectangle extends shape{ void area() { prn();
System.out.println("rectangle : "+(a*b)); }}class triangle extends shape{ void area() { prn(); System.out.println("triangle : "+(0.5*a*b)); }}class abstrct{ public static void main(String args[]) {
rectangle rec=new rectangle(); rec.area(); triangle tri=new triangle(); tri.area();}
}OUTPUT
Program-21 Date: 30-08-12
Aim: WAP illustrating a Super class variable , a refrence as a subclass object.
import java.io.*;class suprcls{ void prn() {
System.out.println("this is super class"); }
}class subcls extends suprcls{
public static void main(String args[]) {
subcls obj=new subcls(); System.out.println("sub class calling function of super class"); obj.prn();
} void prn() {
System.out.println("this is sub class"); super.prn();
}}
OUTPUT :