0% found this document useful (0 votes)
113 views16 pages

Test 3 Key

1. The document discusses key points that will be tested in Test 3 for the Object Oriented Programming course. 2. It provides examples of multilevel inheritance in Java using a vehicle class hierarchy and demonstrates method overloading and overriding. 3. It also demonstrates using the super keyword to call parent constructors and abstract methods.

Uploaded by

Bhargav Konda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views16 pages

Test 3 Key

1. The document discusses key points that will be tested in Test 3 for the Object Oriented Programming course. 2. It provides examples of multilevel inheritance in Java using a vehicle class hierarchy and demonstrates method overloading and overriding. 3. It also demonstrates using the super keyword to call parent constructors and abstract methods.

Uploaded by

Bhargav Konda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Test 3 key

Object Oriented Programming (15 CS 2002)

For the a.y. 2016-17- II sem(ECE/EEE/PE)

Date of exam : 23-03-17


Basically programs are logic oriented, please verify whether concept to be used in the given
question is followed by the student is primary objective for evaluation.

Note: Points to be tested for every question is mentioned with key.

1.

a)what is multilevel inheritance In java? Demonstrate with a suitable java


program?.

Ans:

In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the
derived class act as the parent class to other class.
As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act
as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.
Program:
class Car{
public Car() {
System.out.println("Class Car");
}
public void vehicleType() {
System.out.println("Vehicle Type: Car");
}
}
classMaruti extends Car{
publicMaruti() {
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();
}
}

b) develop a java class Arith having two overloaded methods


compute(int,int) and compute(int,int,int),write a sub class BaArith class
which includes both the overridden methods?.write a main() method to
execute all four methods.
Sol:
classArith{
public void compute(inta,int b)
{
System.out.println("Base class Method with 2 Args");
System.out.println("Sum of a and b= "+(a+b));
}
public void compute(inta,intb,int c)
{
System.out.println("Base class Method with 3 Args");
System.out.println("Sum of a,b and c= "+(a+b+c));
}
}
classBaArith extends Arith{
public void compute(inta,int b){
super.compute(10,20);
System.out.println("Derived class Method with 2 Args");
System.out.println("Sum of a and b = "+(a+b));
}
public void compute(inta,intb,int c)
{
super.compute(10,20,30);
System.out.println("Derived class Method with 3 Args");
System.out.println("Sum of a,b and c= "+(a+b+c));
}
public static void main(String args[])
{
BaArithba = new BaArith();
ba.compute(2,5);
ba.compute(2,5,7);
}
}

2.

a) What is the use of super keyword? Demonstrate super key word in a


constructor using a java program.
Sol:
The super keyword can also be used to invoke or call the parent class constructor.
Constructor are calling from bottom to top and executing from top to bottom.

To establish the connection between base class constructor and derived class
constructors JVM provides two implicit methods they are:

super()
super(...)
super()

super() It is used for calling super class default constructor from the context of
derived class constructors.

Super keyword used to call base class constructor


Program:
class Employee{
Employee(){
System.out.println("Employee class Constructor");
}
}
class HR extends Employee{
HR(){
super(); //will invoke or call parent class constructor
System.out.println("HR class Constructor");
}
}
classSupercons{
public static void main(String[] args){
HR obj=new HR();
}
}

b.Devlop an abstract class Bliss in which callMe() method is an


abstract method.write another java class that should implement
callMe() method of class, so that callMe() method can be called?
Sol:

abstract class Bliss{


abstract void callMe();
}
classDerivedBliss extends Bliss{
voidcallMe(){
System.out.println("callMe() method implementd in subclass");
}
public static void main(String args[]){
DerivedBlissdb= new DerivedBliss();
db.callMe();
}
}

3)A) How interfaces are extended in java? Give demonstration using a java
program. 4M

One interface can inherit another by use of the keyword extends.

The syntax is the same as for inheriting classes.


When a class implements an interface that inherits another interface, it must
provide implementations for all methods defined within the interface inheritance
chain.

Using the keyword interface, you can fully abstract a class’ interface from its
implementation.

That is, using interface, you can specify what a class must do, but not how it does
it.

// One interface can extend another.

