Inheritance and Polymorphism in Java: CIS110 Practice, Assignments of Computer Science

The relationship between superclasses and subclasses in java, focusing on inheritance and polymorphism through the example of classes a and c. It covers the concept of type rules, method overriding, and the execution of tostring() method.

Typology: Assignments

Pre 2010

Uploaded on 03/28/2010

koofers-user-lra
koofers-user-lra 🇺🇸

8 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CIS110
Inheritance Practice
Assume that statement below compile and runs without errors:
> A data = new C();
1. What can we infer about relationship between A and C?
A is supertype and C is subtype (C is a A). This is due to type rules – a reference
variable of type t may refer to an object of its own type or any subtype.
2. Assume B is a class that inherit from A. Class B has a method b() but C does not.
Class C has a methods c() but B does not. Consider the following:
> A data = new C();
For the following interaction answer yes or no if the code compiles with error. If it
compiles does it have run-time/semantic errors. Explain.
> ((C)data).c();
Compiles. Without the cast it would be compile error as c() is not in class A and
variable data is of type A. It also executes without errors as variable data is
referring to an instance of C.
> ((B)data).b();
Compiles. Without the cast it would be compile error as b() is not in A and
variable data is of type A. However, it does not execute as variable data is referring
to an instance of C. We cannot convert an instance of type C to a B.
3. Consider toString() in classes A and C. Consider the following:
> A data = new C();
Consider the execution
> data //or can also do data.toString()
From which class is the toString method executed?
From class C. Variable data is referring to object of type C. Due to overriding, java
picks the method lowest in the inheritance chain. If toString( ) was not in class C,
then the method written in class A would be executed by object of type C as it would
inherit the method from class A. If both A and C did not have toString() method,
then method from Object class would be executed (parent of all) and whose default
behavior is to return’s objects heap address.

Partial preview of the text

Download Inheritance and Polymorphism in Java: CIS110 Practice and more Assignments Computer Science in PDF only on Docsity!

CIS

Inheritance Practice

Assume that statement below compile and runs without errors:

A data = new C();

  1. What can we infer about relationship between A and C? A is supertype and C is subtype (C is a A). This is due to type rules – a reference variable of type t may refer to an object of its own type or any subtype.
  2. Assume B is a class that inherit from A. Class B has a method b() but C does not. Class C has a methods c() but B does not. Consider the following:

A data = new C();

For the following interaction answer yes or no if the code compiles with error. If it compiles does it have run-time/semantic errors. Explain.

((C)data).c();

Compiles. Without the cast it would be compile error as c() is not in class A and variable data is of type A. It also executes without errors as variable data is referring to an instance of C.

((B)data).b();

Compiles. Without the cast it would be compile error as b() is not in A and variable data is of type A. However, it does not execute as variable data is referring to an instance of C. We cannot convert an instance of type C to a B.

  1. Consider toString() in classes A and C. Consider the following:

A data = new C(); Consider the execution data //or can also do data.toString()

From which class is the toString method executed?

From class C. Variable data is referring to object of type C. Due to overriding, java picks the method lowest in the inheritance chain. If toString( ) was not in class C, then the method written in class A would be executed by object of type C as it would inherit the method from class A. If both A and C did not have toString() method, then method from Object class would be executed (parent of all) and whose default behavior is to return’s objects heap address.