0

Maybe the question have many people ask, but I still don't know how to get the answer.

I have A, B, C class.

C extends B, B extends A.

the main method in the C class.

If I want to call A constructor method with parameters in C class,

how can I do?

thanks you very much.

A class

 public class A {

    public A() {
        System.out.println("A construtor");
    }

    public A(int a, int b)
    {
        System.out.println("A.a:"+ a + "/B.b:"+b);
    }
 }

B class

 public class B extends A{
    public B() {
        System.out.println("B construtor");
    }

    public B(int a, int b) {
        System.out.println("B.a:" + a + "/B.b:" + b);
    }
 }

C class

 public class C extends B{

    public static void main(String[] str)
    {
        C c = new C();
    }

    public C() {
 //     super(1,2);
        // how to call A constructor with parameters 
        System.out.println("C constructor");

    }

 }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
dickfala
  • 3,246
  • 3
  • 31
  • 52
  • Can calling like this be avoided or is it absolutely necessary? It can get very messy very quickly. – Jorden Aug 31 '15 at 02:00

2 Answers2

1

Class C does not explicitly extend class A, it is only via class B that C extends A, hence you will only be able to access the constructors of A, via the constructors of Class B.

By using super(1,2) in Class C's constructor you will call Class B's constructor which has two int parameters. You would need to change class B's arg constructor to the following:

public class B extends A{
public B() {
    System.out.println("B construtor");
}

public B(int a, int b) {
super(a,b);
    System.out.println("B.a:" + a + "/B.b:" + b);
}

}

Bear in mind the following:

1)When no constructor is supplied to a class, java creates a default non-arg constructor.

2)If a class A extends class B, class B requires a constructor and the superclasses must be called at first.

RamanSB
  • 1,162
  • 9
  • 26
0

I don't think that you can, directly from C. It would need to be done by calling the appropriate B constructor within C's constructor. e.g.,

A class

 public class A {

    public A() {
        System.out.println("A construtor");
    }

    public A(int a, int b)
    {
        System.out.println("A.a:"+ a + "/B.b:"+b);
    }
 }

B class

 public class B extends A{
    public B() {
        System.out.println("B construtor");
    }

    public B(int a, int b) {
        System.out.println("B.a:" + a + "/B.b:" + b);
    }

    // ****** added ******
    public B(int a, int b, boolean ignore) {
        super(a, b);
    }
 }

C class

 public class C extends B{

    public static void main(String[] str)
    {
        C c = new C();
    }

    public C() {
        super(1, 2, true);
        System.out.println("C constructor");

    }

 }

Curious -- can you give a solid reason for why you'd want to do this?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • If I add public void callAConstructor() { super(1,2); } in class B. It will show "Constructor can must be the first statement in a constructor" – dickfala Aug 31 '15 at 01:53
  • @dickfala: and that is correct: a super constructor call **always** must be done first. Please see edit to answer. – Hovercraft Full Of Eels Aug 31 '15 at 01:54
  • I just study java . I just think the problem. So I don't know if we encounter the problem, how to do . thanks. – dickfala Aug 31 '15 at 01:55
  • @dickfala: note that [this has been asked many times before](http://stackoverflow.com/questions/1878558/jump-over-parent-constructor-to-call-grandparents). I have changed my answer to a community wiki because of this. – Hovercraft Full Of Eels Aug 31 '15 at 01:57