Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:15:13+00:00 2026-05-24T04:15:13+00:00

I had to do a Java program that said: Write a program in java

  • 0

I had to do a Java program that said:

Write a program in java to implement Inheritance. Your program should have the following structure

  1. Create a Base class to hold two Integers and a method to Display them.
  2. In the derived Class add another Integer and Display it.
  3. Create a method in the same derived Class to add the three numbers.
  4. Pass the values to the integers and Display the results.

and I’m not that good at Java, so I don’t know if what I did was right or wrong or what! And I think I didn’t understand what was wanted quite well. Here is what I came up with:

public class firstclass {
    int a=5;
    int b=6;

    public void Display (){
        System.out.println(a+b);
    }
}

public class secondclass extends firstclass {
    int z=0;

    public void Displaysecond (){
        System.out.println(z);
    }

    public void add (){
        z=a+b;
        System.out.println(z);
    }
}

public class mainOne {
    public static void main(String[] args) {
        firstclass call = new firstclass();
        secondclass call2 = new secondclass();

        call.Display();
        call2.Displaysecond();
        call2.add();
    }
}

It runs without any problems but I get “11” for the “System.out.println(a+b);” while a = 5 and b = 6.
Am I going about this correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:15:13+00:00Added an answer on May 24, 2026 at 4:15 am

    You have a few problems. As Grammin mentioned, your add function is not what your teacher wanted. Your teacher wanted you to a, b, and z. So

    public void add(){
        int sum = a + b + z;
        System.out.println(sum);
    }
    

    Alternatively, you could make void add() int add() and return sum.

    Secondly, you are not following Java convention. Your variables need to be more inappropriately named, your classes need to start with uppercase and your methods need to start with lowercase.

    Perhaps you should name your classes FirstClass, SecondClass, and MainClass?

    Your methods: display(), displaySecond(), and add()

    Your variables: a,b,c or num1, num2, num3. (for consistency)

    Also, call.display() should yield the same result as call2.display() because SecondClass is a subclass of FirstClass. Read this for a better understanding of inheritance.

    A side note: you should add your numbers in parentheses () for clarification and safety if you’re doing some sort of in-line math. For example

    public void display(){
        System.out.println((a + b));
    }
    

    instead of

    public void display(){
        System.out.println(a+b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a Java program that I run thousand times based on a loop
I'm trying to write a program in Java that when given an MxN matrix
I have a Java program using a basic Hibernate session factory. I had an
I have a Java program that makes a request to a web service that
I have a Java program that calls a C++ program to authenticate users. I
I have a c# program that access a java program by tcp. In my
I work on a Java program that should be compatibe with Java 5. I
I have a java program that runs a bunch of queries against an sql
I have writted a Java program that will be able to add customers to
I worked for a company that had both Java and .NET implementations of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.