java program to add three numbers in java using inheritance..please help
class A
{
int a;
int b;
}
class B extends A
{
int c;
}
class C extends B
{
int d;
public int sum()
{
d=a+b+c;
return d;
}
}
class add
{
public static void main(String args[])
{
A obj1= new A();
B obj2= new B();
C obj3= new C();
obj1.a = 1;
obj1.b = 2;
obj2.c = 3;
System.out.println("sum is"+obj3.sum());
}
}
the output it showed was sum is 0
then i wrote again the code
class A
{
int a;
int b;
void showab()
{
System.out.println(a+b);
}
}
class B extends A
{
int c;
void showc()
{
System.out.println(c);
}
}
class C extends B
{
int d;
public int sum()
{
d=a+b+c;
return d;
}
}
class add1
{
public static void main(String args[])
{
A obj1= new A();
B obj2= new B();
C obj3= new C();
obj1.a = 1;
obj1.b = 2;
obj1.showab();
obj2.c = 3;
obj2.showc();
obj3.sum();
System.out.println("sum is"+obj3.sum());
}
}output showed was 3,3 sum is 0can anyone plz tell what mistake i made in my two programs and what basic i m lacking that garbage value i,e 0 comes again n again what methodology should i adopt to overcome this.