4

I have had a burning question for a long time now, relating to the Java syntax and why it allows this:

Object object1 = new Integer();

Why can we declare child classes using Object as a reference? Also, let's take a look at this piece of code:

public class Parent{}    
public class Child extends Parent{}

public static void main(String[] args){
   Parent var1 = new Child();                
   Object var2 = new Child();
   Object var3 = new Parent();                
}

All three cases will run, as I have tried this. The question is, does it matter whether I use Object to reference a new object/variable or the actual class name? Also, how does the reference type selected affect the program and code? Does a certain reference type like "Object" take up less memory or make the program more efficient? Thanks in advance

T. Trifonov
  • 43
  • 1
  • 5
  • 4
    Yes it does matter, you can't access Child or Parent class methods with var2, var3. Compiler wont let it happen. – ajay.patel Jun 09 '17 at 22:28
  • The actual object will always stay the same, regardless which type you use to access it. So `Object obj = new Person ();` won't be converted to Object, it's always a Object of the Type Person. You Just don't see the Methods of Person. – Felix Jun 09 '17 at 22:47

4 Answers4

3

The question is, does it matter whether I use Object to reference a new object/variable or the actual class name?

Yes, it does. If you use the general interface/class name for your reference it will allow you to use dynamic dispatch, which means to decouple the client code from concrete implementations. Let's say you have Cat and Dog classes which extend Animal class. Both classes overide makeNoise() method. Then you may write a code:

Animal animal = animalFactory.getAnimal(); 
animal.makeNoise();

Which is not dependent on a concrete implementation of animal: it may be Dog or Cat whatever you need. The advantage of this approach is that your code is less prone to changes. Less changes -> less bugs. But it is important to mention, that you may only call the methods which are declared in the variable type. So, if your variable is Object type only Object methods are available to be called.

Vasiliy Vlasov
  • 3,316
  • 3
  • 17
  • 20
0

Because public class Parent{} is the exact same as

public class Parent extends java.lang.Object {
}

The compiler adds the "extends java.lang.Object" if you don't specify any super type.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • But what effect does the reference type have on the programming? Does an Object reference take up more memory and computing power than say a Parent reference? Or is it just bad programming to declare everything as Objects? Thanks in advance – T. Trifonov Jun 09 '17 at 22:26
  • 1
    It isn't very useful to make everything an `Object`, (the reference type has no effect on memory, computing power, efficiency or anything else) - `Object` doesn't have many public methods. Instead [**program to an interface**](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). – Elliott Frisch Jun 09 '17 at 22:28
0

For clarification:

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

As Oracle Doc

And in terms of OOP that's called type polymorphism which means by example:

If S is a subtype of T, the subtyping relation is often written S <: T, to mean that any term of type S can be safely used in a context where a term of type T is expected As this WIKI said.

Because of that any data types subtype can be assigned to Object instance

Fady Saad
  • 1,169
  • 8
  • 13
0

In terms of efficiency I don't think it makes any difference since you have that instance of the object type anyways.

The main advantages of using it might be to make Polymorphism. For instance

Object var1 = new GrandParent();                
Object var2 = new Child();
Object var3 = new Parent();
List<Object> object_list = new ArrayList<>();
object_list.add(var1);
object_list.add(var2);
object_list.add(var3);

Of course this doesn't make much sense since you wouldn't have any advantage. But if you used an Interface you could call a method without specifing the type. Something like this:

IPerson var1 = new GrandParent();                
IPerson var2 = new Child();
IPerson var3 = new Parent();
List<IPerson > object_list = new ArrayList<>();
object_list.add(var1);
object_list.add(var2);
object_list.add(var3);

for(IPerson person : object_list) {
    person.laugh();
}

In this situation you could apply different algorithms inside each concrete class. This is a Strategy Design Pattern.

César Ferreira
  • 681
  • 1
  • 5
  • 12