• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79240
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic