I have a couple problems here that I have answered. I am not 100% sure of my answer(s). Would you be so kind as to check them and provide suggestions if they are not correct. These 2 questions are regarding what methods super/sub class(es) can invoke (question1) and which objects invoke which methods (question2). Thanks in advance.

Question 1
==================================================
Super Class: Expression (Includes prefix, postfix, and infix expressions)
Public Method: characterAt
Protected Method: isOperator
Protected Method: isIdentifier
Private Method: somePrivateMethoda
Private Method: somePrivateMethodb

SubClass: InfixExpression (Derived from Expression. Represents infix expression)
Public Method: isLegal
Public Method: evaluate
Protected Method: someProtectedMethoda
Protected Method: someProtectedMethodb
Private Method: anotherPrivateMethoda
Private method: anotherPrivateMethodb
==================================================
What methods can the implementation of isIdentifier invoke?
CharacterAt
isOperator
somePrivateMethoda
somePrivateMethodb

What methods can the implementation of isLegal invoke?
characterAt
isOperator
isIdentifier
evaluate
someProtectedMethoda
someProtectedMethodb
anotherPrivateMethoda
anotherPrivateMethodb


Question 2
Assuming the classes described in the previous question and consider a main method that contains:

Expression algExp;
InfixExpression infixExp;

Soooo...
==============================================
Super Class: Expression (Includes prefix, postfix, and infix expressions)
Main Method: algExp
Public Method: characterAt
Protected Method: isOperator
Protected Method: isIdentifier
Private Method: somePrivateMethoda
Private Method: somePrivateMethodb

SubClass: InfixExpression (Derived from Expression. Represents infix expression)
Main Method: infixExp
Public Method: isLegal
Public Method: evaluate
Protected Method: someProtectedMethoda
Protected Method: someProtectedMethodb
Private Method: anotherPrivateMethoda
Private method: anotherPrivateMethodb
==============================================
Which of these objects can correctly invoke the method CharacterAt?
algExp and infixExp

Which of these objects can correctly invoke the method isOperator?
algExp and infixExp

Which of these objects can correctly invoke the method isLegal?
infixExp

Recommended Answers

All 2 Replies

Hi everyone,

Ask a more specific question.

Richard West

Basic info on methods.
(1) Static methods cannot access non static fields/methods in its own class.
Because an instance might not exist.
(2) Methods in a class can access all other methods in the same class.
Except for those that apply to (1).
(3) Protected methods are methods that appear private to other classes except for
those classes that extend it or are derived from it.
So unless i'm missing something that will jump right up and hit me in the face. Those rules should answer your questions.

p.s. On question 1 you are remembering the tricky protected concept but i think you are missing the basic ones...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.