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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:34:40+00:00 2026-06-02T03:34:40+00:00

I’m stuck with a Java OOP problem. I have come up with some toy

  • 0

I’m stuck with a Java OOP problem. I have come up with some toy code to explain the problem. Here are my classes –

Class 1 – Car.java

public class Car {

    public void reportProblem(String problem){
        ReportUtil.reportVehicleInfo("Car", 4, problem); //4 is number of wheels
    }

    //bunch of other methods
}

Class 2 – Truck.java

public class Truck {
    public void reportProblem(String problem){
        ReportUtil.reportVehicleInfo("Truck", 6, problem);
    }

    //bunch of other methods
}

Class 3 – ReportUtil.java

public class ReportUtil {
    public static void reportVehicleInfo(String name, int wheels, String problem){
        System.out.println(String.format("%s %s %s", name, wheels, problem));
    }
}

Class 4 – Test.java

public class Test {
    public static void main(String[] args) {
        Car c = new Car();
        c.reportProblem("puncture");

        Truck t = new Truck();
        t.reportProblem("engine missing");
    }
}

I want to abstract the “reportProblem” method implementation in “Car” and “Truck” to a parent class. This is what I did –

Class 1 – Vehicle.java

public abstract class Vehicle {
    public String mName;
    public int mNumWheels;

    public void reportProblem(String problem){
        ReportUtil.reportVehicleInfo(mName, mNumWheels, problem);
    }

    public void setName(String name){
        mName = name;
    }

    public void setNumWheels(int numWheels){
        mNumWheels=numWheels;
    }
}

Class 2 – Car.java

public class Car extends Vehicle {

    //bunch of other methods
}

Class 3 – Truck.java

public class Truck extends Vehicle {

    //bunch of other methods
}

Class 4 – ReportUtil.java (No change made to this class).

public class ReportUtil {   
    public static void reportVehicleInfo(String name, int wheels, String problem){
        System.out.println(String.format("%s %s %s", name, wheels, problem));
    }
}

Class 5 – Test.java

public class Test {
    public static void main(String[] args) {
        Car c = new Car();
        c.setName("Car"); //NOTE : Can be missed!
        c.setNumWheels(4); //NOTE : Can be missed!
        c.reportProblem("puncture");

        Truck t = new Truck();
        t.setName("Truck"); //NOTE : Can be missed!
        t.setNumWheels(6); //NOTE : Can be missed!
        t.reportProblem("engine missing");
    }
}

This achieves what I want (I have abstracted the implementation of “reportProblem”). But I know this is not the best way to do it. One reason is that the “reportProblem” method should not be called without calling “setName” and “setNumWheels” methods. Otherwise ‘null’ will be passed. Is there a way of enforcing, using some OOP technique, the two methods calls (setName and setNumWheels) BEFORE reportProblem is called?

I hope I have made myself clear. If I am not, just let me know how you would have done it so that I can learn from it.

  • 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-06-02T03:34:42+00:00Added an answer on June 2, 2026 at 3:34 am

    Yes, make name and numWheels final and assign then in the constructor. So…

    Class 1 – Vehicle.java

    public abstract class Vehicle {
      public final String mName;
      public final int mNumWheels;
    
      protected Vehicle(String name, int numWheels){
        this.mName = name;
        this.mNumWheels = numWheels;
      }
    
      public void reportProblem(String problem){
        ReportUtil.reportVehicleInfo(mName, mNumWheels, problem);
      }
      ...
    }
    

    Class 2 – Car.java

    public class Car extends Vehicle {
    
       public Car(){
         super("Car", 4);
       }
     //bunch of other methods
    }
    

    Class 3 – Truck.java

    public class Truck extends Vehicle {
    
       public Truck(){
         super("Truck", 6);
       }
    //bunch of other methods
    }
    

    Also, public fields are not good OO practice, because they expose details of your class’ implementation that could be modified by users of the class. Those fields should be private. If the clients of the class need to know about them (or change them), then you should allow public getter (or setter) methods.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.