Using the book as analogy, suppose I have a book resource with the following API (there are also update and delete etc but for simplicity only show two here) Each of these API would call other APIs to get the result, rather than the typical CRUD database operation. And based on the requirement / existing framework constraint, there are separate
Tag: oop
Java: make static methods of one class mirror instance methods of another class
I have a POJO like this: As an alternative to Java reflection (which is quite slow in general), I would like to define a class with static methods like this: which enforces the creation/deprecation of a static method every time the Foo class is updated. For example, if the field bar2 is deleted in FooStatic, then the static methods getBar2()
How to show that a Catch block for the Parent-exception will handle subclasses as well
I have met this problem. I have created a class CatHandler, with 3 inner classes (ExceptionAlpha extends Exception, ExceptionBeta extends ExceptionAlpha, ExceptionGammer extends ExceptionBeta). These three exception subclasses are empty; they contain no code. All code should be written in CatHandler. Now the question is to write some code in CatHandler to show that ExceptionBeta and ExceptionGammer will be caught
Abstract Classes & Methods In Java
I am working on a Java exercise where a parent class is abstract, and the subclasses override the isSimilar method. This method should take a device parameter. In the Server subclass, this method should compare the Brand, Operating System, and Available Services. In the NetworkDevice subclass, this method should compare the Brand, Number of Ports, and Power Over Ethernet. The
Object is modified implicitly in one method and used by another
I have two methods meth1 and meth2. meth1 takes in an input of a set set1, returns a value, and also modifies set1. Now meth2 uses this set1 to compute another return value. The issue I see with this code is that the set modification is implicit and would be missed by a coder who is not careful enough. I
Java Inheritance, instanceof, casting – how to accept a children object throught parent object?
I have three classes: (Parent) Animal, (Child) Bird & Monkey Monkey has weight & two pet Animals, which can be another Monkey, or bird Bird has weight & a name (weight, name) Together, they form a TREE, where leaf nodes are Bird, and non-leaf nodes are Monkeys (SEE VISUAL) While recursively traversing this tree to find a bird with a
Avoiding circular depenency between Arena and Portal
I’m trying to resolve this ciruclar dependency while preferebly keeping both objects immutable. Basically, I need portal to be able to activate itself, and start the arena. Opposite, I need Arena to reset & close itself, and the portal. I found a solution by having two HashMap’s <Arena, Portal> and <Portal, Arena>, however I want to figure out if it’s
How to write a method which will take item array as input and return item with least price (considering discount as well)?
I am new to Java, and I am trying to declare another method in this class called getItemPrice, which will take item array as input and return item with least price (considering discount as well). So far, I have declared an array of 2 item objects in main method. This is my main method: As you can see above, each
Java: reading objects from file into different derived classes
I have class Employee, and two derived classes Hourly and Salary. Instantiations of those derived classes are placed in the same Array List called employeeList. When I read these objects from file using ois.readObject() how can I ensure the data ends up in the appropriate derived class? Thank you! Answer Note: I am not familiar with ObjectInputStream, so I am
abstractor method with object return type in java
I’m new at oop ,I want to pass object parameter of an abstract method but it gives me error, can anyone explain it to me and help me fix the error . Thanks for your help. Answer You cannot change the abstract method signature. You have created an entirely new method unrelated to the method you wanted to override. Use