• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Java - C++ differences in function overriding

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why my java program is not behaving same as C++?
==================================
C++ program
==================================
#include <stdio.h>

class base
{
public:
void func1(){
func2();
}
void func2(){
printf(" I am in base:func2() \n");
}
};

class derived : public base
{
public:
void func1(){
base::func1();
}
void func2(){
printf(" I am in derived:func2() \n");
}
};

void main()
{
derived *d = new derived();
d->func1();
}

==========================================================================
Result:
I am in base:func2()
==========================================================================

==================================
Java program
==================================
public class Test
{
public static class base{
public void func1(){
func2();
}
public void func2(){
System.out.println(" I am in base:func2() \n");
}

}

static class derived extends base{
public void func1(){
super.func1();
}
public void func2(){
System.out.println(" I am in derived:func2() \n");
}
};

public static void main(String[] args){
derived d = new derived();
d.func1();
}
}


===========================================================================
Result:
I am in derived:func2()
===========================================================================
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your question... with Java all non-static methods are polymorphic. When you call a method on a instance, it will call the latest overridden version of that method, for the class type of that instance, regardless on how it is being referred to.

This is not true for C++. For C++... for polymorphism to be supported by a method, that method must be declared as "virtual".

Henry
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
This is not true for C++. For C++... for polymorphism to be supported by a method, that method must be declared as "virtual".



And, insanely (but we are talking C++ so what do you expect?!), this applies to destructors. So, if you do not declare your destructor virtual, then subclasses do not get destructed properly.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic