Doubt of competition on inheritance and polymorphism in object orientation

Asked

Viewed 191 times

17

I took the test of the IFNMG competition for the position of computer teacher. The bank that prepared the test was the CEFET Foundation. One of the questions was this::

About the statements below, related to programming concepts object-oriented, it is correct to state that the

a) instantiate an object of an abstract class, it is only possible to access its attributes defined as public.
b) define an attribute as Static, in a class, it is only possible to have access to it after there is, by minus, an instantiated object of that same class.
c) declare in a class a method Abstract, all classes that inherit from that class to instantiate objects are required to implement this method.
d) define, in a base class of a hierarchy of classes, a method with the final modifier, in a daughter class, that same method can only be overwritten with public visibility.
e) define, in a base class class of a class hierarchy, a method with visibility protected, in a child class, that same method can only be overwritten as protected or private.

I ended up marking the words E, because I was in doubt between C and E. The feedback says that the correct is the letter C.

The letter E is not correct either?

Is there anything wrong with this issue that could be appealed?

  • 4

    E is wrong anyway. Due to Liskov’s replacement, you could not reduce visibility, just increase. I will try to improve this comment as an answer or look for duplicates (I think I’ve seen this question here)

  • 3

    https://stackoverflow.com/a/23081702/2241463

  • My mistake, there’s no duplicate. It was another "mark the right" contest, but I was able to find it with OO keywords

1 answer

21


a) instantiating an object of an abstract class, it is only possible to access its attributes defined as public.

It is not possible to instantiate an abstract class.

b) defining an attribute as Static, in a class, is only possible to access it after there is at least one instantiated object of that class.

A static "attribute" belongs to the class and not to the instance, so it can be accessed at any time, even without instances of this class.

c) declare in a class an Abstract method, all classes that inherit from that class to instantiate objects are required to implement that method.

Exactly, an abstract method has no implementation, so it cannot be instantiated. Classes that inherit from it need to implement it, otherwise the inherited class needs to be abstract as well, which will prevent it from being instantiated.

d) define, in a base class of a class hierarchy, a method with the final modifier, in a daughter class, that same method can only be overwritten with public visibility.

final indicates that there will be no overlap of the method so all the rest of the statement is incorrect.

e) define, in a base class of a class hierarchy, a method with protected visibility, in a child class, that same method can only be overwritten as protected or private.

May protected or public. I think the first one is obvious that it can. Just as a daughter class may have new members that it did not have in the mother it is clear that an existing method with a restricted visibility may appear with a more liberal visibility, ie it is as if it were a new member with full visibility, the public. This already shows an error in the question. But the opposite indicates more error still. A daughter class cannot fail to have a member who existed in the mother class, to be a He needs to have everything that ever existed before. When you decrease the visibility you do with a member who was quite visible is not visible in all situations, so it is common if that member ceased to exist in certain scenarios, which would make the object incompatible with what is expected of it. This is formally defined by liskov principle.

But beyond that, polymorphism only makes sense in members protected and public, a private member by definition is an implementation detail and only belongs to the class in which it was defined, it cannot pass to another, in this case even in the mother class this would be a problem.

If there were no polymorphism it could do this because they would be different methods and then Liskov does not apply. But the existence of apalavra superscript in the text makes it clear that there is polymorphism.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.