Java - Extending Unknown Classes [duplicate] - java

This question already has answers here:
Extend a generic type in Java
(2 answers)
Closed 8 years ago.
I wanted to create a class that can extend an unknown class that is provided at runtime. I thought that I could do something like this:
public class Foo<T extends Bar> extends T {}
but that doesn't work. Is there a way to do this?

Not without some crazy runtime code generation. Java's generics are not the same sort of thing as C++'s templates: Internally, T is simply treated as an Object, and up- and down-casted as necessary for the benefit of your code. So while a C++ template is instantiated for a particular T, and can decide at compile-time whether that works, in Java the machinery underlying inheritance requires that the actual base class be decided upon in the class definition itself.

Related

Implement an interface with the same name in Java [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 1 year ago.
I am recreating basic data structures as an assignment. I have an interface called Queue and I have to implement the same data structure with the same name (both are in different packages) I tried doing something like this but it gives a warning
Queue is a raw type. References to generic type Queue<E> should be parameterized
This is my code
public class Queue implements uo.mp2021.util.collections.Queue
What's it a Queue of?
Anything?
public class Queue<E> implements uo.mp2021.util.collections.Queue<E>
Something?
public class Queue implements uo.mp2021.util.collections.Queue<Something>
The point is that the interface is generic, so you need to decide whether you're providing a generic implementation, or a specific implementation.

Can we use multiple extends of class in java selenium? [duplicate]

This question already has answers here:
Java Multiple Inheritance
(17 answers)
Closed 2 years ago.
Please explain how to extend two classes from different packages.
public class Animal extends Herbivores extends Omnivores {
}
Multiple Inheritance or Diamond Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class.
Java does not support that.
There are multiple resources over the net which tells you the problem that arises if you are allowed to do that.
Coming to your problem, it seems to me like a fault in Design.
Animal should be a parent type rather than a child type. Both Herbivores and Omnivores are animals. Whereas an animal doen't necessary have to be either. It can be a carnivore also.

Why only java.lang. Object is given superclass in java? [duplicate]

This question already has answers here:
Why Object class is Superclass in java [closed]
(4 answers)
Closed 6 years ago.
Is there any reason sun microsystems make Object for all javaclass for superclass. I face the question my last interview. I hope, I can find answers here
Thanks
Following could be the reasons for this design decision,
By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.
Before generics was introduced, imagine the state of heterogeneous Java collections. A collection class like ArrayList allows to store any type of classes. It was made possible only by Object class hierarchy.
The other reason would be to bring a common blueprint for all classes and have some list of functions same among them. I am referring to methods likehashCode(), clone(), toString() and methods for threading which is defined in Object class.
Please check the below link. I hope it will answer your question.
Why object is super class in JAVA

when abstract class doesn't contain any abstract method [duplicate]

This question already has answers here:
Why use an abstract class without abstract methods?
(11 answers)
Closed 7 years ago.
I am still confused when abstract class doesn't contain any abstract method, what a purpose of it? why don't use regular class rather than abstract class if it doesn't contain any abstract method ? In fact, I was saw this situation is applied on java and libgdx library or perhaps for every library.
So, because this situation, I was thinking is it very important to know why use abstract class without abstract method rather than regular class.
When you make a class abstract (either with or without abstract methods), you are forcing the users of this class to create concrete sub-classes of it, since they can't instantiate it.
A user of an abstract class must create a concrete derived class.
This can be useful since it allows the author of an abstract class to introduce abstract functions at a later date. The amount of refactoring necessary at that time is then significantly reduced.

Meaning of Java syntax: class A extends B<C> [duplicate]

This question already has answers here:
The syntax <T extends Class<T>> in Java
(3 answers)
Closed 8 years ago.
What does this syntax mean in Java: class A extends B<C>? I came across this in an Android library and couldn't find it on google.
More specifically, this is what I had come across:
public class PullToRefreshExpandableListView extends PullToRefreshAdapterViewBase<ExpandableListView>
Generics with Class and Interfaces
Genrics is one of the core feature of Java programming and it was introduced in Java 5.
We can define our own classes and interfaces with generics type.
A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter.
Please refer this link for more information

Categories