prog 25:
import java.lang.*;
class A
{
int x;
void meth1()
{
System.out.println("method 1");
}
}
class B extends A
{
int y;
void meth2()
{
System.out.println("method 2");
}
}
class demo
{
public static void main(String arg[])
{
B b=new B();
b.x=100;
b.y=200;
System.out.println("x:"+b.x);
System.out.println("y:"+b.y);
b.meth1();
b.meth2();
}
}
o/p:
x:100
y:200
method 1
method 2
prog 26:
import java.lang.*;
class sample
{
private void fun1()
{
System.out.println("fun1 from class sample");
}
public void fun2()
{
System.out.println("fun2 from class sample");
}
void fun3()
{
System.out.println("fun3 from class sample");
}
protected void fun4()
{
System.out.println("fun4 from class sample");
}
}
class subsample extends sample
{
void test()
{
System.out.println("test from class subsample");
}
}
class mainclass
{
public static void main(String arg[])
{
//illegal
//sample s=new sample();
//s.fun1();
//s.fun2();
subsample ss=new subsample();
ss.test();
//ss.fun1();
ss.fun2();
ss.fun3();
ss.fun4():
}
}
o/p
test from class subsample
fun2 from class sample
fun3 from class sample
fun4 from class sample
prog 27:
//Single inheritance
import java.lang.*;
import java.util.*;
class employee
{
private int empno;
private String ename;
protected double sal;
Scanner scan= new Scanner(System.in);
public void setempdetails()
{
System.out.print("enter the empno :");
empno=scan.nextInt();
scan.nextLine();
System.out.print("enter the ename :");
ename=scan.nextLine();
System.out.print("enter the sal :");
sal=scan.nextDouble();
}
public void printempdetails()
{
System.out.print("empno :"+empno);
System.out.print("ename :"+ename);
System.out.print("sal :"+sal);
}
}
class salesemployee extends employee
{
private double comm;
private double nsal;
public void setcomm()
{
Scanner scan1= new Scanner(System.in);
System.out.print("enter comm:");
comm = scan1.nextDouble():
}
public void printcomm()
{
System.out.print("comm:"+comm);
}
public void netsal()
{
nsal=sal+comm;
System.out.print("netsal:"+nsal);
}
}
class samp
{
public static void main(String arg[])
{
employee ep=new employee();
ep.setempdetails();
ep.printempdetails();
salesemployee se= new salesemployee();
se.setempdetails();
se.setcomm();
System.out.println("employee details");
se.printempdetails();
se.printcomm();
se.netsal();
}
}
o/p
enter the empno :10
enter the ename :shankar
enter the sal :100000
empno :10ename :Shankar
sal :100000.0
enter the empno :20
enter the ename :revathy
enter the sal :0
enter comm:10
employee details
empno :20
ename :revathy
sal :0.0
comm:10.0
netsal:10.0
prog 28:
import java.lang.*;
import java.util.*;
class employee
{
private int empno;
private String ename;
double sal;
Scanner scan= new Scanner(System.in);
public void setempdetails()
{
System.out.print("enter the empno :");
empno=scan.nextInt();
scan.nextLine();
System.out.print("enter the ename :");
ename=scan.nextLine();
System.out.print("enter the sal :");
sal=scan.nextDouble();
}
public void printempdetails()
{
System.out.print("empno :"+empno);
System.out.print("ename :"+ename);
System.out.print("sal :"+sal);
//System.out.print("hra:"+hra);
// System.out.print("grosspay:"+gp);
}
}
class salesemployee extends employee
{
private double comm;
public double nsal;
public void setcomm()
{
Scanner scan1= new Scanner(System.in);
System.out.print("enter comm:");
comm = scan1.nextDouble();
}
public void printcomm()
{
System.out.print("comm:"+comm);
}
public void netsal()
{
nsal=sal+comm;
System.out.print("netsal:"+nsal);
}
}
class cal extends salesemployee
{
private double hra;
private double gp;
public void setgp()
{
hra=sal/2;
gp=nsal+hra;
}
public void printgp()
{
System.out.print("hra:"+hra);
System.out.print("grosspay:"+gp);
}
}
class samp
{
public static void main(String arg[])
{
cal se= new cal();
se.setempdetails();
se.setcomm();
System.out.println("employee details");
se.printempdetails();
se.printcomm();
se.netsal();
se.setgp();
se.printgp();
}
}
o/p:
enter the empno :10
enter the ename :sf
enter the sal :200
enter comm:100
employee details
empno :10ename :sfsal :200.0comm:100.0netsal:300.0hra:100.0grosspay:400.