CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@zion • 28 May, 2012
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 0

can 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.

@Anoop Kumar • 28 May, 2012
package sample;
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;
}
}
public class add {​
public static void main(String args[]) {​
A obj1 = new A();​
B obj2 = new B();​
C obj3 = new C();​
obj3.a = 1; // you ware creating object from obj1 and obj2 and then​
obj3.b = 2; //putting values in these and then calling obj3 😲​
obj3.c = 3;​
System.out.println("sum is" + obj3.sum());​
}​
}​


and value 0 is not garbage value its default value of class member of Integer type that is 0.​
@zion • 28 May, 2012 thanx......
@zion • 28 May, 2012 what is use or need to create object for A and B..we can directly create for C
@Anoop Kumar • 28 May, 2012
zion
what is use or need to create object for A and B..we can directly create for C
You got that.. there is no use now... 😀
@zion • 28 May, 2012 thanx😀
@oyetayo micheal • 26 Feb, 2019

why it is that multiple inheritance is not allowed in java except when dealing with interface

@supriya VN gowda • 19 Jan, 2020

class A{

int a;

int b;

void add(){

int c=a+b;

System.out.println("The sum of two numbers is"+c);

}

}

class B extends A{

int d;

void add(){

int e=c+d;

System.out.println("The sum of three numbers is"+e);

}

}

class C extends A{

void add(){

System.out.println(e);

}

}

class demo{

public static void main(String[] args){

A a1=new A();

a1.add();

B b1=new B();

b1.add();

C c1=new C();

c1.add();

}

}

@Chebet Sarah • 24 Jan, 2020 • 1 like

import java.util.Scanner;

class A {

    int x, y, z, sum;

    Scanner sc = new Scanner(System.in);

    void input() {

        System.out.println("Type your three numbers :");

        x = sc.nextInt();

        y = sc.nextInt();

        z = sc.nextInt();

    }

}

class Main extends A {

    void add() {

        sum = x + y + z;

    }

    void display() {

        System.out.println("The Sum is :" + sum);

    }

    public static void main(String args[]) {

        Main obj = new Main();

        obj.input();

        obj.add();

        obj.display();

    }

}


@Mohana Chandra • 25 Jan, 2020

The following is the java program  to add given  three numbers using single inheritance:

class A

{

 public int c=10;

}

class B extends A

{

    public int a=20,b=30;

    public void sum()

    {

     int d=a+b+c;

     System.out.println("sum is:"+d);

    }

}

class Sum

{

    public static void main(String[] args) 

    {

        B b= new B();

        b.sum();

    }

}


@Karshil Sheth • 26 Jan, 2020

Program:- 

class Parent

{

  int number=14;

}

class child extends Parent

{

    public int var1=40,var2=50;

    public void addition()

    {

     int sum=var1-var2+number;

     System.out.println("Addition is as: "+d);

    }

}

class Minus

{

    public static void main(String[] args) 

    {

        child b= new child();

        child.addition();

    }

System.out.println("Happy Coding!!!");

}


@Durgesh Srivastava • 31 Jan, 2020

import java.util.Scanner;

class A {
int a, b, c, sum; 

Scanner sc = new Scanner(System.in);
void input() { 

System.out.println("Enter Three Numbers :"); 

a = sc.nextInt();

 b = sc.nextInt(); 

c = sc.nextInt(); }
}

class Main extends A {
void add() {

  sum = a + b + c;

 }
void display() {

 System.out.println("The Sum is :" + sum);

 }
public static void main(String args[]) { 

Main obj = new Main();

 obj.input(); 

obj.add();

 obj.display(); 

}
}

@Divisha Madupalli • 12 Feb, 2020

import java.util.Scanner;

class A {
int a, b, c, sum; Scanner sc = new Scanner(System.in);
void input()

 {

 System.out.println("Enter Three Numbers :"); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); 

}
}

class Main extends A {
void add()

 { sum = a + b + c; }
void display() {

 System.out.println("The Sum is :" + sum);

 }
public static void main(String args[]) { 

Main obj = new Main(); 

obj.input();

 obj.add();

 obj.display();

 }
}

12.2k views

Related Posts

@Ankita Katdare · Apr 12, 2015

Catching up with the trend of developing self driving cars, two Professors from Gujarat's Amiraj College of Engineering have built their own version of a driverless vehicle in India called...
4.6k views

@Dhruti · Oct 30, 2009

hey guys!! i need the code for a scientific or graphing calculator in c or c++. please help!! and also suggest me with a topic for embedded technology
3.2k views

@Ankita Katdare · Jun 7, 2015

IES or Engineering services exam is taken by thousands of aspirants each year. We need to build a repository of the best book books for different subjects of the IES...
5.4k views

@Alloyau · Oct 18, 2011

Hello everyone, I want to alloys steel with silver or gold or platina. How can I do this? I read in a book that by making a battery using a...
3.1k views

@sisir47 · Sep 24, 2011

Is it a single phase motor or three phase motor and why? I want to know which motor is used in ceiling fan and washing machines and mixer - ac...
14k views