Skip to content

Tag: oop

Correct Use Of Factory Pattern?

I have been doing research on the factory design pattern and was wondering if the below example, while not “textbook” is technically a correct use of it. Is there another design pattern that may fit the situation better? Answer IMO: Looks like the given code example actually implemented the Factory Method pattern with using the Method Object solution Because

Update constructor using method

I have a game app and I want it to update the constructor strength value when I call the levelUp() method. But when i do System.out.println(hero.hp); after calling levelUp() it returns the first value. Answer One way to always get an up-to-date value for a calculation that depends on other variables/inputs is to calculate it on demand (useful for simple

How can I override object methods inside interface?

All classes in java extend the Object class implicitly. But that doesn’t concern interfaces. Interfaces can only extend other interfaces, but no classes. However, I can override object class methods inside my interface. Can you please explain in simple words how is this even possible? Is there any use case for this? Answer Interface is a just contract. It says

Expose `final static Set` of a class

I have a class that has: public static final Set<String> IDS = new HashSet<>();, whose values are initiated in a static block after running some query (Therefore I can’t declare it as unmodifiableSet). Now that other classes need to use IDS but apparently I don’t want to let them get direct access to it to avoid IDS being changed by

How to add items to array of objects using loops

I’ve got a multiple student objects I want to write into with a CSV file containing their details. I’ve set each row of the CSV file to an array then was going to split each entry of the array into another array and use that to set the attributes of the object. However, each time I try, I get a

Runnable interface instantiation

I am following the book Head first java and was reading about threads and came to know about the Runnable interface. Now we know an interface can’t be instantiated directly. Its members are implemented by any class that implements the interface , but below code made bouncers over my head. The book declares the instance of the Runnable interface and