Calling parent's method even though it has been overridden in the child

Discussion in 'Java' started by Patel, Oct 22, 2003.

  1. Patel

    Patel Guest

    Is there a way to directly access a Super-Class method from an
    instance of Sub-class.

    eg.
    public class ClassA
    {
    void Perform()
    {
    System.out.print("ClassA");
    }
    }

    public class ClassB extends ClassA
    {
    public static void main(String[] args)
    {
    ClassB objB = new ClassB();
    ClassA objA = (ClassA) objB;

    objA.Perform();
    }

    void Perform()
    {
    System.out.print("ClassB");
    }
    }

    This code would print out -- ClassB

    But what I am looking at is, Is there a way to make a Perform() call
    from instance of ClassB and achieve the result as -- ClassA.

    I am new to OO programming.

    Thanks,
    Sajid
     
    Patel, Oct 22, 2003
    #1
    1. Advertising

  2. Re: Calling parent's method even though it has been overridden inthe child

    +Is there a way to directly access a Super-Class method from an
    +instance of Sub-class.

    super.Perform() should do the trick I think.

    +
    +But what I am looking at is, Is there a way to make a Perform() call
    +from instance of ClassB and achieve the result as -- ClassA.
     
    Anand Gopinath, Oct 22, 2003
    #2
    1. Advertising

  3. Patel

    bm Guest

    I think I read it in 'Thinking in Java' that this is one of the
    design differences between Java and C++. Even though
    you cast a reference of Class B to a reference of Class A,
    a call to Perform method will still result in a call to Class B.
    This is done in purpose. Read 'Thinking in Java' to understand
    the rational behind it.

    hope this helps.


    "Patel" <> wrote in message
    news:...
    > Is there a way to directly access a Super-Class method from an
    > instance of Sub-class.
    >
    > eg.
    > public class ClassA
    > {
    > void Perform()
    > {
    > System.out.print("ClassA");
    > }
    > }
    >
    > public class ClassB extends ClassA
    > {
    > public static void main(String[] args)
    > {
    > ClassB objB = new ClassB();
    > ClassA objA = (ClassA) objB;
    >
    > objA.Perform();
    > }
    >
    > void Perform()
    > {
    > System.out.print("ClassB");
    > }
    > }
    >
    > This code would print out -- ClassB
    >
    > But what I am looking at is, Is there a way to make a Perform() call
    > from instance of ClassB and achieve the result as -- ClassA.
    >
    > I am new to OO programming.
    >
    > Thanks,
    > Sajid
     
    bm, Oct 22, 2003
    #3
  4. Patel

    Roedy Green Guest

    On 22 Oct 2003 14:03:02 -0700, (Patel) wrote or
    quoted :

    >But what I am looking at is, Is there a way to make a Perform() call
    >from instance of ClassB and achieve the result as -- ClassA.


    If ClassB extends ClassA, in methods of ClassB, you can use

    super.somemethod();

    and you will get the ClassA version in place of the ClassB version.

    However, code outside ClassB can't do that.

    If a method is static, clients can specify which version they want by
    saying:

    ClassA.someMethod() or ClassB.someMethod()

    The author of ClassB decides just what methods make sense on his
    object. Nobody is allowed to play games, using methods from other
    classes (not even ClassA) on his objects, unless they extend the
    class. And even then, the author of ClassB gets to decide which
    methods are final, protected, private etc, to prevent the author of
    extending class from screwing things up.

    It is hard enough getting code to work without having other people
    calling your methods in ways you never intended. The mechanism gives
    greater control to the author of the base class than you would find in
    other languages.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 22, 2003
    #4
  5. Patel

    Sudsy Guest

    Re: Calling parent's method even though it has been overridden inthe child

    Patel wrote:
    > Is there a way to directly access a Super-Class method from an
    > instance of Sub-class.
    >
    > eg.
    > public class ClassA
    > {
    > void Perform()
    > {
    > System.out.print("ClassA");
    > }
    > }
    >
    > public class ClassB extends ClassA
    > {
    > public static void main(String[] args)
    > {
    > ClassB objB = new ClassB();
    > ClassA objA = (ClassA) objB;
    >
    > objA.Perform();
    > }
    >
    > void Perform()
    > {

    super.Perform();
    // System.out.print("ClassB");
    > }
    > }
    >
    > This code would print out -- ClassB
    >
    > But what I am looking at is, Is there a way to make a Perform() call
    > from instance of ClassB and achieve the result as -- ClassA.
    >
    > I am new to OO programming.
    >
    > Thanks,
    > Sajid


    Certainly you can do it, but you have to be asking yourself why.
    If it makes sense and doesn't confuse those who attempt to grok
    your code then it's fine.
    There are some situations where you NEED to access the methods
    of the superclass but it's not common.
     
    Sudsy, Oct 23, 2003
    #5
  6. Patel

    bm Guest

    I believe you have some problem understanding the late-binding concept.
    In C++ early-binding is the default behaviour. So what you are asking
    would work fine by upcasting an object to its' base class. To do
    late-binding
    in C++ you must use the keyword virtual in the base class method.
    But in Java late-binding is the default behaviour. So if you override a
    method
    in the parent class you are going to have a polymophic behavior.
    To switch off the default late-binding in Java you should declare a method
    static, final, or private (private implicitly is final).

    Spend some time reading chapter 7 of 'Thinking in Java' at
    http://www.faqs.org/docs/think_java/TIJ3_c.htm



    "Patel" <> wrote in message
    news:...
    > Is there a way to directly access a Super-Class method from an
    > instance of Sub-class.
    >
    > eg.
    > public class ClassA
    > {
    > void Perform()
    > {
    > System.out.print("ClassA");
    > }
    > }
    >
    > public class ClassB extends ClassA
    > {
    > public static void main(String[] args)
    > {
    > ClassB objB = new ClassB();
    > ClassA objA = (ClassA) objB;
    >
    > objA.Perform();
    > }
    >
    > void Perform()
    > {
    > System.out.print("ClassB");
    > }
    > }
    >
    > This code would print out -- ClassB
    >
    > But what I am looking at is, Is there a way to make a Perform() call
    > from instance of ClassB and achieve the result as -- ClassA.
    >
    > I am new to OO programming.
    >
    > Thanks,
    > Sajid
     
    bm, Oct 23, 2003
    #6
  7. Re: Calling parent's method even though it has been overridden inthe child

    Behzad,

    All method invocation in Java is done polymorphically--based on the
    true type of the instance, not on the declared type of the variable or
    value through which the invocation is made. Casting changes only the
    static or compile-time type associated with a value, not the real type
    of the instance to which the variable or value refers.

    As for rationale, I don't know, but that's how it is in Java.

    Note that in C++ the same would happen if the function was "virtual"
    (which makes the member function selection polymorphic, just as in
    Java) but not for non-virtual member functions.

    Also, in C++ you can use the namespace operator to select any
    (applicable) member function in the inheritance lattice whereas in
    Java you can only select the method from the class's superclass.

    Randall Schulz


    bm wrote:
    > I think I read it in 'Thinking in Java' that this is one of the
    > design differences between Java and C++. Even though you cast a
    > reference of Class B to a reference of Class A, a call to Perform
    > method will still result in a call to Class B. This is done in
    > purpose. Read 'Thinking in Java' to understand the rational behind
    > it.
    >
    > hope this helps.
     
    Randall R Schulz, Oct 23, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patel
    Replies:
    1
    Views:
    441
    Harald Hein
    Oct 23, 2003
  2. Replies:
    0
    Views:
    383
  3. Replies:
    2
    Views:
    395
  4. Bruno Desthuilliers
    Replies:
    12
    Views:
    507
    Raymond Hettinger
    Sep 24, 2007
  5. wink
    Replies:
    4
    Views:
    1,223
Loading...

Share This Page