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

Thread: What if a class extends another class and they each have a method with the same prototype? When to call the subclass and when to call superclass?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What if a class extends another class and they each have a method with the same prototype? When to call the subclass and when to call superclass?

    m confused with very simple example of Inheritance like.
    class A
    {
    int k=1;
    void show()
    {
    System.out.println("===inside A class= Show()=="+k);
    }
    }
    class B extends A
    {
    int k=8;
    void show()
    {

    System.out.println("===inside B class=Show()=="+k);
    }
    void show1()
    {

    System.out.println("===inside B class show1() ===");
    }
    }

    public class Inheritance
    {
    public static void main(String s[])
    {
    A a=new B();
    a.show();
    //a.show1();//error
    System.out.println("===="+a.k);

    }
    }
    in the above example i m making reference of superclass to Subclass object. Now i have two Question.
    1-Why we Do, means===> A a=new B(); Why not use B=new B(); or A a=New A();When We should do Like it A a=new B();
    2-when i call a.show(); its print "==inside B class=Show()==" while it should show "==inside A class= Show()==" because as i know Reference Of A class know only A class member only.
    because if i will a.show1(); it give Error Because show1() is function of class B. and if it show class B member also then i print a.k it show "1". why not "8" defined in Class B."
    Last edited by Deep_4; November 7th, 2012 at 11:48 AM.


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: inheritance problem with references

    1) A a=new B()
    With A a=new B() you can access overrided method show but not show1 method, this method is accessible only from class B instances and also your LOCAL (Class A) variable k
    Inside B class method show() === 8
    Inside class variable:1

    2) B b=new B()
    With B a=new B() you can access overrided class A method show, method show1 inside class B and your LOCAL (Class B) variable k
    Inside B class method show() === 8
    Inside B class method show1() === 8
    Inside class variable:8

    3) A c=new A()
    With A c=new A() you can access method show inside Class A, and your LOCAL (Class A) variable k
    Inside A class method Show() === 1
    Inside class variable:1

Similar Threads

  1. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  2. [SOLVED] Small problem regarding inheritance of classes
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 10th, 2011, 02:11 PM
  3. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM
  4. An associations with copies instead of references
    By Muskar in forum Object Oriented Programming
    Replies: 5
    Last Post: January 5th, 2011, 01:22 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 10:11 PM

Tags for this Thread