Results 1 to 6 of 6

Thread: Ambiguos JAVA

  1. #1
    ALINAMARIA is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Ambiguos JAVA

    Moved from Advanced Java. Homework questions belong here, not there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Ambiguos JAVA

    What have you tried?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    fillaru0 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Ambiguos JAVA

    Well spotted.
    I missed that they were both declared as Adults.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    fillaru0 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •