A_Elric
A_Elric

Reputation: 3568

Extending an abstract constructor?

So I've come across a bit of a snag in some code that I'm working with. Essentially I have the following three tidbits of code:

Abstract class:

public abstract class TestParent {
    int size;

    public TestParent(int i){
        size = i;
    }

}

Child Class:

     public class TestChild extends TestParent{
     public void mult(){
         System.out.println(this.size * 5);
     }

 }

Implementation:

public class TestTest {

   public static void main(String args[]) {
       TestChild Test = new TestChild(2);
       Test.mult();
   }
}

Upvotes: 1

Views: 3925

Answers (6)

kosa
kosa

Reputation: 66677

When you have explicit constructor defined in super class and no constructor without arguments defined, your child class should explicitly call the super class constructor.

public class TestChild extends TestParent{
        TestChild ()
        {
            super(5);
        }
    }

or, if you don't want call super class constructor with parameters, you need to add constructor with no arguments in super class.

public abstract class TestParent {
    int size;
    public TestParent(){

    }
    public TestParent(int i){
        size = i;
    }

}

Upvotes: 1

user1071979
user1071979

Reputation: 1791

Consider the following case of abstract class and extends implementation. https://stackoverflow.com/a/260755/1071979

abstract class Product { 
    int multiplyBy;
    public Product( int multiplyBy ) {
        this.multiplyBy = multiplyBy;
    }

    public int mutiply(int val) {
       return muliplyBy * val;
    }
}

class TimesTwo extends Product {
    public TimesTwo() {
        super(2);
    }
}

class TimesWhat extends Product {
    public TimesWhat(int what) {
        super(what);
    }
}

The superclass Product is abstract and has a constructor. The concrete class TimesTwo has a default constructor that just hardcodes the value 2. The concrete class TimesWhat has a constructor that allows the caller to specify the value.

NOTE: As there is no default (or no-arg) constructor in the parent abstract class the constructor used in subclasses must be specified.

Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum fields required to setup the class.

Upvotes: 2

Zsolt János
Zsolt János

Reputation: 511

 public class TestChild extends TestParent{
             public TestChild (int i)
             {
               super(i * 2);
             }

}

This code would use the double of i. This is an overriding, though i'm not sure what you want to ask.

Other solution:

 public class TestChild extends TestParent{
             public TestChild (int i)
             {
               super(i);
               this.size = 105;
             }

}

For this solution, size must be protected or public.

Upvotes: 0

devang
devang

Reputation: 5516

You code wont compile because your base class does not have a default constructor. Either you need to provide it in base class or you need to provide parameterized constructor in derived class and invoke super.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

Use super to call parent (TestParent.TestParent(int)) constructor:

public class TestChild extends TestParent{

    public TestChild(int i) {
        super(i);
    }

    //...

}

or if you want to use some constant:

    public TestChild() {
        super(42);
    }

Note that there is no such thing as abstract constructor in Java. Essentially there is only one constructor in TestParent which must be called before calling TestChild constructor.

Also note that super() must always be the first statement.

Upvotes: 1

Imaky
Imaky

Reputation: 1257

public class TestChild extends TestParent{
     public TestChild(int i){
         super(i); // Call to the parent's constructor.
     }
     public void mult(){
         System.out.println(super.size * 5);
     }

 }

Upvotes: 1

Related Questions