Inheritence (java vs. c++)



 DEVELOP > c-Plus-Plus > Inheritence (java vs. c++)

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Digital Puer"
Date: 12 Nov 2003 12:37:55 PM
Object: Inheritence (java vs. c++)
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
.

User: "Dale King"

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 04:08:21 PM
"Digital Puer" <digital_puer@hotmail.com> wrote in message
news:80678590.0311121037.16b346e7@posting.google.com...

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.
--
Dale King
.

User: "Ron Natalie"

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 12:49:05 PM
"Digital Puer" <digital_puer@hotmail.com> wrote in message news:80678590.0311121037.16b346e7@posting.google.com...

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.
.
User: "jeffc"

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 12:57:12 PM
"Ron Natalie" <ron@sensor.com> wrote in message
news:3fb280a2$0$25241$9a6e19ea@news.newshosting.com...


"Digital Puer" <digital_puer@hotmail.com> wrote in message

news:80678590.0311121037.16b346e7@posting.google.com...


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.

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

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 01:16:45 PM
jeffc wrote:

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.
.
User: "jeffc"

Title: Re: Inheritence (java vs. c++) 14 Nov 2003 11:35:34 AM
"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:3FB2871D.2050508@jpl.nasa.gov...

jeffc wrote:

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.

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



User: "Adam Jenkins"

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 01:18:06 PM
Ron Natalie wrote:

"Digital Puer" <digital_puer@hotmail.com> wrote in message news:80678590.0311121037.16b346e7@posting.google.com...


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.

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.
.
User: "Andy Fish"

Title: Re: Inheritence (java vs. c++) 12 Nov 2003 01:20:33 PM
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
"Adam Jenkins" <adam@remove.thejenkins.me.org> wrote in message
news:3FB2876E.3080001@remove.thejenkins.me.org...

Ron Natalie wrote:

"Digital Puer" <digital_puer@hotmail.com> wrote in message

news:80678590.0311121037.16b346e7@posting.google.com...



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.


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.

.



User: "Chris Uppal"

Title: Re: Inheritence (java vs. c++) 13 Nov 2003 05:32:26 AM
Digital Puer wrote:

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER