1

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();
   }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
A_Elric
  • 3,508
  • 13
  • 52
  • 85

6 Answers6

2

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.

Community
  • 1
  • 1
user1071979
  • 1,701
  • 2
  • 12
  • 25
1

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;
    }

}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
kosa
  • 65,990
  • 13
  • 130
  • 167
1
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);
     }

 }
Imaky
  • 1,227
  • 1
  • 16
  • 36
1

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

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.

devang
  • 5,376
  • 7
  • 34
  • 49
0
 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.

Zsolt János
  • 491
  • 8
  • 18