• Post Reply Bookmark Topic Watch Topic
  • New Topic

Correct me if I'm wrong but can't you call a child method from main  RSS feed

 
Patrick Morris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong. I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instinate an object like Assessment a = new test(); and call a method in test.
I know the method can be called if I instinate the test t = new test() but that defeats the purpose of inheritance which I'm learning in class.
 
Patrick Morris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
none of these methods invocation will compile. Assessment is my parent class, Tests and Quizzes are my child classes

Assessment assessment = new Assessment();


case 2: assessment = new Quizzes();
assessment.displayAssessmentType();
break;
case 3: assessment = new Tests();
assessment.displayAssessmentType();
assessment = new Tests();
assessment.addTestQuestionAnswers();
assessment.displayAssessmentType();
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a variable of type Assessment, you can only call methods of the Assessment class (and its superclasses). You can't call methods which are declared only in subclasses of Assessment because there is no way to guarantee that the variable will always refer to an object of that subclass.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To elaborate, if you really want to do it, then you will need to cast the instance to a Test object before you can call the test's method. And if you really want to do it, but don't want a runtime error, then you should check it (with the instanceof operator) before casting the instance.

Henry
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Despite what lots of people say, don't call a subclass child. Call it subclass.
You cannot tell from a superclass how many subclasses it has (unless the superclass is marked final, in which case it has no subclasses). I get suspicious about design when I see people adding methods in subclasses. If you have an addQuestions method, why is that method in the subclass and not in the superclass? If you write the addQuestions method in the superclass, then there will be no need to do any casting.

Oh, they say, we wanted the method to be different in the different subtypes. In that case override the method in the different subtypes. Maybe make the Assessment version abstract (then Assessment would have to be an abstract class, too). Then you declare all your types as Assessment and let polymorphism do the hard work.

By the way, call your classes things singular: Test and Quiz rather than Tests and Quizzes.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember...there are two things created when you have a line like:

test t = new test()

there is the REFERENCE (in the above line, the variable "t"). It has a defined type - in this case, it refers to a "test" kind of thing.

Then there is the OBJCET - the thing created by the "new test()". It also has a type.

The reference type and the object type must be related, but the are not the same thing.

The reference only knows about what its type can do. so when you have:

Assessment a = new test();

Your reference "a" only knows about Assessment stuff. This is actually a good thing. The reference can be reset to point to something else. If you tried to call a method only available in the "test" subclass, but the reference had been reset to an Exam object, that may not have the method. Then what would it do? Crash and burn in a fiery death. To prevent hat from happening, java prevents you from calling a method the way you are asking.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!