I was practising some of the inheritance concept which i have learned from my book although i haven't completely studied inheritance yet but i thought to just write a simple program based on inheritance here is it
public class InheritanceInJava
{
public static void main(String args[])
{
SupperClass_A supperObj_A = new SupperClass_A(20,30,10);
SubClass_A subObj_A = new SubClass_A(10,20,30);
System.out.println(subObj_A.Add());
System.out.println(subObj_A.Multiply());
}
}
class SupperClass_A
{
int num1 ; int num2 ; int num3 ;
SupperClass_A(int a, int b, int c)
{
num1 = a ; num2 = b ; num3 = c;
}
public int Multiply()
{
return num1 * num2 * num3;
}
}
class SubClass_A extends SupperClass_A
{
SubClass_A(int a, int b, int c)
{
num1 = a ; num2 = b ; num3 = c;
}
public int Add()
{
return num1 + num2 + num3;
}
}
but it shows one error which is :
constructor SupperClass_A in class SupperClass_A cannot be applied to given types; { ^ required: int,int,int found: no arguments reason: actual and formal argument lists differ in length
Can anyone help me understand why this program is not working and whats the cause for this error ?
SuperClass_Ayou have to call it from the Constructor in your SubClass.SupperClasstoSuperClass) -- ASuperClass_Aconstructor has to be called when you make aSubClass_Ainstance, because everySubClass_Ainstance is also aSuperClass_Ainstance. YourSubClass_Adoes not specify whatSuperClass_Aconstructor should be called (usingsuper(a,b,c)), so the compiler assumes you want to call the no-arguments constructor. But there isn't a no-arguments constructor, so the compile fails.