473,398 Members | 2,368 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,398 software developers and data experts.

Inheritance Doubt

21
Hi ,

I am migrating slowly from c++ to java. The output of the following program differs from c++ in java. It should be really easy for any java programmer to explain. Just an hint could help me to understand.


class base {
public void f(){
System.out.println("base.f()");
g();
}
public void g(){
System.out.println("base.g()");
}
}

class derived extends base {
public void g(){
System.out.println("derived.g()");
}
}

public class bade{
public static void main(String[] args){
base p = new derived();
p.f();
}

o/p in java -> base.f(), derived.g()
o/p in c++ -> base.f(),base.g().
Oct 10 '07 #1
2 1134
vinothg
21
I am sorry as i recalled now that all methods in java defaults to virtual. In C++, it needs to be done explicitly.
Oct 10 '07 #2
dmjpro
2,476 2GB
Hi ,

I am migrating slowly from c++ to java. The output of the following program differs from c++ in java. It should be really easy for any java programmer to explain. Just an hint could help me to understand.


class base {
public void f(){
System.out.println("base.f()");
g();
}
public void g(){
System.out.println("base.g()");
}
}

class derived extends base {
public void g(){
System.out.println("derived.g()");
}
}

public class bade{
public static void main(String[] args){
base p = new derived();
p.f();
}

o/p in java -> base.f(), derived.g()
o/p in c++ -> base.f(),base.g().
Yes buddy .............. at the beginning you are trying to understand the base concept of Java.
Really happy to see that :-)
Actually Java resolves these during runtime.
Have a look at "Class","Method" and it's relevant classes.
Then you will understand that by yourself.

Debasis Jana
Oct 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
2
by: Søren Holstebroe | last post by:
Hi there, I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in STL containers. I'm trying to port some old code I wrote with MS Visual C++ and it looks like there is a...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
2
by: Ravimama | last post by:
Hello All, I am planning to use inheritance for forms which have similar controls and properties. I tried to do it with a small example, but I am facing some issues. I created a base form...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
5
by: colint | last post by:
Hi I'm fairly new to c++ and I have a question regarding inheritance. I'm trying to create a class based on 2 inherited classes, e.g. class A { ... } class B: public A
1
by: Gary Wessle | last post by:
Hi is there some guide lines on how many level deep once can structure the inheritance, I have 3 level deep inheritance setup and just wondering whether this is the norm? each level is...
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
7
by: pradeep | last post by:
Hello friends: In the following setup, class A { ... }; class B { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.