0

I just came across this amazing tutorial video about Java and I found out that Object class is the super class of all classes behind the scenes. So that easily deals with the problem of having multiple data types in the arraylist by simply declaring the Object class when dealing with the list itself.

My question is this : Why not just declare Object object = new SubObjectClass(); for all objects?

Is this because of some sort of performance inefficiency or memory issue that I have yet to come across? I found another question but I didn't see anyone explain why NOT just have Object class declaration?

I understand why it is there. I apologize if this is a basic question.

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
OrangeLime
  • 105
  • 3
  • 10
  • 6
    Try it, see how easy it is to access the properties of the class after you've done it... – MadProgrammer Mar 10 '15 at 05:21
  • JavaScript more or less does things the way you describe; you use `var` for all variables, and you don't specify the type name. This has advantages, but also disadvantages: (1) it makes it easy to assign a variable to an object of the wrong type, whereas in Java the strong typing lets you catch many of those errors at compile time; (2) whenever you call a method, the program has to do a search to find out whether the object has a method with that name, while I think in Java it just loads a virtual address from an array, which is a little quicker. – ajb Mar 10 '15 at 05:49

5 Answers5

2

Sure, you could do that, but then you'd have to typecast every time you attempt to access a member through a variable. That typecast check takes time to execute, as it needs to ensure an exception is thrown if the object's class doesn't match the typecast constraint.

By using the appropriately typed variables, much of that chewcking is moved to compile-time, allowing (almost) unchecked runtime access to members, as long as the object isn't null.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
1

One reason to not using it can be problem with comparing or assigning object to other object.

For example:

Object obj = new SubObjectclass();
SubObjectClass subobj = obj;// This will throw error. 
SubObjectClass subobj = (SubObjectClass)obj;//This will work.
vishal patel
  • 832
  • 1
  • 7
  • 21
1

You can diclare List of Objects like you said :

List<Object> list = new ArrayList<Object>();

Which will be universal list of 'Objects' can store any kind of Object.

But the problem arises when we try to retrive item from such list we will need to cast the retrieved object to the specific Object type. In order to process it further.

We will not sure which object going to get out of list.

Prashant
  • 4,474
  • 8
  • 34
  • 82
0

There is another problem with doing this is you can not access variable or method of subclass with object of superclass. It can only access variable or method of superclass

Here's example

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    Object obj= new subclass();
    System.out.println(obj.i);
    System.out.println(obj.abc());

    // your code goes here
}
}

class subclass
{
int a;
public int abc(){
    return 1;
}
}

Here are the error i receive when i try to compile it:

Main.java:13: error: cannot find symbol
    System.out.println(obj.i);
                          ^
symbol:   variable i
location: variable obj of type Object
Main.java:14: error: cannot find symbol
    System.out.println(obj.abc());
                          ^
symbol:   method abc()
location: variable obj of type Object
2 errors
vishal patel
  • 832
  • 1
  • 7
  • 21
0

Type erasure in Java creates a huge performance hit as everything has to be boxed and unboxed. Like already mentioned you can create collections of objects in C# as well but should be avoided.

What are the differences between Generics in C# and Java... and Templates in C++?

http://www.jprl.com/Blog/archive/development/2007/Aug-31.html

Community
  • 1
  • 1
SKall
  • 5,234
  • 1
  • 16
  • 25
  • I don't see how this answer is relevant to the question. – ajb Mar 10 '15 at 06:01
  • First, the question may have been suggested by a video that mentioned generics, but the question is not about generics. Second, type erasure is a feature of all generics, so you can't avoid it, and I don't think it causes a performance hit at all. You may be thinking of raw types, in which you say `List list = ...` without a type parameter. And raw types are irrelevant to the question, because the OP referred to `List`, which is not a raw type. – ajb Mar 10 '15 at 06:08
  • Type erasure happens in Java but not in C#. That is why Java generic performance suffers greatly. – SKall Mar 10 '15 at 06:13
  • OK, I see how it might be true, since some runtime checks that need to take place in Java may not in C#. I haven't studied the byte code. In any case, it's still far afield from the original question; he was asking why not just declare every object in Java to be of type `Object`, and we're here discussing why generics in C# are faster than in Java. – ajb Mar 11 '15 at 04:49
  • The question is tagged with C# as well and he made a reference to having multiple data types in array list. In Java declaring ArrayList just takes away the type safety as at runtime the types are erased anyways. In C# going from strongly typed lists to introduces a performance hit (casting and with value types also boxing/unboxing). – SKall Mar 11 '15 at 14:54