-1

ClassName a = new ClassName1();

What does ClassName and ClassName1 mean? I thought that ClassName had to equal ClassName1.

Anna Lee
  • 1
  • 1
  • 2
    Hard to tell without more context, but it seems that `ClassName1` is subtype of `ClassName` – rkosegi Apr 05 '20 at 19:49
  • It's not really clear what you're trying to do, but I highly doubt "class1" needs to be a subtype of "class". Rather you should be writing `ClassName class1 = new ClassName("class1")`, or similar – OneCricketeer Apr 06 '20 at 06:45

2 Answers2

2

If

public class ClassName1 extends/implements ClassName

than it is perfectly valid.

This is how polimorphism works - you can treat given instance as it would be a solely instance of any of its superclasses or interfaces it implements.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

ClassName is the type of the variable a, which means for your problem, that a can represents an object of the type ClassName (and any other subclass of it).

So, the sentence ClassName a = new ClassName1() only make sense and is right if the ClassName1 is a subclass of ClassName.

Vinicius Almada
  • 386
  • 3
  • 13