Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default y superclass constructor is called automatically

    Java Code:
    class A {
    
        A() {
            System.out.println("Class : A");
        }
    }
    
    class B extends A{
    
        B() {
            System.out.println("Class : B");
        }
    }
    
    public class Myclass {
    
        public static void main(String[] args) {
            [COLOR="Red"]new B();[/COLOR]
        }
    }
    In this program when the red line is executed the constructor of class B is supposed to be called Only as far as I know........ but the constructor of super class is also called. y??????????????
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  2. #2
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    Guys I got the answer. Thnx n e ways :)
    If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-arguement constructor of the superclass. If the super class doesnot have a no-arguement constructor, you will get a compile-time error.
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

Similar Threads

  1. called external program does not automatically write file
    By nickvandewiele in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 03:38 PM
  2. My constructor not called
    By rdtindsm in forum New To Java
    Replies: 2
    Last Post: 09-20-2009, 02:38 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Replies: 0
    Last Post: 11-12-2007, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •