473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4227

"Digital Puer" <di**********@h otmail.com> wrote in message news:80******** *************** ***@posting.goo gle.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.
Jul 19 '05 #2

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Digital Puer" <di**********@h otmail.com> wrote in message news:80******** *************** ***@posting.goo gle.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.
Jul 19 '05 #3
Ron Natalie wrote:
"Digital Puer" <di**********@h otmail.com> wrote in message news:80******** *************** ***@posting.goo gle.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, "virtualnes s" 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.th ejenkins.me.org > wrote in message
news:3F******** ******@remove.t hejenkins.me.or g...
Ron Natalie wrote:
"Digital Puer" <di**********@h otmail.com> wrote in message news:80******** *************** ***@posting.goo gle.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, "virtualnes s" 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**********@h otmail.com> wrote in message
news:80******** *************** ***@posting.goo gle.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.prin tln( "Foo" );
}
public Foo()
{
printMessage();
}
}

class Bar
{
public void printMessage()
{
System.out.prin tln( "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(c har)
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::anothe rMethod() will resolve to Derived::aMetho d(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
4144
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 capabilities
5
2430
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 visible and gives it new functionality, but retains the look of the top half of the master page. I then hide the original submit button. Ok, now, after pressing the Submit button in the user control, I want to change the look of the master page.
0
1377
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 perhaps some more). Also. I need it to have a specielized constructor in the base. And the baseform must implement some methods (like Setstatusbartext) - and som default eventhandlers (like Closing), which must be virtual so that additional code can...
5
1422
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 death) Ray
16
3099
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
7
1985
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 inheritence. One more article I read was Allen holub's "Why extends is evil". http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html One general advice I found was that - extending behaviour is better done with interfaces and...
7
1601
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
2127
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 disagreement about whether is essential in C++". Are there any disadvantages of using multiple inheritence?
6
3862
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 card, and a calling card. Now, i'm not coming to you guys to finish my program, i'm coming to you guys so i can understand it(as it is my major, as of 2 days ago) class Card { public String name; public Card() { name = "";} ...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.