0

I just want a short clarification on these two lines of code. What's the difference between these two?

SuperClass c = new JustTesting(1, 'b');
JustTesting b = new JustTesting(2, 'c'); 

By the way, in case you'd want to see the full code, here it is:

import static java.lang.System.out;

public class JustTesting extends SuperClass{

public JustTesting(int firstVar, char secondVar) {
    super(firstVar, secondVar);
}

public static void main(String[] args) {
    SuperClass c = new JustTesting(1, 'b');
    JustTesting b = new JustTesting(2, 'c'); 
    out.println(c.firstVar);
    out.println(b.firstVar);
}
}
Jude Maranga
  • 865
  • 2
  • 9
  • 27
  • The only difference is the type of the variable. Both will still reference an *object* of the same type, but what methods you can call on them will differ based on the type of the *variable*. If you want to access methods on `c` which only exist in the `JustTesting` class, you'll need to cast it. – azurefrog May 21 '15 at 16:37
  • @Sir Sotirios, I'm sorry I still didn't get the difference. I already read the answer of the duplicate question. I am sorry. And if it doesn't bother you, would you mind if I'd ask for a further clarification. I am sorry I'm still a beginner of Java really. Sorry if I had annoyed you – Jude Maranga May 21 '15 at 16:47
  • Please use `@user-name` to address someone. You can ask for any clarification you want. You can edit your question and others will review it and potentially vote to reopen it. Please read the Help Center for more details. – Sotirios Delimanolis May 21 '15 at 17:17
  • As for the question itself, this is one of the strengths of polymorphism. You can reference an object with any type above it in its inheritance hierarchy. This restricts the API you want to expose to consumers. – Sotirios Delimanolis May 21 '15 at 17:18

0 Answers0