You are here:

Java/Java Inheritance Hierarchy

Advertisement


Question
Hi Artemus,

I've come across couple of tricky questions regarding inheritance from a java text book. i have made an attempt to answer all of them below but im unsure whether they all are correct, could you please correct me if i'm wrong.

The 1st Question poses an inheritance hierarchy from an extension of a cluedo game, and this is how it looks:

http://img201.imageshack.us/img201/2463/qojj.png

from the image i have to verify whether or not following statements are valid (my attempts are followed by the '<---'):

Floor carpeted = new Floor(); <---This statement is valid

BoardSquare sq = new BoardSquare(); <---This statement is invalid because abstract classes cannot be instantiated.

Doorway hidden = new SecretDoor(); <---This statement is valid

Lockable brick = new Doorway(); <---This statement is valid? unsure

BoardSquare door = new Trapdoor(); <---This statement is valid? is it because abstract classes can instantiate their subclasses?

Lockable secret = new SecretDoor(); <--- This statement is valid? unsure

Trapdoor invisible = new Lockable(); <---This statement is invalid. because interfaces cannot be instantiated.


Another question i would like to ask is, from the class BoardSquare, Doorway and Lockable(interface) which methods would i need to implement for the SecretDoor class? I've made the attempt at the very bottom with my explanation.

Ive been given the following javadoc which includes:
Class BoardSquare{
int addOccupant()
double removeOccupant()
abstract getStringRepresentation()
}

Class Doorway{
getStringRepresentation()
}

interface Lockable{
boolean isLocked()
boolean isSameAs(Lockable other)
boolean isUnlocked()
void lock()
void unlock()
}

From above im guessing that the SecretDoor class will must have to implement all the methods from the interface Lockable since it implements the interface(from the image i've provided), but im not sure whether or not the getStringRepresentation method should be implemented in the SecretDoor class?

class SecretDoor extends Doorway implements Lockable{
boolean isLocked()
boolean isSameAs(Lockable other)
boolean isUnlocked()
void lock()
void unlock()
getStringRepresentation()???
}

I'm sorry for the long question & thank you for taking you're time to look at this.

Answer
Floor carpeted = new Floor(); <---This statement is valid
Correct, since Floor is concrete.

BoardSquare sq = new BoardSquare(); <---This statement is invalid because abstract classes cannot be instantiated.
Correct

Doorway hidden = new SecretDoor(); <---This statement is valid
Correct, since Doorway is a superclass of SecretDoor

Lockable brick = new Doorway(); <---This statement is valid? unsure
Not valid. Doorway doesn't implement the Lockable interface, even though its subclass of SecretDoor does.

BoardSquare door = new Trapdoor(); <---This statement is valid? is it because abstract classes can instantiate their subclasses?
Valid, TrapDoor is a concrete class, and BoardSquare is a super class (indirectly) of TrapDoor. Although your statement is true, abstract can create an instance of any concrete class. However, their constructor will only run when their subclasses are created.

Lockable secret = new SecretDoor(); <--- This statement is valid? unsure
Valid, SecretDoor implements the Lockable interface. This would also be valid for creating any subclass of SecretDoor.

Trapdoor invisible = new Lockable(); <---This statement is invalid. because interfaces cannot be instantiated.
Correct. Futhermore, Trapdoor isn't a subclass or subinterface of Lockable (the reverse is true).

You don't need to implement getStringRepresentation(), since a subclass of SecretDoor already implements it. In particular you need to implement all the methods in Lockable and no more, since Doorway, being concrete, will implement all the methods in BoardSquare and Board that aren't already implemented in BoardSquare. Since, Lockable isn't an interface implemented by Doorway, it shouldn't implement any of the methods in there*.

* Doorway can have a method name that matches Lockable, and in this case you won't need to implement it. But this is a really bad design, and should be avoided. If you were using c++ or c#, then you would have to implement these methods even if this is the case.  

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.