All Questions
Tagged with java inheritance
99 questions
2
votes
2
answers
265
views
Is it ok to extend utilities?
Apache Commons has StringUtils. It's great, but I wish it had a shuffle() method or similar
Is it ok to create my own StringUtils that extends Apache's StringUtils and adds the method (Apache's class ...
0
votes
5
answers
2k
views
Comparing Java objects with different member variables
I have a base class "People" which two other classes inherit from: Employee and Student. The Student class includes a GPA (type double) and the Employee class does not. I have an ArrayList ...
1
vote
5
answers
679
views
How does inheritance lead to higher coupling than composition?
One major reason for using composition over inheritance is, that inheritance leads to higher coupling. How is that?
In both cases members are exposed to the subclass (in case of inheritance) or ...
0
votes
0
answers
518
views
What is the Best Practice for handling multiple Entities that behave identically?
Because I have multiple entities with unique fields, I need multiple repositories for each one even though each Entity will be handled exactly the same. What is the best way to handle these separate ...
1
vote
1
answer
185
views
Is it an acceptable pattern to put derived classes inside an abstract base class in Java?
Suppose I have some Java code such as the following (in this case, the use of the name "interaction" is referring to interacting with an object in a video game):
public abstract class ...
1
vote
2
answers
939
views
Concept/Design question: Alternatives to switch/conditional statements and Enums
I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine.
I have two questions focused on ideal structure and design ...
6
votes
9
answers
2k
views
Java instanceof and Clean architecture
It seems to me that there is a conflict between clean architecture and the recommendation not to use instanceof. Consider the following code:
class ParentEntity {
}
class AEntity extends ParentEntity ...
2
votes
3
answers
163
views
Java design: there is two interface: B extends A. A and B have one subclass each, named ABase and BBase, can I make BBase extend ABase?
there is two interfaces A and B:
public interface A {
}
public interface B extends A{
}
A and B have one subclass each:
public abstract class ABase implements A{
}
public abstract class BBase ...
2
votes
1
answer
321
views
Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?
I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case ...
2
votes
2
answers
167
views
How to structure classes for two distinct use cases that share key parameters
I have a Java application that needs to generate mathematically-defined 3D shapes for a voxel world (Minecraft specifically, but that's not important to the discussion). These include sphere, ovoid, ...
1
vote
2
answers
1k
views
Is it wrong to extend an inner static class in java? (Builder pattern)
I'm working on a java project for the university. The project is a card game in which you travel around a 2D map and fight against some enemies. My part consists of creating the deck and the cards.
I ...
0
votes
2
answers
785
views
Object Oriented Design for chess
Recently I came across some article on Chess OOPS design.Following is some snippet from it:
public class Chess {
ChessBoard chessBoard;
Player[] player;
Player currentPlayer;
List<...
0
votes
3
answers
266
views
Java Inheritance Problem
I have the finance application where we can have different types of transactions, like balance enquiry, funds transfer, and pin change etc. Now the Transaction is a base class, and there are specific ...
6
votes
1
answer
2k
views
Why were default methods introduced to Java?
Was introducing default methods to java inevitable? As far as I know multiple class inheritance was not introduced to avoid difficulties with the method signature clash in base classes.
So we avoided ...
3
votes
2
answers
178
views
Composing and Inheriting from the Same Type
To start off with an example: I have a read-only Repository used for getting arbitrary values. This behavior can be implemented multiple ways.
I also want to allow opt-in mutation of the repository's ...
1
vote
4
answers
1k
views
Why have separate keywords for 'extends' and 'implements' in Java? [closed]
Short answer that I've come to accept:
Firstly, it helps with readability, being able to see which is the superclass apart from interfaces. Secondly, though 'extends' and 'implements' do the same ...
-3
votes
1
answer
93
views
In Java API documentation, what does it mean to inherit an abstract method? [closed]
Here's an example: In the documentation for java.util.HashSet, there's a list of "Methods inherited from interface java.util.Set", including equals, which is an abstract method in java.util....
28
votes
7
answers
10k
views
Polymorphism case study - Design pattern for morphing?
Imagine two classes:
class Person {
MarriedPerson marry(Person other) {...}
}
class MarriedPerson extends Person {
}
The idea is that a Person is automatically "morphed" to type MarriedPerson ...
1
vote
2
answers
197
views
Need good design: Anemic Model, Inheritance and Pattern Matching
I have Handler classes which accepts Queries and returns Results. Handlers is anemic. They accept input data bag and returns output data bag. Handlers can be many so I created common generic interface ...
4
votes
1
answer
2k
views
Delegate vs Forwarding in Java OOP
I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source:
https://en.wikipedia.org/wiki/...
3
votes
2
answers
178
views
When covariance becomes an issue, how can I restructure my code and still be clean?
Suppose I have the following interfaces, GameObject and Enhance.
GameObject:
public interface GameObject {
void prepare();
void use();
void cleanup();
}
Enhance:
public interface ...
12
votes
3
answers
17k
views
Is it anti-pattern to have inheritence in a dto?
Are data transfer objects or POJOs meant to be final or can they be extended and create hierarchies for them?
It is not clear to me if such a value class is properly designed only as a final class and ...
1
vote
1
answer
252
views
Avoiding "instanceof" and explicit casts when selecting applicable handlers for inheriting object
In my current project, I am trying to implement an environment to perform simulations of different workflows in a range of programs, websites, and mobile applications. These simulation subjects can ...
1
vote
1
answer
338
views
Best pattern to solve problem where objects only differ in one attribute
As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
-2
votes
3
answers
605
views
Does Java Have True Single Inheritance? [closed]
I've been doing some studying on the types of inheritance. From what I've learned:
Single inheritance refers to when a class inherits another class.
Multi-level inheritance refers to when a class ...
0
votes
1
answer
2k
views
Java convention - Implementing two similar functions for two different objects
I have two classes, let's call them Foo and Bar. They both extend different classes (Foo extends X, Bar extends Y), which have some common ancestor "way up" the inheritance tree, something like this:
...
4
votes
2
answers
229
views
How should I sub class a class that constructs its objects primarily using Static methods
I have a class, that takes a lot of esoteric parameters to construct an object. I didn't write the code and frankly speaking, I don't understand completely, all of it's nuances. There is a valueOf(...
-1
votes
1
answer
117
views
How to Model below Hiearchy with OOP
Note that I cannot use static inheritance due to language limitations (Java).
There is a general Building class. Each instance of Building has properties that exist regardless of instance variables (...
1
vote
5
answers
340
views
Inheritance as a specialization
I have a class called Book with fields such as title, type etc. I also have a class called Library that manages books.
Library has methods that:
Add a copy of a book on a shelf
Move a copy of a ...
4
votes
4
answers
6k
views
How to avoid code duplication while extending two umodifiable classes
I already have this core class structure that can not be changed:
class A {
//some basic fields and methods
}
class B {
//some another basic fields and methods
}
It is core classes and I'm ...
0
votes
2
answers
293
views
Classes as parameters
I would like to write a data structure implementation in Java that uses caches as a core part of its functionality, and I would like the user to be able to provide their own cache implementations that ...
0
votes
1
answer
533
views
How to design inheritance from abstract class which is not initiated but seemingly must be initiated?
I need to design a program in Java which calculates arithmetic expressions (only addition or subtraction).
Requirements:
1) abstract class Expression which contains abstract method calculate(...
3
votes
1
answer
515
views
Best design for classes that draw objects but do not inherit from JPanel
I'm doing the exercise 10.1, page 476 from the book Java: How To Program, Early Objects by Paul and Harvey Deitel (10th Edition).
Modify the MyLine, MyOval and MyRectangle classes of GUI to create ...
0
votes
3
answers
570
views
Overriding methods with stricter signature
I'm programming in Java and have the following problem:
I would like to do collision detection. For that, I need different types of BoundingBoxes. For the sake of example, let's say that I have ...
1
vote
2
answers
2k
views
Function that throws exceptions extending IllegalArgumentException
I have a try/catch block which looks like this :
try {
geoms.add(convertLineToGeom(ln));
} catch(IllegalArgumentException e) {
System.out.println("ligne n°" + counter + " : " + e.getMessage())...
10
votes
5
answers
4k
views
Is it ever okay to violate the LSP?
I'm following up on this question, but I'm switching my focus from code to a principle.
From my understanding of the Liskov substitution principle (LSP), whatever methods are in my base class, they ...
0
votes
3
answers
2k
views
Are objects that can pass more than one IS-A test really polymorphic?
A number of tutorials on polymorphism state that "Any object that can pass more than one IS-A test is considered to be polymorphic." I wonder what they mean by that, and if that's even a true ...
1
vote
2
answers
93
views
Why 'Package' type does not inherit 'AccessibleObject' type?
Below are the relevant types(in java) for annotation processing,
In addition to Field & Method types, Package type is also used in context of annotations with reflection, because these types ...
4
votes
1
answer
5k
views
Designing generic type inheritance hierarchy
I have now put another rephrased version of original question as requested by the user in the comments with class names mimicking real world scenario of postal office (though I dont know how real ...
6
votes
1
answer
2k
views
Access methods from two interfaces's implementation in a Class
I am trying to implement the following pattern in a Cache layer.
I'm splitting the implementation of possible Cache methods such as getUsers() in UserCache Class, getLikes() in PostsCache class.
But ...
0
votes
1
answer
614
views
What is called the act of extending a Java class with another class?
When we create a Java class for example B that inherits from another class e.g. A, we say that our subclass (B) extends the super class (A). What is this act called in programming. Simply put, for ...
3
votes
2
answers
710
views
Correct way to extend a hierarchy tree
I have the following tree currently to be implemented in Java.
My problems are the following:
How can I go about addressing the fact Admin needs to have all tier 4
logic from both branches of the ...
1
vote
1
answer
91
views
Object passed to super() referenced by subclass - Any Violations?
This is the code in question, comments point it out:
class Actor extends Entity {
private MutableVector2f position;
private MutableIdentity identity;
public Actor(MutableVector2f ...
4
votes
2
answers
2k
views
Inheritance and factory together?
I have a hierarchical data model and am trying to implement their CRUD operations in my Web Application.
Currently I have code inheritance for CRUD operations of my entities (resources) as follows:
...
10
votes
1
answer
788
views
Is there a situation where it would be better to use weak references instead of simple composition?
Although the Java docs specify, that Weak references are primarily for canonicalizing mappings, you will find many, many, many people on the internet stating, that the WeakHashMap is perfect for ...
3
votes
1
answer
2k
views
Is it bad practice to perform an "optional" interface inheritance?
Say... you're trying to write a networkCallback code in java:
public interface NetworkCallback {
void onNetworkResult1(Object object);
void onNetworkFailure(Object object);
}
you want to use ...
7
votes
3
answers
422
views
Composition of two classes with common inheritance root
Hope your day is going well.
I want to make the following composition of two objects with the same inheritance root:
There will be 3 basic operations on the Wallet class: add, getCards and ...
1
vote
1
answer
1k
views
Using old-styled inheritance over annotations in Java
What are the actual perks of using an annotation that adds a particular functionality to a class or a block of code (other than rapid development purposes of course)? This may come down to preferences ...
2
votes
2
answers
151
views
Recommended practice for sharing fields between domain models
I'm working with a model that currently looks like this:
class Sign {
enum Type { A, B }
Color color;
String textLine1;
String textLine2;
Type type;
Sign(Type type, Color color, ...
2
votes
3
answers
205
views
Data duplication, can it be an unavoidable practice in this example?
Say I have different employees of type Employee stored in a list inside a class SubCase.
public class SubCase{
protected ArrayList<Employee> employees;
...
}
SubCase represents a part ...