super « Constructor « Java Class Q&A

Java Class Q&A
1.abstract class
2.Base class
3.class hierarchy
4.class name
5.class version
14.equal method
15.extend Class
19.inner class
21.main class
26.object reference
28.parent class
31.Private Field
35.Static Class
39.Wrapper Class
Java Class Q&A » Constructor » super 

1. What's wrong with using super() in a constructor in Java?

When I run static analysis on the following code:

  public ExtractDBScripts(String resBundleName)

2. Why does this() and super() have to be the first statement in a constructor?

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why? For example:

public class MyClass {
 public MyClass(int x) {}

public class MySubClass extends MyClass ...

3. One-arg ctor A(x) implicitly invoking super() and NOT super(x)

class A{

    A(int x){}

class B extends A{

    B(int x){}

    public static void main(String args[]){

4. Refering to "this" while invoking super constructor?

I have a class A and write a subclass B. A has only one constructor which is parameterised. B has to call this super constructur of A. Now I want to use ...

5. unnecessary to put super() in constructor?

isnt this one automatically put by the compiler if i donĀ“t put it in a subclass's constructor? that means i dont even should care about it? in some articles they put it ...

6. Is it possible to do computation before super() in the constructor?

Given that I have a class Base that has a single argument constructor with a TextBox object as it's argument. If I have a class Simple of the following form:

public class ...

7. Do you put a super() call a the beginning of your constructors?

This is a question about coding style and recommended practices: As explained in the answers to the question unnecessary to put super() in constructor?, if you write a constructor for a ...

8. How to resolve 'Implicit super constructor classA() is not visible. Must explicitly invoke another constructor'?

I am having a class 'ClassA' which is having private constructor.

public final class ClassA{
  private ClassA{

  public static void main(String[] arg) }{

9. How does the keyword "super" work, when called from a constructor?


class MyException1
static String str="";

 public static void main(String args[])
 BufferedReader br=new BufferedReader(new InputStreamReader(;
 System.out.println("Enter your food");

 }catch(IOException e){
 System.out.println("Exception has been occurred"+e);

 }catch(BadException be){

10. Super constructor in java

Please explain

public class Contact {
    private String contactId;
   private String firstName;
    private String lastName;
    private String email;

11. What does super() method do?

What does the super method do?

public DataFetch (Context context){
Does this constructor make the context of ...

12. very specific java constructors and Object class problem

I have an array at actionTable.get(state). When I go to add an onject to the array, namely the Reduce, the properties of the reduce don't seem to go with it. The array is ...

13. java constructor in super classes

The following code is a testing program. Why i can't use A(int a), in this program?

public class A {
    int a;
    void meth(int b) {

14. cannot reference roomNumber before supertype constructor has been called

i have created two classes Standard and Family which extend abstract class Room, when compiling the Standard class i am met with the error "cannot reference roomNumber before supertype constructor has ...

15. object creation with super() and constructor

A call to super() which has to be the first statement in the constructor, creates [gross oversimplification] that part of the object which is inherited. In most subclasses a super() call is essential. A new Superclass() call creates a new object of the superclass completely separate from where one is working at present.

16. Is it possible for us to create a sub class object by calling super class constructor

Hi i am naveen i am new to this forum.Last week i have attended for one interview in that they asked me that is it possible for us to a create sub class object by calling super class constructor.If any body nows this answer please reply to me. thanks, naveen. [ February 03, 2008: Message edited by: naveen maddala ]

17. Super & constructor

Hi I'm doing a project in Java I've run into a problem. I have a base class with 2 final variables. class ClassParent { // the two final variables private final Object cons1; private final Object cons2; //constructor for cons cell protected ClassParent(Object a, Object b){ cons1 = a; cons2 = b; } DerivedClass extends ClassParent{ //constructor for DerivedClass //only initializes ...

18. superclass constructors

Hi. implicit calls are made for you. that's why they're called implicit. this means that, suppose your superclass has a default constructor - it will be called by the extending class's constructor (unless an explicit call is made). explicit calls are made by you, as the programmer. when you write your constructor, and want to call a specific constructor of the ...

19. super class constructor

I have the following code: class Snack extends VendingMachinePoly { private double cost; private int calories; // Parameterized constructor Snack(double c, int cal) { super(50, 50.0, c); cost = c; calories = cal; When I compile the program I get an error that states: "": cannot resolve symbol: constructor VendingMachinePoly (int,double,double)in class chapter9.VendingMachinePoly at line 26, column 5 However, the following ...

20. problem with constructor and super

21. super Constructor

Well I took some good advise and started my program over and did 1 line at a time to fix errors and not end of with a bunch. But now I got a problem. CheckingAccount has no errors but trying to compile CheckingAccountPlus I get this error revolving around Keyword "SUPER"? Also included is partial Test class, which I stopped with ...

22. invoking super constructors.

The constructor of class B will implicitly be called when you create a new object of class C. When you create a new object of class C, the following will happen: The constructor of class C will call the constructor of class A, which in turn will call the constructor of class B. Note that a subclass constructor will call the ...

23. regarding this or super call in constructors

Since all super types of your class might have internal fields, they have to have one of their constructors run. These super constructor runs have to be completed before your first assignment within your constructor. This makes perfectly sense. Suppose you could place the super() call anywhere within your constructor and a super type had protected member variables which are to ...

24. Calling method right before calling super constructor

As far as I know you can not put code before the call to super(). The call to super is added automatically to your constructor unless you specifically put the call there. The main reason to do this is if you need to call super with some parameters or if there is no parameter-less constructor. *third try's a charm....*

25. Super class constructor calls derived class overridden method?

Hi, Here is a piece of code and its output. Could not understand why the derived class method is called: class Test1 { public Test1() { System.out.println("In ctr of Test1()"); d(); } protected void d() { System.out.println("In Test1.d()"); } } public class Test2 extends Test1 { public int i = 2; public Test2() { System.out.println("In ctr of Test2()"); } protected void ...

26. Avoiding the "call to super must be first statement in constructor" message

Well, it's possible to chain multiple ? : together. But many people would consider that hard to read. You can also call a static method as part of the super constructor call: public MyClass() { super(method(foo)); ... } private static int method(int foo) { ... } Put whatever you want inside the method, as long as it doesn't access instance variables. ...

27. using superclass constructor

public class GraphicalDice extends PairOfDice { public GraphicalDice() { super(){} // This super class constructor gets called automatically, if nothing is specified in first line super(3,4); // Call the constructor from the // PairOfDice class, with parameters 3, 4. initializeGraphics(); // Do some initialization specific // to the GraphicalDice class. } super(7, 8); //can I make call like this ? . ...

28. Superclass non parameterized constructor

I have extended a BaseClass to SubClass. In BaseClass, I have defined parameterized consstructor, but there is no non parameterized constructor defined. Since I have defined parameterized constructor, non parameterized constructor will not be created automatically and I am required to define it. If I dont create a non parameterized constructor in BaseClass and extend this BaseClass to SubClass. If I ...

29. call constructor of super class?

30. over ridden method and super constructor

public class SuperTest{ public SuperTest(){} public SuperTest(int i){} public void testMethod(){ System.out.println("This is test method in super class"); } } public class SubTest{ public SubTest(){ super.SuperTest(); } public void testMethod(){ System.out.println("This is test method in sub class"); SuperTest sup = new SubTest(); sup.testMethod(); } } When I run this code it prints output "This is test method in sub class" Why ...

31. What is the use of SUPER call in the derived class constructor?

hi all, Let us say that we have a class A as follows. Class A { private int a; public int b; } And class B, which extends class A. Class B extends A { int c; } Now, if I create an object for B, then will it be having the member variable 'b' as we have inherited? The answer ...

32. Super class constructors

Since you are calling a constructor of a class using super() or this(), you don't have an object of that class yet.. So, you don't have access to instance variables or methods of that class. So, while instantiating a class using super() or this(), you have access only to their static members since they are loaded when jvm loads the class. ...

33. Super class constructors

Hi all, why do we have to call the super class constructors in order to use the instance variables and methods of sub class.Even if we want to use the instance variables inside the constructor of the sub class,we cannot do so unless we have the constructors of all the super classes run already. Why is so? Why is it necessary ...

34. call to constructor via super();

class Atom { Atom() { System.out.print("atom "); } } class Rock extends Atom { Rock(String type) { System.out.print(type); } } public class Mountain extends Rock { Mountain() { super("granite "); new Rock("granite "); } public static void main(String[] a) { new Mountain(); } } The output of the code is : atom granite atom granite when Mountain constructor is executed the ...

37. Confusion on super constructor ...

First of all, please don't ever have a class named "super". Also, many of your fields and arguments are missing types; I'm assuming that this is not your actual code. As to your actual question, what you put in your classes depends on what the classes are supposed to be about. So in your "super" class, you put in fields and ...

39. Alternative to 'super(this)' in a constructor?

In addition to what the others have said, there's no reason to pass this to a constructor. The c'tor already has a this, and it's referring to the same object. There's just one object, not a separate parent and child object. (Though, as already pointed out, it doesn't make any sense for Receiver to extend Sender. A Receiver is not a ...

40. this() and super() invocations in constructor bodies

Hi, Could someone please explain why it is not allowed to explicitly call this() or super() in a constructor body anywhere as opposed to the first statement in the constructor (which in turn implies that this() and super() can not be used together) ? Also, If the constructor is a constructor for an enum type, it is a compile-time error for ...

41. Implicit super constructor undefined.

public static void main(String[] args) { Cena2 x = new Cena2(); x.morder("Alguien"); } } As they see treatment use the functions inheriting them, but it leaves the following error to me: **Implicit super constructor Galleta() is undefined. Must explicitly invoke another constructor. In this line: public Cena2() { System.out.println("Haciendo una galleta de chocolate..."); } I do not understand very well to ...

42. Sequence of calling super's constructor

Thank you. This piece from the link you specified: [quote] Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is ...

43. Constructors and super(...)

When you are calling super() in your child class, you are calling the super class of child, which is ONLY parent 2. It goes directly to Parent2's constructor, which ONLY takes 5 arguments, 4 ints and a String data type. So in the child class, you would have to call super(int arg1, int arg2, int arg3, int arg4, String arg5) Otherwise, ...

45. call constructor of the superclass

46. Why super constructor call exist?

Hello tivrfoa, it's the only way to initialize the (including but not limited to private) state of the super class fields without knowing the internal workings of the implementation. Also, if you were to initialize the super class state, it would break capsulation; you could not make changes to a class without potentially breaking subclasses. With kind regards Ben

47. This() and Super()? Constructors? Need help

In response to topic: this is a keyword that refers to the current object. So if your code is this(x) then it will call another constructor in your class and pass the value of the x variable to it. The keyword super is used to refer to the parent class. So if you code is super(x) then it will call a ...

48. sub class calls super in constructor

51. Need help understanding "super" with constructors

The first statement of a constructor can be one of the following three things: 1) an explicit call to another this( ... ) 2) an explicit call to super( ... ) 3) something else in case 3) the compiler inserts an implicit call to super(), i.e. a call to the constructor of the super class without any parameters. If no such ...

52. regarding this or super call in constructors

You can do this using byte code. (not advised!!) However if you feel you want to move this() or super() there is another way to do what you have in mind. One thing you can do is call static method in the this/super line which means you can execute code before this/super is called.

53. Don't need call to super class constructor

54. Implicit super constructor is undefined.

55. Check params on constructors that use super()

private static final String[] VEX_MSGS = { "File type not supported", "Structure version not supported", "Filename format is wrong" }; int code; public ValidationError(int code) { this.code = code; } public String getMessage() { if ((code >= 0) && (code < VEX_MSGS.length)) return "error code " + code + ":" + VEX_MSGS; else return "error code " + code + " ...

56. Super with This in same constructor

Hi All, I tried to use Super and this keyword in same constructor but it is not working.It gives the exception like constructor call must be the first. I know in base class's constructor super class's constructor should be first statement but also can we not use same class's constructor as second statement in the constructor.? Thnkx

57. Super constructor problem

You have probably subclassed an object. If you dont call the superclass constructor, Java makes an implicit super() call. The super class probably doesn't have a noarg constructor defined. I am guessing you have defined a constructor in the super class that takes an argument. If you hadn't Java would have added an implicit constructor Just add a no arg constructor ...

58. constructor of a superclass

59. Constructors and Super

I wrote a class and a subclass - both immutable. The superclass only has one constructor and the subclass also has one constructor (though it is different than the superclass). I need for the subclass constructor to take some numbers, perform some operations and call the the constructor of the superclass. Of course this wont work because super() has to be ...

60. super in constructor

if you want do not do it! it will not really effect anything. over there you should be just calling the default constructor of the object class. Now in other cases, that is, when the class is extending some other object you could have something as follows: public A(String name){ super(name) // continue... } I feel it is usefull sometimes and ...

61. Superclass with no constructors

I am using third party libraries to do my development. The base class I am extending my class from does not have any constructors (with or without arguments). So, during compilation I get the following error message "Implicit super constructor Foo() is undefined. Must explicitly invoke another constructor." So, I declared a new constructor with argument in my class but still ...

62. How to get constructor of superclass of a superclass?

Hi, Ive got 3 classes and I need to resolve constructor of superclass of superclass: class A{ A(i){} } class B extends A{ B(i,d){ super(i) } class C extends B{ C(i,d){ } I need to resolve constructor from A class inside a constructor of class C. How can I do it? Can you help me here:)  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.