amitshekhariitbhu / android-interview-questions

Your Cheat Sheet For Android Interview - Android Interview Questions and Answers
https://outcomeschool.com
Apache License 2.0
11.2k stars 2.22k forks source link

clarification on Integer class Hierarchy #108

Open SHAMSRIZWAN opened 2 years ago

SHAMSRIZWAN commented 2 years ago

Hi, its written ,-> It should be noted that standard class hierarchy does not apply to generic types. It means that Integer in List is not inherited from - it is actually inherited directly from

but when see the class hierarchy it clearly extending The Number class can you eloborate on this , And thanks for collecting all the resources ,

MalaRuparel2023 commented 4 months ago

Generics in Java: Generics in Java provide a way to create classes, interfaces, and methods that operate on objects of specified types. They allow you to parameterize the types used in your code. For example, List is a generic type where T is the type parameter representing the type of elements in the list.

Inheritance in Java: Inheritance is a fundamental concept in object-oriented programming where a class can inherit fields and methods from another class. In Java, every class (except for Object) has exactly one direct superclass (single inheritance).

Now, let's address the statement you provided: "It should be noted that standard class hierarchy does not apply to generic types." This statement might refer to the fact that when you have a generic type, such as List, the type List itself is not part of the class hierarchy in the same way as non-generic classes.

For example, List is not a subclass of List. Instead, it's a separate type that is parameterized with Integer. However, the elements contained within a List are indeed instances of Integer, which does inherit from Number.

So, when you see List, it doesn't directly inherit from List in the class hierarchy, but it does contain elements of type Integer, which does inherit from Number.

Here's a simple example to illustrate this:

List integerList = new ArrayList<>(); integerList.add(10);

Integer value = integerList.get(0); // value is of type Integer, which inherits from Number

In this example, List is not inheriting from List, but the elements it contains (Integer) do inherit from Number.