Results 1 to 6 of 6
Thread: Ambiguos JAVA
- 11-12-2012, 10:47 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Ambiguos JAVA
How can you define the classes Adult, Student and Engineer so that the next sequence to give compilation error only where specified?
class Test {
public static void main(String args[]) {
Adult a = new Student(); //without giving compilation error
Adult b = new Engineer();//without giving compilation error
a.explorare(); //without giving compilation error
b.explorare(); //without giving compilation error
a.afisare(); //without giving compilation error
b.afisare(); //compilation error
- 11-13-2012, 04:20 AM #2
Re: Ambiguos JAVA
Moved from Advanced Java. Homework questions belong here, not there.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-13-2012, 09:51 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Ambiguos JAVA
What have you tried?
Please do not ask for code as refusal often offends.
- 11-26-2012, 10:04 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Re: Ambiguos JAVA
I thing you want to use inheritance (between Adult, Student and Engineer) in such a way that you get a compilation error when you call “b.afisare()”.
If it is so then, I feel it won’t happen as variable “a” and “b” are of same type ‘Adult’. So when you call “a.afisare()” or “b.afisare()” compiler will check the method signature in Adult class only (never the subclass). So when “a.afisare()” compiles even “b.afisare()” will compile.
- 11-26-2012, 11:24 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Ambiguos JAVA
Well spotted.
I missed that they were both declared as Adults.Please do not ask for code as refusal often offends.
- 11-27-2012, 11:09 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks