Fred Butler

Greenhorn
+ Follow
since Oct 16, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Fred Butler

Francky Boy wrote:SELF TEST Page 82, question 7.
Answer:
C,D, and F are correct.

F is wrong !

What is wrong on the enum on line 10, compilation succeeds.



In J2SE version 6, F won't compile:
"The member enum Traffic can only be defined inside a top-level class or interface"
- but it's defined within a method, hence the compiler error.

You could check that you've typed the code correctly, and that you're using the correct compiler (version 6).

Regards,
Al

David Samer wrote:Greetings there everyone.

<snip>



OK, plain polymorphism , so I take, as book states, I have to watch for reference type. Fine.

In this last example makes sense , Animal reference but Horse object, and based in the above line, our var h has Animal reference, so there when trying to invoke buck() method fails.

Based in the above,why in the first example :




b having Animal reference, yet runs Horse version of eat() method? (Like if it were overloading instead an overriding )


Thanks in advance.



Hi David,

In the second example, both classes (Animal and Horse) have an eat() method so the compiler is happy that an Animal object reference can have an eat() method invoked on it
At Runtime, the JVM can invoke this method on whatever the actual object is, whether it's an Animal or a Horse object.

However, in the second example, the compiler knows that the Animal class does not have a buck() method, so compilation fails.
The fact that the actual object referred to is a Horse (which does have a buck() method) isn't enough, because the compiler knows the Animal object reference could refer to an Animal (or any subclass of Animal) which may not have a buck() method. So you can't invoke a method buck() on a reference type that doesn't implement a method buck().

Regards,
Alan


[EDIT: Apols, Just saw Ankits answer]

sharma ishu wrote:

Alan Cowap wrote:

sharma ishu wrote:Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.



Hi Sharma,

A superclass can't require it's subclasses to create a no-arg constructor (i.e. a no-arg constructor in the subclass).
Furthermore, it's good OO that a superclass should have no 'knowledge' of it's subclasses.

Regards,
Al


This question is from "practice exams book by kethy and bates" edition 2011 chapter-4 page-150. So, I think it should have a solution.



With respect to the K&B, the question could be written less ambiguously.
"Write a superclass that requires any of its subclasses to create a no-arg constructor [in the superclass]. Write a subclass for this superclass.
With the question in this context, the answer given above by Himai Minh is what you want.

Aside:
One could equally interpret the question as:
"Write a superclass that requires any of its subclasses to create a no-arg constructor [in the subclass(es)]. Write a subclass for this superclass.
In which case my previous answer would suffice.

Regards,
Alan

sharma ishu wrote:Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.



Hi Sharma,

A superclass can't require it's subclasses to create a no-arg constructor (i.e. a no-arg constructor in the subclass).
Furthermore, it's good OO that a superclass should have no 'knowledge' of it's subclasses.

Regards,
Al

Srinivas Palam wrote:Thanks for your Reply. I am assuming the same when Init(7) is involved. It will invoke its implicit super(with one argument).



No, the implicit call from a subclass constructor is to the super() no-args constructor.
If there is no no-args constructor in the superclass then compilation will fail.
You can explicitly call a specific superclass constructor from the subclass constructor, and that call must be the first statement in your subclass constructor.

Regards,
Al

Chris Devine wrote:In the Chapter 1 Self-test, question 6:

The answer given (A. Compilation succeeds) seems incorrect. If the Device interface contains the method public void doIt(), then all classes implementing that interface should implement that method. However, the class Phone 3 implements the Device interface but only implements a doStuff() method. This would cause a compile error due to a mistake on line 7. Answer E should be the correct answer in that case.



Hi Chris,

The answer in the book is correct.
Note that "Phone3 extends Electronic" and that "Electronic implements Device { public void doIt() { } }"
Hence, Phone3 inherits the "public void doIt() { }" implementation from Electronic.
Aside, there's no need for Phone3 to explicitly state it implements Device, since Phone3 already inherits that interface from Electronic.

Regards,
Al