-1

In below code what is difference between obj1 & obj2 ? My question is in syntax -

Classname1 objectname = new Classname2();

May be Classname1=Classname2

what is significance of Classname1.

// Superclass
class Base {
    public static void display() {
        System.out.println("Static or class method from Base");
    }

}

// Subclass
class Derived extends Base {


       public void print() {
         System.out.println("Non-static or Instance method from Derived");
   }
}


public class Test {
    public static void main(String args[ ])  {
       Base obj1 = new Derived();
       Derived obj2 = new Derived();
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
nikhil jain
  • 105
  • 9
  • Where is `Classname1 `? – akhil_mittal Jun 21 '15 at 09:40
  • It can be any class in java which i have declare some where. Mainly i want to ask what is difference between obj1 & obj2 in my code if any? – nikhil jain Jun 21 '15 at 09:46
  • 1
    Possible [duplcate](http://stackoverflow.com/questions/24807029/superclass-reference-to-subclass-object-showing-same-behaviour-as-subclass-refer) – Madhan Jun 21 '15 at 09:48
  • 1
    Possibly duplicates [this](http://stackoverflow.com/questions/1348199/java-hashmap-vs-map-objects) or [this](http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java). – T.J. Crowder Jun 21 '15 at 09:50

3 Answers3

1

In the line

Classname1 objectname = new Classname2();

Classname1 is the type of the variable objectname; Classname2 is the type of the object we're assigning to that variable. From that line we can infer that either Classname1 is an interface that Classname2 implements (like HashMap implements Map), or that Classname2 is a subclass (directly or indirectly) of Classname1, like HashMap extends AbstractMap.

In a comment you said:

what is difference between obj1 & obj2 in my code if any?

I assume you're talking about this code:

Base obj1 = new Derived();
Derived obj2 = new Derived();

There's no difference in the objects that are assigned to those variables, but there's a difference in the type of the variables and therefore the type of reference you have to the object: The object that obj1 points to has a print method, but you can't access it via obj1 because the type of the reference you have is Base, not Derived, and Base doesn't have print. In contrast, you can access print via obj2 because the type of the reference is Derived.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • you are saying i cant access print method of derived class via obj1. what happen if i have same print method in base class also.in that case which print method will execute.suppose i have situation like this http://pastebin.com/tJcpuj52 In this code output will be "hello from derived". Means via obj1 i have access print method of derived class not base class.it will be grate if you help me out of this confusion. – nikhil jain Jun 26 '15 at 11:08
  • @nikhiljain: Right, if you define `print` on the base class, then references of that type can access it. The one they access, in Java, is the `print` from the *object's* type (not the reference's type). That's called [polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)) (specifically, subtype polymorphism). – T.J. Crowder Jun 26 '15 at 11:17
  • So in my code object's type of obj1 & obj2 is derived right? can you tell me one think what is difference between object type & reference type. – nikhil jain Jun 26 '15 at 11:30
  • @nikhiljain: The type of the *object* is the type you used with `new`. The type of the *reference* is the type you used when declaring your variable (or method argument, etc.). – T.J. Crowder Jun 26 '15 at 11:31
  • @nikhiljain: For completeness about polymorphism: There are some languages (such as C++ and C#) where which `print` gets used may be the reference's type's `print` **or** the object's type's `print`, depending on how `print` is defined. A "non-virtual" method will use the reference's type; a "virtual" method will use the object's type. Java only has virtual methods. (C#, sadly, defaults to non-virtual methods and you have to explicitly declare them virtual.) – T.J. Crowder Jun 26 '15 at 11:32
  • suppose i have added static in both print method(base or derived class) at that time why obj1 access print from the reference type(not from object's type)whether static word make the method non-virtual – nikhil jain Jun 26 '15 at 11:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81638/discussion-between-nikhil-jain-and-t-j-crowder). – nikhil jain Jun 26 '15 at 11:48
  • @nikhiljain: Search for "override static java" here on SO and you'll find explanations for that. – T.J. Crowder Jun 26 '15 at 11:52
1

You want to know the difference in these lines:

Base obj1 = new Derived();
Derived obj2 = new Derived();

In both the cases the object constructed is of class Derivedbut references are different. You are able to use reference of type Base because Derived is also Base (assuming Derived extends Base) hence can be used. But which one should we prefer? The former one because it makes robust design.

Consider the Collections Framework in Java which has a List interface and two implementations: ArrayList and LinkedList. We can write our program to use a LinkedList or an ArrayList specifically. But then our code depends on those specific implementations. So we should write our program to depend on the super type, List, instead then our program can work for either of the List implementations.

In short this is one rule of OOP design: Program to an interface.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • If i cant access method of derived class by obj1 then how it will be preferable.Instead of this i will use obj2 & access both class method(base & derived).May be it is silly question but i am not clear – nikhil jain Jun 21 '15 at 10:42
  • Idea is to use the interface as reference variable. IN that case you are free to provide different implementations for the same interface and that wont create any compilation issues. Check: http://stackoverflow.com/questions/1992384/program-to-an-interface-what-does-it-mean – akhil_mittal Jun 21 '15 at 10:46
0

The different between the following two lines -

Base obj1 = new Derived();
Derived obj2 = new Derived();

Is that -

  1. for line 1 - you are creating a object of type - Derived and storing it in type Base variable , this is legal , since Derived class is a sub-class of Base , but when you do that, using obj1 , you cannot call functions defined in Derived class, example -

    obj1.print()

This would throw an error , because the type of object obj1 is Base (class) and there is no print function in that. To be able to call print function for obj1 you would need to cast obj1 to Derived class and call the function , like -

((Derived)obj1).print()

In this case, you are casting the obj1 to type Derived, and hence compiler now knows that obj1 can call the print() function.

  1. In line 2 , you are directly creating an object of type Derived and storing it in Derived type variable.

This is just one example of how they are different, there can be other examples as well .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176