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

P

Patel

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
 
A

Anand Gopinath

+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.
 
B

bm

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.
 
R

Roedy Green

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.
 
S

Sudsy

Patel said:
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.
 
B

bm

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
 
R

Randall R Schulz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top