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 7090489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:03:39+00:00 2026-05-28T08:03:39+00:00

import java.util.*; public class Test { public static void main(String[] args) { List db

  • 0
import java.util.*;

public class Test {
    public static void main(String[] args) {

        List db = new ArrayList();

        ShopDatabase sb = new ShopDatabase(db);
        sb.addEntry(new ProductEntry("film", 30, 400));
        sb.addEntry(new CashierEntry("Kate", "Smith", 23, 600));
        sb.addEntry(new ManagerEntry("Jack", "Simpson", 1, 700));
        sb.addEntry(new ProductEntry("soap", 18, 3));
        sb.addEntry(new ManagerEntry("Helen", "Jones", 60, 650));
        sb.addEntry(new CashierEntry("Jane", "Tomson", 15, 900));
        sb.addEntry(new ProductEntry("shampoo", 25, 5));
        sb.addEntry(new CashierEntry("Bill", "Black", 59, 300));
        Collections.sort(db);
        Iterator itr = db.iterator();

        while (itr.hasNext()) {
            Entry element = (Entry) itr.next();
            System.out.println(element + "\n");
        }
    }
}

public abstract class Entry implements Comparable {

    public String name;
    public int id;

    public Entry(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public int getId() {
        return this.id;
    }

    public int compareTo(Object entry) {
        Entry temp = (Entry) entry;
        int difference = this.id - temp.id;
        return difference;
    }

    public String toString() {
        return this.id + " " + this.name;
    }
}



public class People extends Entry {

    private String name;
    private String familyName;
    private int id;
    private int salary;

    public People(String name, String familyName, int id, int salary) {
        super(name, id);
        this.familyName = familyName;
        this.salary = salary;
    }

    /*
     * public String toString() { return this.id + " " + this.name + " " +
     * this.familyName + " " + this.salary; } /*public int compareTo(Object
     * entry) { Entry temp = (Entry) entry; int difference = this.id - temp.id;
     * return difference;
     * 
     * }
     */

}


public class ProductEntry extends Entry {

    public String name;
    public int id;
    public int stock;

    public ProductEntry(String name, int id, int stock) {
        super(name, id);
        this.stock = stock;
    }

    /*
     * public String toString() { return this.id + " " + this.name + " " +
     * this.stock; } /*public int compareTo(Object entry) { Entry temp = (Entry)
     * entry; int difference = this.id - temp.id; return difference;
     * 
     * }
     */
}



public class ManagerEntry extends People {

    public String name;
    public String familyName;
    public int id;
    public int salary;

    public ManagerEntry(String name, String familyName, int id, int salary) {
        super(name, familyName, id, salary);
    }
}



public class CashierEntry extends People {

    public String name;
    public String familyName;
    public int id;
    public int salary;

    public CashierEntry(String name, String familyName, int id, int salary) {
        super(name, familyName, id, salary);
    }

    /*
     * public String toString() { return this.id + " " + this.name + " " +
     * this.familyName + " " + this.salary; }
     */
}

Can anyone please help when I have code like this the output is

1 Jack

15 Jane

18 soap

23 Kate

25 shampoo

30 film

59 Bill

60 Helen

but if I uncommenting toString in People and ProductEntry the output is

0 null Simpson 700

0 null Tomson 900

0 null 3

0 null Smith 600

0 null 5

0 null 400

0 null Black 300

0 null Jones 650

why name and id is becomming null ???? Thank you in advance

  • 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-28T08:03:40+00:00Added an answer on May 28, 2026 at 8:03 am

    The reason is name and id are null.

    When you retrieve this.id, it returns the value of this classes (ProductEntry) member variable rather than its super class (which is what you want in this case). If these member variables weren’t defined in ProductEntry, a call to this.id would retrieve the id from the super class, in this case Entry. This is known as Variable shadowing

    You can solve the problem in two possible ways. The first is to simply remove name and id member variables from the sub classes of Entry. This will mean this.id will access the member variable of Entry rather than its sub class ProductEntry. The other option is to simply assign these in the constructor, but as you already storing these values in the super class, I don’t think this is the best approach.

    In the spirit of encapsulation you also may want to consider making your member variables private (only accessible by the class there defined in), or protected (accessible by the class its defined in and its sub types).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
import java.util.Scanner; import java.lang.String; public class Test { public static void main(String[] args) {
import java.util.Collection; public class Test { public static void main(String[] args) { Collection c
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
I have the following program import java.util.*; public class Test { public static void
When compiling this import java.util.List; public class Test { public static class Subject {

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.