Вы находитесь на странице: 1из 9

OOM ASSINGMENT 2011

RIT2009013

LOKESH KUMAR

IIIT-Allahabad( Amethi Campus )


Q 1) For the given interface program as below, study the program listings -
showing 2 interfaces and 4 classesone being an abstract class, run the
program and write the output while explaining it in brief.

Output :
OUTPUT-of program is :

I am in methodInterfaceOne of class classTwo


I am in methodInterfaceTwo of class classTwo
I am in methodInterfaceOne of class classTwo
I am in methodInterfaceTwo of class classTwo
var2 : I am in methodC1 of class classOne
var3 : I am in methodC1 of class classOne
var4 : toString() method of class classOne
var5 : toString() method of class classOne
var6 : C1@190d11
I am in methodInterfaceOne of class classTwo
I am in methodInterfaceOne of class classTwo
I am in methodInterfaceOne of class classTwo

Explaination:
I am in methodInterfaceOne of class classTwo

Explanation:

InterfaceOne i1 = new classTwo(); -> reference of interface i1 is declared


and it instantiates classTwo object as classTwo implements interfaceOne
ClassTwo implements interfacetwo which extends interfaceone

i1.methodInterfaceOne(); -> methodInterfaceOne() of classTwo prints “I am


in methodInterfaceOne of class classTwo ”

I am in methodInterfaceTwo of class classTwo

Explanation:
((InterfaceTwo) i1).methodInterfaceTwo();

I1 is typecasted to be a reference to interfaceTwo as in classTwo


implements interfaceTwo .It prints

methodInterfaceTwo i.e ,” I am in methodInterfaceTwo of class classTwo”

I am in methodInterfaceOne of class classTwo

Explanation

InterfaceTwo i2 = new classTwo();->Reference to interfaceTwo i2 is


declared and classTwo Object is instantiated as classTwo extends
interfaceTwo.
i2.methodInterfaceOne(); -> methodInterfaceOne of classTwo is called and
prints ” I am in methodInterfaceOne of class classTwo”

I am in methodInterfaceTwo of class classTwo

Explanation:

i2.methodInterfaceTwo();->methodInterfaceTwo of classTwo is called as i2


points to a classTwo object

and it prints ” I am in methodInterfaceTwo of class classTwo”

var2 : I am in methodC1 of class classOne

Explanation:

String var2 = ((classOne) i1).methodOne();


System.out.println("var2 : " + var2);

I1 was a reference to interfaceTwo which points to classTwo object which


currently is type casted to

Point to a classOne object as classTwo extends classOne this is


possible.Now methodOne of classOne

Is called which returns a string “I am in methodC1 of class classOne";

And this string is printed in the next statement


var3 : I am in methodC1 of class classOne

Explanation:

String var3 = ((classTwo) i1).methodOne();


System.out.println("var3 : " + var3);

Explanation: i1 is typecasted to be a reference of type classTwo i1 was


pointing to classOne object which is a parent class of classOne so it can be
typecasted.
And methodOne is called it is a method in superclass of classTwo i.e,
classOne so it returns the string “I am in methodC1 of class classOne”
which is printed in the next statement

var4 : toString() method of class classOne

Explanation:

String var4 = i1.toString();


Explanation:System.out.println("var4 : " + var4);

I1 which is a reference to classTwo object calls toString() method .This method is


defined in parent class of classTwo which is classOne and it returns the string”
toString() method of class classOne”

Which is printed in the second statement

var5 : toString() method of class classOne

Explanation:
String var5 = i2.toString();

System.out.println("var5 : " + var5);

I2 is referenced to classTwo object and tostring method of its superclass is called


and it returns a string” () method of class classOne” is printed in the next
statement

var6 : javaapplication2.C1@190d11

Explanation:
InterfaceOne i3 = new C1();
String var6 = i3.toString();
System.out.println("var6 : " + var6);

I3 is used to instantiate the object of class c1 This is possible since classc1 extends
interfaceTwo which extends interfaceOne and i3 can hold a reference of type
interfaceOne .

toString method from object class is called which converts integer to string

i3 holds a reference so it returns a string “ javaapplication2.C1@190d11” which is


returned to var6

It is printed .

i3 holds a reference so it contains 32 bit integer value which is converted to above


string.

I am in methodInterfaceOne of class classTwo

