Inheritence (java vs. c++)

D

Digital Puer

I made the following table to help me (re)learn inheritence basics.
Can someone check if it's correct? This table requires a courier-like
font.


Java C++
---- ---
subclass' methods can yes, default must use "virtual" in front
override base class' of base class' method

subclass' methods can use "final" method don't use "virtual"
NOT override base class'

an empty base class use "abstract" in use "= 0" to make a
method front of method pure virtual method

subclass calls Base super.foo() Base::foo()
class' foo() method

determining an object's use instanceof use RTTI dynamic_cast
subclass when given a
pointer/reference to
base class

"prototypes" of a class' create interface create class definition
methods file .h file

constructors inherited? no no
 
R

Ron Natalie

Digital Puer said:
subclass' methods can use "final" method don't use "virtual"
NOT override base class'

Nope, can't be done in C++. Functions with the same signature are virtual
in derived classes whether declared so or not.
an empty base class use "abstract" in use "= 0" to make a
method front of method pure virtual method

I wouldn't exactly call this empty base class method. In both cases it denotes
an function that makes the class abstract (must be overridden to make a derived
class concrete). There's nothing that prohibits an implementation.
determining an object's use instanceof use RTTI dynamic_cast
subclass when given a
pointer/reference to
base class

or typeid for C++, depending if you want to know exact type or not.
"prototypes" of a class' create interface create class definition
methods file .h file

This is purely by convention.
constructors inherited? no no

No, but called in sequence in either case to initialize the subobjects.
 
J

jeffc

Ron Natalie said:
Nope, can't be done in C++. Functions with the same signature are virtual
in derived classes whether declared so or not.

I think he meant "don't use 'virtual' " in the base class. Then you can't
"override" them, you can only "redefine" them.
 
E

E. Robert Tisdale

jeffc said:
I think he meant "don't use 'virtual' " in the base class.
Then you can't "override" them, you can only "redefine" them.

Evidently,
you believe that override and redefine mean different things
in this context. Please elaborate.
 
A

Adam Jenkins

Ron said:
Nope, can't be done in C++. Functions with the same signature are virtual
in derived classes whether declared so or not.

This is only true in C++ if the base class method is declared virtual.
Basically, "virtualness" is contagious; you can't have a method with the
same signature be virtual in the base class, but non-virtual in the
derived class. If the base class is NOT virtual, then neither will the
derived class method be.

However, non-virtual C++ methods are NOT equivalent to final Java
methods. If a Java method is declared final, then an attempt to define
a method with the same signature in a derived class will generate a
compile error. In C++ you can override a non-virtual method in a
derived class. However because it's not virtual, if you call the method
via a base class reference or pointer type, the base class method will
be called.
 
A

Andy Fish

let's face it, c++ is the work of the devil, that's why java was invented in
the first place.

my suggestion would be to learn how java does it, then if anyone asks you to
do any C++, tell them you need danger money

just my two cents
 
D

Dale King

Digital Puer said:
I made the following table to help me (re)learn inheritence basics.
Can someone check if it's correct? This table requires a courier-like
font.


Just another piece of information you might want to add that is a difference
between Java and C++ that has been biting me a bit lately working in C++, is
what happens when the constructor calls a "virtual" method. For instance
consider this Java code:

class Foo
{
public void printMessage()
{
System.out.println( "Foo" );
}
public Foo()
{
printMessage();
}
}

class Bar
{
public void printMessage()
{
System.out.println( "Bar" );
}
public Bar()
{
printMessage();
}
}

In Java, if you create an instance of Bar you will see the following:

Bar
Bar

The equivalent C++ code will produce:

Foo
Bar

Here is an explanation from the C++ perspective:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.3

The C++ folks consider it too dangerous to call the subclass' version of the
method when its fields have not been initialized yet. But the flip side of
that is that it is quite useful to have the base class constructor ask for
information about the actual subclass and not allowing it is a big pain in
the butt (just look at how much work they have to go through to try and
simulate the behavior). So there is a trade-off and neither answer is the
"right" way to do it.
 
C

Chris Uppal

Digital said:
I made the following table to help me (re)learn inheritence basics.

*Creating* such a table may well be a useful exercise for you to consolidate
your memory of the rules in the two languages. But I'd advise against trying
to *use* such a table, i.e. treat it as "write-only".

You have to understand how/why each language works to use it, and when you are
writing -- say -- C++, there's no point (and lots of possibility for confusion)
if you are holding on to excess Java baggage at the time.

Your table is correct (subject to the quibbles that others have mentioned), but
you don't have a hope of making a complete list of the differences. For
example, method resolution in C++ is based on the name of the method only,
whereas in Java it's based on the name and signature. (Actually it's based on
the name, signature *and* return type -- but the Java language hides that from
you.) For instance (untested):

class Base
{
public:
virtual void aMethod(char c) { ... }
}

class Derived
: public Base
{
public:
// NB: does *not* override Base::aMethod(char)
virtual void aMethod(int i) { ... }

void anotherMethod()
{
this->aMethod('C');
}
}

If I've got this right (my C++ is a bit rusty) then the call to aMethod() in
Derived::anotherMethod() will resolve to Derived::aMethod(int), the compiler
will coercing the char argument into an int. In Java the "same" code would
resolve to the method in Base since the type of the argument would be taken
into account when trying to find the right method.

Another difference is that in C++ it is possible to override private virtual
methods in subclasses. Java treats the case differently.

What else ...? Oh yes, Java has interfaces, C++ has multiple inheritance.

-- chris
 
J

jeffc

E. Robert Tisdale said:
Evidently,
you believe that override and redefine mean different things
in this context. Please elaborate.

class A
{
public:
void f();
virtual g();
};
class B : public A
{
public:
void f(); // redefine, hide
void g(); // redefine, override
};
 

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,780
Messages
2,569,614
Members
45,292
Latest member
EttaCasill

Latest Threads

Top