Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Single Inheritance

class A

public void methodA()

System.out.println("Base class method");

class B extends A

public void methodB()

System.out.println("Child class method");

public static void main(String args[])

B obj = new B();

obj.methodA(); //calling super class method

obj.methodB(); //calling local method

}
class Employee{

float salary=40000;

class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println("Programmer salary is:"+p.salary);

System.out.println("Bonus of Programmer is:"+p.bonus);

//float sal = p.salary+p.bonus;

//System.out.println("Salary of Programmer\t"+sal);

Multi-Level Inheritance

Class X

public void methodX()

System.out.println("Class X method");

Class Y extends X

public void methodY()

System.out.println("class Y method");
}

Class Z extends Y

public void methodZ()

System.out.println("class Z method");

public static void main(String args[])

Z obj = new Z();

obj.methodX(); //calling grand parent class method

obj.methodY(); //calling parent class method

obj.methodZ(); //calling local method

//1. Create an abstract class shape. Derive three classes sphere, cone and cylinder from it. Calculate
area and volume of all (use method overriding) [30]

import java.io.*;

abstract class Shape{

protected double s1,s2;

public Shape(double s1, double s2){

this.s1 = s1;

this.s2 = s2;

}
public abstract double calcArea();

public abstract double calcVolume();

class Sphere extends Shape{

public Sphere(double radius){

super(radius,0);

public double calcArea(){

return 4*Math.PI*s1*s1;

public double calcVolume(){

return 4*Math.PI*Math.pow(s1,3)/3;

class Cone extends Shape{

public Cone(double radius, double height){

super(radius,height);

public double calcArea(){

return Math.PI*s1*(s1+

Math.sqrt(s1*s1+s2*s2));
}

public double calcVolume(){

return Math.PI*s1*s1*s2/3;

class Cylinder extends Shape{

public Cylinder(double radius, double height){

super(radius,height);

public double calcArea(){

return 2*Math.PI*s1*(s1+s2);

public double calcVolume(){

return Math.PI*s1*s1*s2;

class ShapeDemo{

public static void main(String args[])

throws Exception{

BufferedReader br = new BufferedReader(

new InputStreamReader(

System.in));

Shape s = null;
double r=0,h=0,l=0,b=0;

while(true){

System.out.print("1.Sphere"+

"\n2.Cone"+

"\n3.Cylinder"+

"\n4.Exit"+

"\nEnter your choice (1-4):");

int ch = Integer.parseInt(br.readLine());

switch(ch){

case 1:

System.out.print("Enter sphere radius:");

r=Double.parseDouble(br.readLine());

s = new Sphere(r);

break;

case 2:

System.out.print("Enter cone radius:");

r=Double.parseDouble(br.readLine());

System.out.print("Enter cone height:");

h=Double.parseDouble(br.readLine());

s = new Cone(r,h);

break;

case 3:

System.out.print("Enter cylinder radius:");

r=Double.parseDouble(br.readLine());

System.out.print("Enter cylinder height:");


h=Double.parseDouble(br.readLine());

s = new Cylinder(r,h);

break;

case 4:

System.exit(0);

System.out.println("Area="+s.calcArea()+

"\nVolume="+s.calcVolume());

You might also like