• Register
1 vote
498 views

Problem:

Hello guys,

Please check mine below code snippet of abstract class which is producing a warning the type parameter animal is hiding the type. What does the warning mean? How can I fix the problem?

abstract class Habitat<Animal> {
    public abstract void getAreaInSquareYards(Animal k);
}

Please help me out if possible. I am looking forward to have a legit solution from you.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

Well, before anything else we need to understand the Generics And Parameter Type Hiding in Java. Generics is a vital ingredient for a tensile and cunning design. When used prudently, generics make a programming language look good and efficiently alongside. The second thing we need to be cleared is the type parameter. The type parameters are usually defined inside < > sign. 

As far as your program is concerned, Animal is not a generic type but a concrete implementation in your abstract class. The java compiler is looking for a generic type parameter but it does not appear there.  That’s the reason you are getting the error. Why don’t you try this?

abstract class Habitat<K>{
    public abstract void getAreaInSquareYards(K k);
}

Good Day!

13 9 6
94,260 points
0 votes

Solution:

Do you somewhere have a class or interface named T, or are you exercising T as a concrete type name somewhere instead of as a type parameter (which imply you might have forgotten somewhere else, for example in an enclosing class, to seclude that T is a type parameter)? I can reform your problem with this:

class T {  // A concrete type T
}

interface B<T> {  // warning: The type parameter T is hiding the type T
}

interface A<T> extends B<T> {  // warning: The type parameter T is hiding the type T
    T getObject();
}

In case I remove class T, it vanish

The List interface is also gentile. Ensure that you are not also exercising T for the generic type in your class. Note that in http://docs.oracle.com/javase/6/docs/api/java/util/List.html, they exercise "E" for the class generic parameter and "T" for the toArray() generic parameter. This confines the overlap.

public class MyList<T> implements List<T> {

// V1 (compiler warning)
public <T> T[] toArray(T[] array) {
    // in this method T refers to the generic parameter of the generic method
    // rather than to the generic parameter of the class. Thus we get a warning.
    T variable = null; // refers to the element type of the array, which may not be the element type of MyList
} 

// V2 (no warning)
public <T2> T2[] toArray(T2[] array) {
    T variable = null; // refers to the element type of MyList
    T2 variable2 = null; // refers to the element type of the array
}

Suppose an interface with a generic system such as:

<T> Output<T> doSomething(Input<T> input);

In case you attempt to override it as:

@Override
public <Object> Output<Object> soSomething(Input<Object> input){
    /*..*/
}

The compiler notify you:

The type parameter Object is hiding the type Object

What it implies is that "Object" is a generic label here, it is not java.lang.Object. In case you altered Object type to V or whatever arbitrary letter, that would not occur. However, you are exercising and appelative which collides with an actual identified class, hence compiler cautions you that this Object of you is disguise what otherwise would be understood as the common Object class.

10 6 4
31,120 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: Please provide me with a response to this >Java is not abstract and does not override abstract method.
asked May 9 Florina Gulnar 8.5k points
0 votes
1 answer 5 views
5 views
Problem: An abstract class can be extended, so so all class members(variables+methods)of an abstract class can be invoked/used from a subclass that extends the abstract class. So, why does JAVA prohibits from creating an object of the abstract class?
asked Apr 25 ochena 23.3k points
0 votes
1 answer 28 views
28 views
Problem: I am new to the OOPS concepts. Is it compulsory for abstract classes to have abstract methods?
asked Sep 1, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 22 views
22 views
Problem: I came across with this problem > Which of the following class definitions defines a legal abstract class
asked May 6 ummeshani 10.8k points
0 votes
1 answer 36 views
36 views
Problem: I came across with this problem > Which of the following declares an abstract method in an abstract java class?
asked Apr 29 ummeshani 10.8k points
0 votes
1 answer 3 views
3 views
Problem: I have no idea how to solve this, someone help >How many objects can be created from an abstract class
asked 3 days ago Sawyer Parviz 21.6k points
0 votes
1 answer 3 views
3 views
Problem: Need to facilitate with this >Do you have to implement all methods of an abstract class
asked May 10 Sawyer Parviz 21.6k points
0 votes
1 answer 11 views
11 views
Problem: Please offer me an answer to the problem >Does the abstract class have to implement all interface methods?
asked May 8 Ashok Dileep 6.7k points
1 vote
2 answers 24 views
24 views
Problem: Does any one know about this? Restoring and non restoring division algorithm with example:
asked May 1 PkGuy 23.5k points
0 votes
1 answer 8 views
8 views
Problem: Difference between abstract class and interface in java with real time example.
asked Apr 28 Ryan evans 1.5k points