You are on page 1of 5

Practical 26

Write a program for hierarchical Inheritance in java.

class e{

public void methodA()


{
System.out.println("Class A");
}
}
class f extends e{
public void methodB()
{
System.out.println("Class B");
}
}
class h extends e{
public void methodC()
{
System.out.println("Class C");
}
}
class p extends e{
public void methodD() {
System.out.println("class D");
}
}
public class Hierarchical {

public static void main(String[] args) {


f obj=new f();
obj.methodB();
obj.methodA();

h obj1=new h();
obj1.methodC();
obj1.methodA();

p obj2=new p();
obj2.methodD();
obj2.methodA();

}}
Practical 28
Write a program for Abstraction using Abstract class in java.
abstract class Shape {
String color;

abstract double area();


public abstract String toString();

public Shape(String color)


{
System.out.println("Shape constructor called");
this.color = color;
}

public String getColor() { return color; }


}
class Circle extends Shape {
double radius;

public Circle(String color, double radius)


{
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}

double area()
{
return Math.PI * Math.pow(radius, 2);
}

@Override public String toString()


{
return "Circle color is " + super.getColor()
+ "and area is : " + area();
}
}
class Rectangle extends Shape {

double length;
double width;

public Rectangle(String color, double length,


double width)
{

super(color);
System.out.println(
.println("Rectangle constructor called");
);
this.length = length
length;
this.width = width
width;
}

double area() { return length * width; }

public String toString()


{
return "Rectangle color is " + super.getColor()
+ "and area is : " + area();
}
}
public class Abstraction {
public static void main(String[] args)
{
Shape s1 = new Circle(
Circle("Red", 2.2);
Shape s2 = new Rectangle(
Rectangle("Yellow", 2, 4);

System.out.println(
.println(s1.toString());
System.out.println(
.println(s2.toString());
}
}
Practical 28
Write a program for Exception Handling class in java.
package ArraysInJava;
import java.util.Scanner;
public class ExcHandling {

public static void main(String[] args) {


Scanner s=new Scanner(System.
Scanner(System.in);
System.out.println(
.println("enter the first no");
int a=s.nextInt();
System.out.println(
.println("enter the second no");
int b=s.nextInt();

try {
int c=a/b;
System.out.println(
.println(c);
}
catch(ArithmeticException
(ArithmeticException e)
{
System.out.println(
.println(e);
}

You might also like