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 3 of 3

Thread: problem with inheritance

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with inheritance

    package exam804;
    class Base
    {
    public static void foo(Base bObj)
    {
    System.out.println("In Base.foo()");
    bObj.bar();
    }

    public void bar()
    {
    System.out.println("In Base.bar()");
    }
    }

    class Derived extends Base
    {
    public static void foo(Base bObj)
    {
    System.out.println("In Derived.foo()");
    bObj.bar();
    }

    public void bar()
    {
    System.out.println("In Derived.bar()");
    }
    }

    class OverrideTest
    {
    public static void main(String[] args)
    {
    Base bObj = new Derived();
    bObj.foo(bObj);
    }
    }

    why the output is
    "In Base.foo() In Derived.bar()" and not
    "In Derived.foo() In Derived.bar()" ?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: problem with inheritance

    Static methods aren't polymorphic. Which static method is invoked is determined by: 1. The type of the variable being used (note that the variable type is not necessarily the same as the type of the object held), or 2. The class quantifier provided.

    Using an instance variable to call a static method is discouraged. Instead, use:

    Base.foo();
    Derived.foo();

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with inheritance

    tanks

Similar Threads

  1. Problem with inheritance
    By barry1888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 15th, 2013, 01:49 PM
  2. Java Inheritance problem
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2013, 07:34 PM
  3. problem with inheritance
    By freedom12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 29th, 2013, 04:59 AM
  4. Replies: 1
    Last Post: November 2nd, 2012, 04:56 AM
  5. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM