Just a few remarks:

Using default constructors super() gets called automatically, so the
B() { super() }

part of your code is not required. (The compiler provides a default
constructor, if you do not provide any other constructor)
The first line of the default constructor is always a call to super(),
even if you do not write it explicitly.
So, taken your example

class A
{}

class B extends A
{}

would be sufficient.

Example:
class A
{
    protected String name;
    public A( String name )
    {
        this.name = name;
    }
    
}

class B extends A
{
}

the above code fails to compile. Reason: class A does not have a default
constructor, (as there is an explicit one defined ) but B does only have
a default constructor, which tries to call super(), which is not
existent.

Why the code behaves like that (as it was seen in the original question)
?

This has nothing to do with virtual methods, but rather with the fact,
that Java works with objects by reference.

public class Test
{
        static public void main( String[] args )
        {
                A aClass = new B();
                System.out.println( aClass.toString() );
                B bClass = new B();
                System.out.println( bClass.toString() );
                        Object o = bClass; 
                System.out.println( o.toString() );
    }
}

class A
{
        public String toString()
        { return "Class A"; }
}

class B extends A
{
        public String toString()
        { return "Class B"; }
}

The code will write "Class B" three times, as Java works with your
objects as references internally, and does not really care about the
variable type you use in your code.
to class the method of the superclass, use super.<methodname>, like :

class B extends A
{
        public String toString()
        { return super.toString(); }
}

> -----Original Message-----
> From: Sunil Kumar [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, March 13, 2001 6:45 PM
> To: Balabaskaran, Kanagasabai; [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: RE: A very Interesting Question
> 
> 
> - 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
> 
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to