Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to create a basic Item class hierarchy.
Each Item will have a String name and an int itemID.
These variables will be set in each subclass of Item.
The Item class declares the two variables and each subclass of Item
(public class Hammer extends Item) will set the name and itemID.
For example, name = "hammer" and itemID = 01.
How would I do this efficiently and effectively?
The base class will declare two final private fields (name and itemid).
The values for these will be set via the base class constructor.
Each of the derived classes will call the base class constructor with the appropriate name and itemid value.
for example:
public abstract class Blam
{
private final int itemId;
private final String name;
protected(
final int itemId,
final String name)
{
this.itemId = itemId;
this.name = name;
}
}
public class Derived
extends
Blam
{
public Derived()
{
super(123, "nameValue");
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
public class Student {
public Student(String name){
do_smth(name);
}
public Student(){
this("Mike");
}
}
How force developers to use parameterized constructor only if value differs from default:
not calling new Student("Mike") but use for this new Student()?
The reason: we have constructor with 5 parameters. In most cases parameters are the same. But there are about 5%-10% cases when they differ.
So in order to avoid duplications, I would like to use such approach.
I know it maybe better to use smth like Builder pattern here. But I don't like it verbosity.
This may be implemented by using additional private constructor with a flag:
public class Student {
public Student(String name) {
this(name, false);
}
public Student() {
this("Mike", true);
}
private Student(String name, boolean defaultUsed) {
if (!defaultUsed && "Mike".equals(name)) {
throw new IllegalArgumentException(
"Tut-tut lil kid, it's pwohibited to set Mike's name outside defauwt constwuctor");
}
do_smth(name); // only if do_smth cannot be overridden in child classes
}
}
Note: method do_smth should be private or final so that it could not be overloaded in subclasses which is far more important than setting a limit on setting a name from specific constructor.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have Bike class if I have types of bikes like Mountain, Road, City, an eBike. How do I put to put these types of Bikes into code?
public class Bike {
private String type;
private String height;
private String name;
}
If the individual bike types would ot have different attributes other than type, I would use enum instead:
public enum BikeType {
MOUNTAIN, ROAD, CITY, E_BIKE;
}
And in your class (also, height should probably not be a string):
public class Bike {
private BikeType type;
private double height;
private String name;
}
Then, if you have all-args costructor declared, you can create an instance like this:
Bike bike = new Bike(BikeType.ROAD, 5.3, "my shiny new bike");
But, if you really want to use subclasses, declare classes like this:
public class Bike {
// common parameters
}
public class EBike extends Bike {
// eBike specific parameters
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to print the attributes from TestCar class by creating a public hackCar method in class Terminal. The hackCar method needs to take a TestCar as a parameter and print the attributes of TestCar. The caveat for this assignment is that I cannot touch anything in the TestCar class.
I am still struggling with printing the two private attributes in TestCar. How can I print the two private attributes from Test Car class by using the Test Car object as the parameter in the hackCar method?
Story class:
class Story {
public static void main(String args[]) {
TestCar testCar = new TestCar();
Terminal terminal = new Terminal();
terminal.hackCar(testCar);
}
}
class Terminal {
public void hackCar(TestCar other) {
System.out.println(other.doorUnlockCode);
System.out.println(other.hasAirCondition);
System.out.println(other.brand);
System.out.println(other.licensePlate);
}
}
class TestCar {
private int doorUnlockCode = 602413;
protected boolean hasAirCondition = false;
String brand = "TurboCarCompany";
public String licensePlate = "PHP-600";
}
Thanks!
Private fields are called 'private' because there is no way to get them. But you can make public getter for them:
class TestCar {
// Your 4 fields here...
public int getDoorUnlockCode() {
return this.doorUnlockCode;
}
}
Then in hackCar method change
System.out.println(other.doorUnlockCode); to this: System.out.println(other.getDoorUnlockCode());
So now you can access field doorUnlockCode through public getter.
Do the same for protected field hasAirCondition
Your methods Terminal.getdoorUnlockCode() and Terminal.getAirCondition() can't get to fields from another object, they must be in TestCar object
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to implement this singleton class in java . How can I
put these variables in this class?
This is one simple example for your singleton. Feel free to add setters and getters as you need it for the fields.
I'm not sure what the set-method in your diagram should do but maybe you don't need it anyway.
public class LibraryInfo {
private static final LibraryInfo instance = new LibraryInfo();
public static LibraryInfo getInstance() {
return instance;
}
private LibraryInfo() {}
private String name;
private int phone;
private String address;
private String openTime;
private String closeTime;
// getters
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the difference between this and .this when calling functions? And, what happens when this or this. is used?
Example:
class reference
{
public void object()
{
reference obj = new reference();
this.obj();
}
}
The Class.this syntax is useful when you have a non-static nested class that needs to refer to its enclosing class's instance.It is only used in cases where there is an inner class, and one needs to refer to the enclosing class
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
A good example
public class TestForThis {
String name;
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
class TestForDotThis {
String name ="in";
String getName() {
return TestForThis.this.name;
}
}
public static void main(String[] args) {
TestForThis t = new TestForThis();
t.setName("out");
System.out.println(t.getName());
TestForThis.TestForDotThis t1 = t.new TestForDotThis();
System.out.println(t1.getName());
}
}
Output will be
out
out