user311302 user311302 - 8 months ago 33
Java Question

Why is one of these classes not inheriting from the interface I made?

I'm trying to get a handle on inheritance and interfaces, and I've got two working files:

interface Speaker{

public String nameCalled();
public void describeSelf(String s);
public double randomIt(String s);

import java.util.Random;

public class testSample implements Speaker{

public String name1;
public String occ;

public testSample(String name1, String occ){
this.name1 = name1;
this.occ = occ;

public String nameCalled(){
return "My name is " + name1 + ".";

public void describeSelf(String s){
System.out.println("I, " + name1 + ", am a " + occ + ".");

public double randomIt(String s){
return Math.random() * s.length();

public static void main(String[] args){
testSample t = new testSample("John","student");


but then, with this:

public class testSample2 extends testSample implements Speaker{

String name;

public String nameCalled(){};
public void describeSelf(String s){
System.out.println("They call me " + name + ".");
public double randomIt(String s){
return Math.random() * s.length();


my compiler says "1 error found:
File: C:\Users\atimothy\Desktop\ [line: 1]
Error: constructor testSample in class testSample cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length"

I want testSample2 to inherit the variables from testSample but with different outputs. Also, what's the point of interfaces, if on testSample, I can just comment out "implements Speaker" and testSample still works.

Answer Source

Your base class has one constructor:

public testSample(String name1, String occ){

Thing is: a child class must always call one constructor from its super class. Even when you dont write down an explicit constructor, the compiler will add the default no-argument taking constructor.

Thus: the compiler tries to add that default ctor to the child class. And as said, that constructor must call some super constructor. But: there is no default constructor in your super class.

Long story short: you need something like:

public testSample2(String name1, String occ){
  super(name1, occ)

for example.

Think about it: inheritance is about an IS A relationship. A testSample2 is a testSample, too. And "testSample" needs those two parameters to be instantiated. So testSample2 must provide those two arguments somehow upon creation.