0

I have an example.

class Candidate is superclass and ExperienceCandidate is subclass.

i have 2 type to create object is:

ExperienceCandidate temp = new ExperienceCandidate();

Candidate temp = new ExperienceCandidate();

Help me distinguish the differences between these two constructs and which one is used in which case

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Have you googled at all? Read through this Q&A - https://stackoverflow.com/questions/6924974/create-instance-from-superclass-instance as well as this article - https://www.oreilly.com/library/view/learning-java-4th/9781449372477/ch06s01.html – David Brossard Oct 25 '20 at 06:29
  • this looks like a homework question, but the term you are looking for is "Polymorphism" – Tom Elias Oct 25 '20 at 07:29

1 Answers1

0

Generally whenever you are creating an object , it is just to create an instance of the class so that you can use it somewhere. In the case of Superclasses and Subclasses , whenever you initiaze a subclass of a class , it already contains all the objects/methods of the superclass. In your case , there are two logical discrepancies :

  • The Candidate temp = new ExperienceCandidate(); is not the correct way to call an object , the correct way to call the object of the superclass is Candidate temp = new Candidate();
  • There is almost no use creating the object of candidate class to use in your superclass as you can just use the keyword super() to call the constuctor of the superclass and anyway all the function of the superclass are available for you to use in the subclass (since you extended to it)
Recondit
  • 114
  • 9