Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to insantiate a variable that encompasses all subclasses of a class? From what I've read so far we must state what type the variable is before setting it equal to something:
Example:
ExampleObject1 object = reference to the object
But what if we wanted to make it so that we could set the variable to any instance or subclass of that object?
Yes, you can already do that.
A variable of type T (as long as T is a class/interface/enum/annotation) can hold a reference to any instance of the class T, or any instance of a class that extends or implements T.
For example, this works:
class MyClass1 {
// ... stuff goes here ...
}
class MyClass2 extends MyClass1 {
// ... stuff goes here ...
}
class Main {
public static void main(String[] args) {
MyClass1 object = new MyClass2();
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm wondering how I would get a class from a java.lang.Object object to a specific class, such as a Cat class I may write; not directly casting to Cat, but to automatically get the main class of the object and cast to the main class, since I may take in Object arguments to take in many kinds of classes. Would getting an object through Class<T>, getting T (the type of the main class) be possible?
Here's what I mean:
class Cat {
public String meowMessage = "meow";
public void meow() {
System.out.println(meowMessage);
}
}
public class Main {
public static void main(String[] args) {
Object obj = new Cat();
// Cat c = obj.getClass()...;
// ^ how to get the Class object and automatically cast to the main class (Cat)?
}
}
What you want would involve determining a generic parameter at runtime. This is not possible due to type erasure.
If I understand correctly, you need instanceof so you can write:
if (obj instanceof Cat) {
Cat cat = (Cat) obj;
cat.meow();
} else {
//do something else
}
It's not possible to cast to a type which is not known compile time and I don't think it would make any sense because casting is needed to access the members of the class instance statically from the code. But how to know what is accessible if the class is unknown?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Lets say I have three classes, A,B,C. B inherits A, is there a way to access the getVal method of Class A, in C?
class A {
getVal method
}
class B extends A {
}
Class C {
main() {
B x = new B
x.getVal?
}
Yes. Assuming the classes remain in the same package - x.getVal will work.
class A {
String getVal(){
return "from a";
}
}
class B extends A {
}
public class C {
public static void main(String [] args) {
B x = new B();
x.getVal();
}
}
It works - because of the default access modifier. Use the protected access modifier for inheritance.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In java, after using a particular wrapper class in the generics of that class, we can't use that particular wrapper class in any static method or instance method or instance variable of that class. And another problem is the constructor which can only accept Integer object is accepting Strings(or any other wrapper class object) too. Look at the code below,what is the reason behind these compilation errors?
public class Exp<Integer> {
Integer val1;
Integer val2=new Integer(2);//compilation error, cannot instantiate the type Integer
public Exp(Integer arg1){
System.out.println(arg1);
val1=arg1;
}
public void lol(){
// Integer intValue2=new Integer(2);//compilation error, cannot make static reference to the non static type Integer
}
public static void main(String[] args){
Exp obj1=new Exp(10);
// Exp obj2=new Exp("Hello world");
// Exp<String> obj3=new Exp<String>("sss");// The constructor takes Integer arg, then why is string arg working perfectly with it?
String str="10";
Character c=new Character('c');//works perfectly
Float f1=3.0f;
Integer intValue1=new Integer(2); //**compilation error, cannot make static reference to the non static type Integer**
Exp<Integer> obj4=new Exp<>(10); //**compilation error, cannot make static reference to the non static type Integer**
}
}
Here you are not using "wrapper class in the generics", you just named your generic type variable as an existing class in java.lang package which hides the original class. However you may still access the original class using fully-qualified name:
java.lang.Integer val2 = new java.lang.Integer(2);
The same for other places where you have compilation error. In general it's better to avoid such names which clash with java.lang classes. Probably you actually wanted to write something different, like
public class Exp extends SomeOtherGenericClass<Integer> { ... }
The type in angle brackets is a dummy which is substituted for the actual type later. It is common to use <T>. You have used a real type, <Integer> which hides the system class Integer, so Integer in your program is no longer referring to java.lang.Integer, leading to the error messages.
Your code should look like this:
public class Exp<T> {
T val1;
...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can someone pls let me know the output of this code?
public Class A {
public int count = 5;
public void test() {
// some code
}
}
public Class B extends A {
public int count = 10;
public void test() {
//some code
}
}
public Class Check {
A a = new A();
A b = new B();
public void myTestMethod() {
a.count; //should call A?
a.test; //should call A?
b.count; //which count is called here? compiler error?
b.test; //should call B?
}
}
a.count; => yes you can
a.test; => yes you can
b.count; => count = 5; its didn't shows the compiler error its return the 5.
b.test; => Yes
Java inheritance lets the extending test over-ride or hide methods from the class it extends. So if B extends A and both have the same method then when you call b.method() it will call that method in B.
Inside B you can choose which method is called by doing either method() or super.method() to specify whether to call the super implementation or not.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My code as Follows:
public class CalledClass
{
public static <T> List<T> conditionMethod(**<T> map,Class<T> tableName)
{
//B.L
return li;
}
}
public class PureCoding
{
public static <T> void main(String[] args)
{
Map<T,T> map = new HashMap<T,T>();
CalledClass.conditionMethod(map,HomeWorkPojo.class);
}
}
By this way I can't call method in CalledClass. How can I call the method. Here I want to pass Collection type Map and Class Name as Parameters to my conditionMethod().
In place of ** What type I will give.?
So...if I understand you correctly, you wish to pass a Map and Class to your method. In that case, the only generics you'd need are the bounds to the key and value of the map, and the type of Class you want (or wildcard):
public static <K, V> List<V> conditionMethod(Map<K, V> map, Class<?> tableName) {
// implementation to go here
}
Remember what generics actually provide - compile time safety with regards to types. They're not some magic bullet; you use them as they are intended to be used.