Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to avoid a constructor of class A be extended to Class B?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to avoid a constructor of class A be extended to Class B?

    Abstract Class A{
    A(int)
    {
    }
    }


    Class B extends A{



    }

    now wen i compile class B. i get a error cannot find symbol: symbol is constructor A()


    Wat do i have to do now?


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    When a subclass object is constructed, the superclass's constructor must be invoked - either explicitly or implicitly. In your case, it's invoked implicitly, and implicit constructor is constructor without arguments. But A does not have such a constructor. Hence an error.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    By Default only non parametric construstor is called but since class A has parametric constructor you need to call it from class B

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    To add to what has already been said:
    When you make a class which has a constructor with or without parameters, and you do not specifically include a constructor (or several constructors) it is the same thing as saying "only allow the listed constructors". So in your example when B attempts to extend A, the error says that B is attempting to use a constructor that does not exist (the default no parameter one implicitly being called)
    Provide the no parameter constructor in class A or construct B with an explicit call to super(int)

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    Quote Originally Posted by jps View Post
    To add to what has already been said:
    When you make a class which has a constructor with or without parameters, and you do not specifically include a constructor (or several constructors) it is the same thing as saying "only allow the listed constructors". So in your example when B attempts to extend A, the error says that B is attempting to use a constructor that does not exist (the default no parameter one implicitly being called)
    Provide the no parameter constructor in class A or construct B with an explicit call to super(int)
    ok ..Guyz i took you were suggestion and modified the code as following:

    ************************************************** ***
    public abstract class Zee{
    Zee(int x)
    {System.out.print("Zee 's constructor is callled///..........");
    System.out.print("x value is " + x);
    }
    }

    class MTV extends Zee{
    private int a=10;
    MTV(int x)
    {
    super(x);
    }


    }
    import java.io.*;
    public class Callo
    {

    public static void main(String [] args)
    {
    MTV w= new MTV(2);
    }
    }
    now this Callo class wen compiled pops me error
    java 1 class ,interface ,or enum expected

    What s going wrong with my Callo class?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    Please use code tags when posting code to the forum. Help can be found on the Announcements page.
    Was Callo defined in it's own file?

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to avoid a constructor of class A be extended to Class B?

    Quote Originally Posted by jps View Post
    Please use code tags when posting code to the forum. Help can be found on the Announcements page.
    Was Callo defined in it's own file?
    Yes sir i hav put Callo class in Callo.java file n i compiled it. Am a beginner n am unable to figure out y it wont compile.

Similar Threads

  1. Instance Variable does't work in extended class?
    By jean28 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2013, 01:36 AM
  2. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. Creating an open-ended class that can be extended:
    By dedshaw1612 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 9th, 2012, 09:34 AM
  5. HOW TO AVOID JAR,CLASS FILE HACKING
    By huwijk in forum Java Theory & Questions
    Replies: 5
    Last Post: January 30th, 2012, 08:04 AM