Explanation:

Object o1 = new classTwo();


((InterfaceOne) o1).methodInterfaceOne();

Object is parent class of all the classes and its object is used to instantiate
classTwo object

It is type casted to hold a reference of interfaceOne but it can still refer


classTwo object

And methodInterfaceOne(); of classTwo object is called which prints ” I am


in methodInterfaceOne of class classTwo”

I am in methodInterfaceOne of class classTwo

Explanation:
It is type casted to hold a reference of interfaceTwo but it can still refer
classTwo object
And methodInterfaceOne(); of classTwo object is called which prints ” I am
in methodInterfaceOne of class classTwo

I am in methodInterfaceOne of class classTwo

Explanation:

It is type casted to hold a reference of classTwo object

And methodInterfaceOne(); of classTwo object is called which prints ” I am


in methodInterfaceOne of class classTwo

Q 2) As discussed during Lectures, there are 03 Java classes where


“Employee” class is the parent class of all classes. “Salary” class extends
“Employee” class and “Hourly” class also extends from the same
“Employee” class. Use Salary, Hourly and Employee classes details from
your OOM class ppts to explain the following with implementations:

Q 2) Answer-

1) Parent class references to Child objects.

A child object can be referenced using a parent class reference. This is the most
fundamental use of polymorphism because using a parent class reference to
refer to a child object is what allows you to take advantage of the benefits of
polymorphism.

Employee p = new Salary(“Rich Raposa”, “Rapid City, SD”, 47, 250000.00);

The left side of the equation creates a reference p of type Employee. The
right side of the equation is a new Salary object.
The is a relationship carries over to polymorphism. The right side of the equation
is a Salary object. A Salary object is an Employee object.

P can refer to an Employee object, and because a Salary object is an Employee


object, p can refer to a Salary object as well.

An Employee reference can refer to any Employee object. Because a


Salary object is an Employee object, an Employee reference can be used
to refer to a Salary object. This is an example of a parent class reference
referring to a child class object.

2) Run and Explain Polymorphic parameters and return values &

Polymorphic parameters:

When a method has a parameter that is a reference, any object that


is compatible with that reference can be passed in, allowing a method to
accept parameters of different data types.

For example, suppose that a method has an Employee parameter:

public void payEmployee(Employee e)

An Employee object needs to be passed in to the payEmployee() method. If


Salary and Hourly extend Employee, a Salary or Hourly object could also be
passed in to payEmployee() because through polymorphism a Salary or
Hourly object is also an Employee object.
Having an Employee parameter makes payEmployee() a more generic
method in that its parameter is loosely defined. Presently, it can accept
Employee, Hourly, and Salary objects. If a new class came along that
extended Employee, say a class named Contractor, Contractor objects
could also be passed in to payEmployee().

The payEmployee() method can be invoked passing in any Employee object. If a


new Contractor class is written that extends Employee, Contractor objects can be
passed into the existing payEmployee() method without changing the method’s
signature.

public class PayDemo


{
public static void main(String [] args)
{
Boss boss = new Boss();
Hourly h = new Hourly(“Abe Lincoln”, “Springfield, IL”,
16, 8.00);
Salary s = new Salary(“George Washington”, “Valley Forge, DE”,
1, 5000.00);
Employee x = new Salary(“Rich Raposa”, “Rapid City, SD”,
47, 250000.00);
Employee y = new Employee(“George W.”, “Houston, TX”, 43);
System.out.println(“** Paying Abe Lincoln **”);
boss.payEmployee(h);
System.out.println(“\n** Paying George Washington **”);
boss.payEmployee(s);
System.out.println(“\n** Paying Rich Raposa **”);
boss.payEmployee(x);
System.out.println(“\n** Paying George W. **”);
boss.payEmployee(y);
}
}
3) How to Create heterogeneous collections

A common use of polymorphism is to create a collection of data that is not all the
same type, but has a common parent. A collection of different objects is referred to
as a heterogeneous collection.

Employee p = new Salary(“Lokesh Kumar”, “IIIT-A, Amethi”, 47, 250000.00 );

The left side of the equation creates a reference p of type Employee. The
right side of the equation is a new Salary object.

P can refer to an Employee object, and because a Salary object is an Employee


object, p can refer to a Salary object as well.

Вам также может понравиться