Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling parent's method even though it has been overridden in the child

Reply
Thread Tools

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

 
 
Patel
Guest
Posts: n/a
 
      10-22-2003
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
 
Reply With Quote
 
 
 
 
Anand Gopinath
Guest
Posts: n/a
 
      10-22-2003

+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.
 
Reply With Quote
 
 
 
 
bm
Guest
Posts: n/a
 
      10-22-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-22-2003
On 22 Oct 2003 14:03:02 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      10-22-2003
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.

 
Reply With Quote
 
bm
Guest
Posts: n/a
 
      10-22-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
Randall R Schulz
Guest
Posts: n/a
 
      10-23-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine if a virtual method has been overridden? wink C++ 4 07-14-2008 03:37 PM
Can a base class know if a method has been overridden? Bruno Desthuilliers Python 12 09-24-2007 10:04 PM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 2 04-26-2007 09:34 PM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 0 04-26-2007 09:24 AM
Calling parent's method even though it has been overridden in the child Patel Java 1 10-23-2003 04:35 AM



Advertisments