Professional Documents
Culture Documents
COLLEGE
Lab Assignment 2
5. Create a class named as ‘A’ and create a subclass ‘B’ which is extends from
class ‘A’.
Ans:
Code:
class A {
int x, y;
void showxy() {
System.out.println("x and y: " + x + " " + y);
}
}
void showz() {
System.out.println("z: " + z);
}
void sum() {
System.out.println("x+y+z: " + (x + y + z));
}
}
Output:
Contents of superOb:
x and y: 10 20
Contents of subOb:
x and y: 7 8
z: 9
6. Write a program to give the example for ‘super’ keyword and ‘this keyword’.
Ans:
Code:
import java.util.*;
class Box {
private double length;
private double breadth;
private double height;
Box() {
Box(Box B) {
this.length = B.length;
this.breadth = B.breadth;
this.height = B.height;
}
double getVolume() {
return length * breadth * height;
}
void showBox() {
System.out.println("Length = " + length);
System.out.println("Breadth = " + breadth);
System.out.println("Height = " + height);
}
}
BoxWeight() {
super();
}
BoxWeight(BoxWeight BW) {
super(BW);
this.weight = BW.weight;
}
void showBoxDetails() {
showBox();
System.out.println("Weight = " + weight);
}
}
Output:
Ans:
Code:
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
Output:
Ans:
Code:
class Shape
{
public void draw()
{
System.out.println("A Shape is drawn");
}
public void erase()
{
System.out.println("Shape erased");
}
}
class Circle extends Shape
{
public void draw()
{
System.out.println("A circle is drawn.");
}
public void erase()
{
System.out.println("Circle erased.");
}
}
class Triangle extends Shape
{
public void draw()
{
System.out.println("A triangle is drawn.");
}
public void erase()
{
System.out.println("Triangle erased.");
}
}
class Square extends Shape
{
public void draw()
{
System.out.println("A Square is drawn.");
}
public void erase()
{
System.out.println("Square erased.");
}
}
class ShapeMain
{
public static void main(String[] args) {
Shape defaultShape= new Shape();
Shape circle1= new Circle();
Shape triangle1= new Circle();
Shape square1= new Circle();
defaultShape.draw();
defaultShape.erase();
circle1.draw();
circle1.erase();
triangle1.draw();
triangle1.erase();
square1.draw();
square1.erase();
}
}
Output:
A Shape is drawn
Shape erased
A circle is drawn.
Circle erased.
A circle is drawn.
Circle erased.
A circle is drawn.
Circle erased.
Ans:
Code:
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Explanation:
In this example we have three classes – Car, Maruti and Maruti800. We have done a
setup – class Maruti extends Car and class Maruti800 extends Maruti. With the help of
this Multilevel hierarchy setup our Maruti800 class is able to use the methods of both
the classes (Car and Maruti).
Output:
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
Code:
// Class 1
// Abstract class
abstract class Base {
abstract void fun();
}
// Class 2
class Derived extends Base {
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class Main {
Output:
// Class 1
// Abstract class
abstract class Base {
// Constructor of class 1
Base()
{
// Print statement
System.out.println("Base Constructor Called");
}
// Class 2
class Derived extends Base {
// Constructor of class2
Derived()
{
System.out.println("Derived Constructor Called");
}
// Method of class2
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class Main {
Output:
Base Constructor Called
Derived Constructor Called
11. Write a program in Java to implement dynamic method dispatch concept.
Ans:
Code:
class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
Output:
12. Write a program to create a Interface Flying which contain one member
variable with value 10 and a method fly(). Now implements the interface for
Bird Class and Plane class, prints some message from bird and plane class
using fly().
Ans:
Code:
interface Flying
{
int minSpeed= 10;
public void fly();
}
Output:
Code:
interface AnimalEat {
void eat();
}
interface AnimalTravel {
void travel();
}
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
System.out.println("Animal is eating");
}
public void travel() {
System.out.println("Animal is travelling");
}
}
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
a.eat();
a.travel();
}
}
Output:
Animal is eating
Animal is travelling