All Questions
Tagged with java inheritance
94
questions
0
votes
2answers
162 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 ...
3
votes
5answers
259 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
3answers
144 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
1answer
132 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
2answers
130 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
2answers
178 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
2answers
157 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
3answers
204 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 ...
5
votes
1answer
425 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
2answers
131 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 ...
0
votes
4answers
436 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
1answer
83 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
7answers
9k 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
2answers
112 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
1answer
782 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
2answers
172 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 ...
7
votes
3answers
6k 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
1answer
89 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
1answer
249 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
3answers
438 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 ...
4
votes
4answers
4k 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
1answer
684 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:
...
63
votes
11answers
59k views
Why is it good to split a program into multiple classes? [closed]
I'm still a student in high school (entering 10th grade), and I have yet to take an actual computer course in school. Everything I've done so far is through books. Those books have taught me concepts ...
4
votes
2answers
218 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(...
0
votes
3answers
47k views
Using super keyword to access super class members
I have following questions about the super keyword in java:
I have a code below with classes:
A Extends no class contains integer variable i
B Extends class A contains integer variable i
C Extends ...
3
votes
3answers
3k views
Why does an interface extend an interface instead of implementing it?
In Java suppose that I have interface A:
public interface A {
// foo
}
I also have interface B:
public interface B extends A {
// foo + bar
}
Why does interface B extend interface A and ...
3
votes
5answers
2k views
Explanation of the definition of interface inheritance as described in GoF book
I am reading the first chapter of the Gof book. Section 1.6 discusses about class vs interface inheritance:
Class versus Interface Inheritance
It's important to understand the difference between ...
-1
votes
1answer
116 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
5answers
288 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 ...
0
votes
2answers
284 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
1answer
941 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 ...
34
votes
3answers
16k views
If I implement an Interface, is it called an Inheritance?
If my class implements an interface then can I say that I'm following inheritance? I know that when a class extends another class then it's inheritance.
0
votes
1answer
507 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
1answer
394 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 ...
1
vote
2answers
877 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())...
0
votes
3answers
496 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 ...
10
votes
5answers
3k 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
3answers
675 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
2answers
81 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 ...
0
votes
1answer
483 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 ...
4
votes
1answer
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
1answer
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 ...
3
votes
2answers
596 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 ...
7
votes
2answers
321 views
Java redeclare inherited interfaces?
While working on a small class called FractionNumber I found asking myself if I should implement an interface that I am already implementing. Sounds stupid, I know, but bear with me.
My class ...
1
vote
1answer
80 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
2answers
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
1answer
706 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
1answer
1k 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
3answers
378 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
2answers
188 views
Processing and sending processed data to super from child class constructor
I want to do some initialization in child class constructor and pass result to super().
But Java doesn't allow any processing in child class constructor before super() call.
Whats a good way to ...