Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trick question?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trick question?

    Hi there, I'm preparing for an interview with a company, and I came across this question earlier and was wondering peoples' thoughts on it.
    I have a fairly solid programming background, but not so much with Java, and as multiple inheritance is not allowed in Java, how would you respond to this question?

    Given a class A and another (B) that inherits from it, if you have a method that casts B to A and invokes a method x() that both A and B implement, which class's implementation is invoked?

    At first look, I'm not entirely sure how to answer this. Since I am usually programming in C#, I do believe this would throw an exception, but since this is Java, after some brushing up, wouldn't it be appropriate to say, "since neither A or B are implementing an interface, it would throw an exception?"

    Any help/thoughts would be greatly appreciated!!

    Thank you!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Trick question?

    I highly suggest you write a small program that does exactly that, and see what happens. Then you can show us!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trick question?

    I'm not even sure I'm breaking this down to myself correctly...
    Given a class A and another (B) that inherits from it, if you have a method that casts B to A and invokes a method x() that both A and B implement, which class's implementation is invoked?

    Basically wouldn't this just be a function call that is within class B, but it really just calls a function in class A, therefore there is no confusion on the matter, it could generically be written as "ClassA.FunctionX()"?

    I don't think I'm even understanding this correctly...
    Last edited by Kungpaoshizi; August 29th, 2011 at 02:15 PM.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trick question?

    public static void main(String[] args) {
    // TODO code application logic here
    A a = new B();
    a.callme();
    }
    }

    class A
    {
    public void callme()
    {
    System.out.println("A.callme");
    }
    }
    class B extends A
    {
    public void callme()
    {
    System.out.println("B.callme");
    }
    }


    results in "b.callme" being printed?
    anyone know of a real world example for this other than to tick off someone interviewing? LOL

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Trick question?

    This is an extremely common example of inheritance, and it's used all the time- not just as an interview question.

    For example, say you have a class Animal that has some default behaviors (let's say eat, speak, and walk)-

    public class Animal{
       public void speak(){
          System.out.println("The animal made a noise!");
       }
        public void eat(){
          System.out.println("The animal ate its food.");
       }
       public void walk(){
          System.out.println("The animal took a step.");
       }
    }

    So far so good, right? But then you want a specific sub-type of Animal, let's say Cat, to override the behaviors for eat and speak:

    public class Cat extends Animal{
       public void speak(){
          System.out.println("The cat meowed!");
       }
        public void eat(){
          System.out.println("The cat ate a mouse.");
       }
    }

    Okay, so now what would you want to happen with this:

    Animal cat = new Cat();
    cat.speak();
    cat.walk();
    cat.eat();

    See, the instance of Cat IS AN Animal, but it also has its own behavior.

    This is one of the most commonly used features of OOP- extending a class to change a part of its behavior. For example, the most common way to do painting is by extending JPanel and overriding the paintComponent() method- everything else stays the same, and the program treats the Object as a normal JPanel, until the painting is done- then the code in the child class is called.

    This is actually a pretty decent interview question, as the only real way to understand what I'm talking about is through the experience of actually using it. And I suggest you do get some more practice before your next interview, as this is just the tip of the iceburg- we haven't touched the ability to call parent class methods from a child, or what happens when a method is static, or mixing child and parent methods, or abstract methods, or...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!