Sunday, March 1, 2009

Puzzle 3 – Inheritance

Language – Java | Type – Concept | Last date 4-Mar-2009 09:00 p.m. IST | Points 2

Inheritance can be fun, especially if you get loads of money. But then again Inheritance can be a big headache. Especially in Java, if you inherit more that you can chew…
package com.twisters;
class Base{
public void baseCall(){
test();
}
public void test(){
System.out.println("Function Test in Base Executed");
}
}

public class Child extends Base {
public static void main(String[] args) {
Base baseRef = new Child ();
baseRef.baseCall();
}
public void test(){
System.out.println("Function Test in Child Executed");
}
}
Got an answer? Why don’t you leave it here.

6 comments:

  1. Function Test in Child Executed
    overriden method takes precedence.

    ReplyDelete
  2. when child overrides parent method, the child method takes precedence.

    ReplyDelete
  3. Function Test in Child Executed
    Function Test in Base Executed

    ReplyDelete
  4. Function Test in Child Executed
    Object of Child Class Can not see the BaseClass Test Method

    ReplyDelete
  5. Function Test in Child Executed

    ReplyDelete

Solution for this question?