Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By cronnin
  • 1 Post By jim829

Thread: Constructor mistake plzz help

  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Constructor mistake plzz help

    i know im doing a lil or a big mistake but im cconfuse here may be with a java rule or dont know soo please help....

    i want to call parent class constructor from child but i failed


    Java Code:
    class A1
    {
        int x;
    	public A1(int x)
    	{
    		 super.x=x; 
    	}
    
    }
    
    class B extends A1
    {
          int x=20;
    	  public void fun()
    	  {
    	       int x=30;
    		   System.out.println("To print local variable that are made in this function just typpe x "+x);
    	       System.out.println("To print instance variable that are made in this class just typpe this .x "+this.x);
    		   System.out.println("To print instance  variable that are made in A1 class just typpe super.x "+super.x);
    	  }
            
    		  public static void main(String Dev[])
    		  {
    		      B b=new B();
    			  b.fun();
    		      A1 a=new A1(50);
    			  
    
    }
    }
    thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Constructor mistake plzz help

    i want to call parent class constructor from child but i failed
    Where is the code that is trying to call the parent class's constructor?
    Where is the constructor for the subclass?

    Read the tutorial: http://docs.oracle.com/javase/tutori...ubclasses.html
    Last edited by Norm; 01-27-2017 at 06:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Constructor mistake plzz help

    Java Code:
    A1 a=new A1(50);
    Here you have called constructor.

  4. #4
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Constructor mistake plzz help

    You don't call super from parent class but from a child class. That should be your first line in class B constructor.
    Also the constructor's for A and B differ in argument list. You have to pass value in line 32.
    Also, line 34 doesn't actually do anything. There is no fun() method for parent class.


    Java Code:
    class A1
    {
        int x;
        public A1(int x)
        {
         this.x = x;
        }
     
    }
    
    
    
    
    public class B extends A1
    {
          int x=20;
    
        public B(int x) {
            super(x);
        }
        
          public void fun()
          {
               int x=30;
               System.out.println("To print local variable that are made in this function just type x:"+x);
               System.out.println("To print instance variable that are made in this class just type this.x: " + this.x);
               System.out.println("To print instance  variable that are made in A1 class just type super.x: " + super.x);
          }
             
          public static void main(String Dev[])
              {
                  B b=new B(80);
                  b.fun();
                  A1 a=new A1(50); 
          }
    }

    Java Code:
    To print local variable that are made in this function just type x:30
    To print instance variable that are made in this class just type this.x: 20
    To print instance  variable that are made in A1 class just type super.x: 80
    vknehra10 likes this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructor mistake plzz help

    You can't override variables from super classes like you can methods. B extends A and they both declare the same field name, you can access
    them by using the appropriate type. In other words, you don't need to use super to do that.

    Assume B extends A

    B b = new B();
    b.x is b's version
    A a = b;
    a.x is A's version

    Regards,
    Jim
    Last edited by jim829; 01-28-2017 at 07:16 PM.
    vknehra10 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 10
    Last Post: 02-06-2011, 04:48 AM
  2. someone help me debug...this plzz
    By andys in forum New To Java
    Replies: 3
    Last Post: 11-28-2010, 11:17 AM
  3. plzz help me to solve this program
    By Gayathri12 in forum New To Java
    Replies: 2
    Last Post: 07-20-2010, 05:15 PM
  4. Replies: 1
    Last Post: 07-06-2010, 02:38 AM
  5. Doubt Plzz Solve it
    By ravjot28 in forum New To Java
    Replies: 3
    Last Post: 06-27-2008, 04:14 AM

Tags for this Thread

Posting Permissions

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