You are on page 1of 15

KALYANI GOVERNMENT ENGINEERING

COLLEGE

NAME- ABHISHEK BARAL


ROLLNO- 10200119031
REGISTRATION NO – 039226 OF 2019-20
DEPARTMENT- CSE
YEAR- 3rd
SEMESTER- 5TH
SUBJECT- Object Oriented Programming

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);
}
}

// Create a subclass B by extending class A.


class B extends A {
int z;

void showz() {
System.out.println("z: " + z);
}

void sum() {
System.out.println("x+y+z: " + (x + y + z));
}
}

public class OOPAssignment {


public static void main(String args[]) {

A superOb = new A();


B subOb = new B();
// The superclass may be used by itself.
superOb.x = 10;
superOb.y = 20;
System.out.println("Contents of superOb: ");
superOb.showxy();
System.out.println();
/*
* The subclass has access to all public members of its superclass.
*/
subOb.x = 7;
subOb.y = 8;
subOb.z = 9;
System.out.println("Contents of subOb: ");
subOb.showxy();
subOb.showz();
System.out.println();
System.out.println("Sum of x, y and z in subOb:");
subOb.sum();
}
}

Output:

Contents of superOb:
x and y: 10 20

Contents of subOb:
x and y: 7 8
z: 9

Sum of x, y and z in subOb:


x+y+z: 24

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(double length, double breadth, double height) {


this.length = length;
this.breadth = breadth;
this.height = height;
}

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);
}
}

class BoxWeight extends Box {


private double weight;

BoxWeight() {
super();
}

BoxWeight(double length, double breadth, double height, double weight) {


super(length, breadth, height);
this.weight = weight;
}

BoxWeight(BoxWeight BW) {
super(BW);
this.weight = BW.weight;
}

void showBoxDetails() {
showBox();
System.out.println("Weight = " + weight);
}
}

public class OOPAssignment2 {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Length, Breadth, Height & Weight of a
Box");
double length = sc.nextDouble();
double breadth = sc.nextDouble();
double height = sc.nextDouble();
double weight = sc.nextDouble();

BoxWeight B1 = new BoxWeight();


B1.showBoxDetails();
BoxWeight B2 = new BoxWeight(length, breadth, height, weight);
B2.showBoxDetails();
BoxWeight B3 = new BoxWeight(B2);
B3.showBoxDetails();
}

Output:

Enter Length, Breadth, Height & Weight of a Box


4
6
8
9
Length = 0.0
Breadth = 0.0
Height = 0.0
Weight = 0.0
Length = 4.0
Breadth = 6.0
Height = 8.0
Weight = 9.0
Length = 4.0
Breadth = 6.0
Height = 8.0
Weight = 9.0

7. Write a program to give the example for method overriding concepts.

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);
}

// override area for rectangle


double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}

class Triangle extends Figure {


Triangle(double a, double b) {
super(a, b);
}

// override area for right triangle


double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}

public class OOPAssignment2 {


public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 8);
Triangle t = new Triangle(10, 9);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}

Output:

Inside Area for Rectangle.


Area is 72.0
Inside Area for Triangle.
Area is 45.0
Area for Figure is undefined.
Area is 0.0
8. Write a program to create a class named shape. In this class we have three
sub classes circle, triangle and square each class has two member function
named draw () and erase (). Create these using polymorphism concepts.
What type of inheritance do we achieve by this program?

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.

9. Write a program in java to illustrate multilevel inheritance.

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

10. Write a program in java to illustrate abstract class concept.


Ans:

Code:

// Java Program to Illustrate That an Iinstance of Abstract


// Class Can not be created

// 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 {

public static void main(String args[])


{

// Uncommenting the following line will cause


// compiler error as the line tries to create an
// instance of abstract class. Base b = new Base();

// We can have references of Base type.


Base b = new Derived();
b.fun();
}
}

Output:

Derived fun() called


// Java Program to Illustrate Abstract Class
// Can contain Constructors

// Class 1
// Abstract class
abstract class Base {

// Constructor of class 1
Base()
{
// Print statement
System.out.println("Base Constructor Called");
}

// Abstract method inside class1


abstract void fun();
}

// 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 {

// Main driver method


public static void main(String args[])
{
// Creating object of class 2
// inside main() method
Derived d = new Derived();
}
}

Output:
Base Constructor Called
Derived Constructor Called
11. Write a program in Java to implement dynamic method dispatch concept.

Ans:

Code:

// A Java program to illustrate Dynamic Method


// Dispatch using hierarchical inheritance
class A
{
void m1()
{
System.out.println("Inside A's m1 method");
}
}

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();

// obtain a reference of type A


A ref;

// ref refers to an A object


ref = a;

// calling A's version of m1()


ref.m1();

// now ref refers to a B object


ref = b;

// calling B's version of m1()


ref.m1();

// now ref refers to a C object


ref = c;

// calling C's version of m1()


ref.m1();
}
}

Output:

Inside A's m1 method


Inside B's m1 method
Inside C's m1 method

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();
}

class Bird implements Flying


{
public void fly()
{
System.out.println("Car can fly");
}
}

class Plane implements Flying


{
public void fly()
{
System.out.println("Plane can fly");
}
}

public class FlyingBase


{
public static void main(String[] args) {
Bird eagle= new Bird();
Plane boeing= new Plane();
eagle.fly;
boeing.fly();
}
}

Output:

Birds can fly


Planes can fly

13. Write a program in java to implement multiple inheritances using interface.


Ans:

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

You might also like