- In Java, every method is like virtual method of C++,
- Constructors don't get called in nested heirarchy in Java. You need to
call the base class constructor explicitly

Your c++ code will be equivalent to java code if you make following changes:


 Java Code :
 class A {
    A() {    this.show();   }
 void show() {    System.out.println("Base Class");    }
 };

 class B extends A {
 B() { super(); }
 void show() { System.out.println("Derived Class"); }
 };

 public class this_test {
 public static void main(String args[]) {  System.out.println("This is
Main");
 B test = new B(); }
 };

C++ :

class A {
  public:
  A() {   this->show();   }
  virtual void show() {   cout<<"Base Class"<<endl;   }   };

 class B : public A {
  public:
  B() { this->show(); }
  void show() {   cout<<"Derived Class"<<endl;   }   };

  int main(void) {   cout<<"This is main"<<endl;   B test;   }





> Hi all,
>
>           Today I came across a very interesting thing, It is really
amazing
> to me.
> This may sound very silly to you. But tell me why java behaving in this
> fashion. I feel the behaviour of C++ is correct.
>
>
> PS:    I may or may not be in this groups, so please include my email-id
too
> in your reply.
>
> Java Code :
> class A {
>    A() {    this.show();   }
> void show() {    System.out.println("Base Class");    }
> };
>
> class B extends A {
> B() { }
> void show() { System.out.println("Derived Class"); }
> };
>
> public class this_test {
> public static void main(String args[]) {  System.out.println("This is
Main");
> B test = new B(); }
> };
>
> Output :
> This is Main
> Derived Class
>
> C++ code:
> class A {
>   public:
>   A() {   this->show();   }
>   virtual void show() {   cout<<"Base Class"<<endl;   }
>   };
>
>  class B : public A {
>   public:
>   B() {  }
>   void show() {   cout<<"Derived Class"<<endl;   }   };
>
>   int main(void) {   cout<<"This is main"<<endl;   B test;   }
>
> Output:
> This is main
> Base Class
>
>
> Thanks,
> Balabaskaran.
>
>
>
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to