473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 19 '05 #1
8 4195

"Digital Puer" <di**********@hotmail.com> wrote in message news:80**************************@posting.google.c om...
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.
Jul 19 '05 #2

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Digital Puer" <di**********@hotmail.com> wrote in message news:80**************************@posting.google.c om...
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.
Jul 19 '05 #3
Ron Natalie wrote:
"Digital Puer" <di**********@hotmail.com> wrote in message news:80**************************@posting.google.c om...

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.

Jul 19 '05 #4
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" <ad**@remove.thejenkins.me.org> wrote in message
news:3F**************@remove.thejenkins.me.org...
Ron Natalie wrote:
"Digital Puer" <di**********@hotmail.com> wrote in message news:80**************************@posting.google.c om...
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.

Jul 19 '05 #5
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.

Jul 19 '05 #6
"Digital Puer" <di**********@hotmail.com> wrote in message
news:80**************************@posting.google.c om...
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-lit....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
Jul 19 '05 #7
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

Jul 19 '05 #8

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@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
};
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
5
by: john bailo | last post by:
For a c# web application, I created a user control that includes a form in control. The idea is, on the main Page, when the user clicks Submit from the master form, that makes the user control...
0
by: Jan Elbæk | last post by:
Hi, I would like to make a base form in my project - which (almost) all forms must inherit from. The baseform must have some visible elements (a toolbar, a topaligned panel and a picturebox and...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
7
by: vj | last post by:
Hello Group, I am C++/OOP newbie and was working on a project when i came accross this puzzleing problem with inheritence. C++ Code ================== class Parent {
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
6
by: bjwillykajilly | last post by:
ok, so we're to right this program(in bluej) that consists of a few classes that will ultimately simulate a simple billfold(consisting of credit cards) a main card class, a drivers license, an id...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.