interface A {

void meth1();

void meth2();

interface B extends A {

void meth3();

// This class must implement all of A and B

class MyClass implements B {

public void meth1() {

System.out.println("Implement meth1().");

public void meth2() {

System.out.println("Implement meth2().");

public void meth3() {

System.out.println("Implement meth3().");
}

class IFExtend {

public static void main(String arg[]) {

MyClassob = new MyClass();

ob.meth1();

ob.meth2();

ob.meth3();

Or

// One interface can extend another.

interface one

void sum(inta,int b);

interface two

void sub(inta,int b);

interface three

void mul(inta,int b);


}

interface four extends three,two,one

void div(inta,int b);

class five implements four

public void sum(inta,int b)

int c=a+b;

System.out.println("sum is="+c);

public void sub(inta,int b)

int c=a-b;

System.out.println("sub is="+c);

public void mul(inta,int b)

int c=a*b;

System.out.println("mul is="+c);

public void div(inta,int b)

{
int c=a/b;

System.out.println("div is="+c);

class Six

public static void main(String args[])

five f=new five();

f.sum(10,20);

f.sub(20,10);

f.mul(5,5);

f.div(10,5);

3)B) Develop an interfaceEmpFace that has compute salary() method.


Write Employee class that implements EmpFace with basicSal as the
instance variable.Write manager class derived from Employee class having
benefits as instance variable. Both the classes should develop
compute_salary() methods with different implementation.
6M

interface EmpFace

{
void compute_salary();

}
class Employee implements EmpFace

floatbasicSal=10000;

public void compute_salary()

float da=basicSal*15/100;
float hra=basicSal*10/100;
float grosssal=basicSal+da+hra;
System.out.println ("Gross Salary= "+grosssal);

class Manager extends Employee

public void compute_salary()

float da=basicSal*20/100;
float hra=basicSal*15/100;
float grosssal=basicSal+da+hra;
System.out.println ("Gross Salary= "+grosssal);

public static void main(String args[])

Manager m=new Manager();

m.compute_salary();

}
}
4) A) Write a java interface Iface having callMe() method in it,
implementIface with a class IfaceImpl to implement callMe(). Write a
demo class to demonstrate this.
4M

interface Iface

Void callMe();

class IfaceImpl implements Iface {

public void callMe {

System.out.println("callme in iface method);

class Demo {

public static void main(String args[]) {

Ifacei = new IfaceImpl();

i.callMe();

or

IfaceImpl i1=new IfaceImpl();

I1.callMe();

}
4) B) Develop a Java program using inheritance and define Animal as the
super class with void sleep(), void eat() as the methods. Class Dog extends
Animal which includes void bark() method? Define a Demo class to create a
Dog object and call all the methods in the inheritance. 6M

class Animal

void sleep()

System.out.println ("Parent Sleep method") ;

void eat()

System.out.println ("Paprent Eat Method");

class Dog extends Animal

void bark ( )

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

class demo

{
public static void main (String args[ ])

Dog do = new Dog();

do.sleep();

do. eat();

do. bark();

Animal a = new Animal ( );

a. sleep ( );

a. eat();

5)a) What is the difference between abstract class and an interface? 4M

Any two differences---4 M

Abstract class Interface

1) Abstract class can have Interface can have only


abstract and non- abstract methods. Since Java 8, it can
abstract methods. have default and static methods also.

2) Abstract class doesn't Interface supports multiple


support multiple inheritance. inheritance.

3) Abstract class can have Interface has only static and final
final, non-final, static and variables.
non-static variables.

4) Abstract class can provide Interface can't provide the


the implementation of implementation of abstract class.
interface.
5) The abstract keyword is The interface keyword is used to
used to declare abstract class. declare interface.

6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

5)b) Develop a Java program Animal class as abstract class having atleast one abstract Method in
it,write a Monkey class as subclass of Animal class,test how the methods of Animal and Monkey
classes are executed?

Abstract class Animal


{
String name;
Animal(String name)
{
this.name = name;
}

Public abstract String getSound();


Public String getName()
{
return name;
}
}
Class Monkey extends Animal
{
Monkey(String name)
{
super(name);
}
Public String getSound()
{
return"chatter and gibber";
}
}
public class Fiveb
{
public static void main (String args[])
{
Monkey m=new Monkey(“Monkey1”);
Stirng S1,S2;
S1=m.getName();
S2=m.getSound();
System.out.println(“Animal Name”+S1);
System.out.println(“Animal Sound is”+S2);
}
}

6)a) How Multiple Inheritance implemented in java? Demonstrate with a


interface and two classes.

o By interface, we can support the functionality of multiple inheritance.

interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage: "+percent+"%");
}
void display()
{
super.display();
}
}

public class Sixa


{
public static void main(String args[])
{
Result R = new Result("Ra.one",12,93,84);
R.display();
R.percent_cal();
}
}

6)b) Develop a java program that creates a super class called Figure that
stores the dimensions of two-dimensional object. It also defines a method
called area() that computes the area of an object. The program derives two
subclasses form Figure. The first is Rectangle and second is Triangle.
Each of these subclasses overrides area() so that it returns the area of a
rectangle and a Triangle respectively.

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);
}
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}

double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
Public class sixb
{
public static void main(String args[])
{
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
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());
}
}

You might also like