Results 1 to 4 of 4
  1. #1
    VakhoQ is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Constuctor In Inheritance

    // ############ main.java ##############
    PHP Code:
    public class main {	
    	public static void main(String[] args ){    
    	       STD2 obj2=new STD2();	         		
    	}		
    }

    PHP Code:
    //########## STD.java ################
    public class STD{
    		   public STD(){ 
    			   System.out.println("Works STD Constructor!");
    		   }
    }


    PHP Code:
    //################# STD2.java #############
    public class STD2 extends STD{
        public STD2(){ 
    	System.out.println("Works STD2 Constructor!");
    	}
    }

    Si then the result will be:

    C:\Users\vakho\Desktop>javac *.java
    C:\Users\vakho\Desktop>java main

    Works STD Constructor!
    Works STD2 Constructor!


    Questions:
    1)Could I invoke(run) only STD2 Constructor here?... If i Cerate STD2 object (STD2 extends on ) STD , why STD Constuctor runs (Invokes)?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    1. No.
    2. Because in order to have a STD2 you need the bits from a STD, so it has to call a parent constructor.

  3. #3
    VakhoQ is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    1. No.
    2. Because in order to have a STD2 you need the bits from a STD, so it has to call a parent constructor.
    is there any other way to break this behavior of Java, In the other words - is there any other way to invoke only STD constructor? actually no?

    thanks a lot! :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    Nope.
    STD2 is a STD.
    It needs to call a constructor on STD in order for the STD bit of its object to be created correctly.

Similar Threads

  1. static constuctor calls?!
    By camel in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 05:43 PM
  2. JPA Inheritance
    By videanuadrian in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:44 AM
  3. using inheritance
    By senca in forum New To Java
    Replies: 4
    Last Post: 08-15-2010, 07:33 PM
  4. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

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