Contest Doubt on Inheritance and Polymorphism in Object Orientation

18

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 issues was as follows:

  

About the following statements related to programming concepts   oriented, it is correct to say that to 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   instantiated object of the same class.
c) declare in   a class an abstract method, all classes that inherit from this   class to instantiate objects are required to implement this   d) define, in a base class of a hierarchy of   classes, a method with the final modifier, in a child class, this   same method can only be overwritten with public visibility.   e) defining, in a base class of a class hierarchy, a method   with the protected visibility, in a daughter class, this same method   it can only be overridden as protected or private.

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

Is not the letter E correct too?

Is there anything wrong with this issue that can be appealed to?

    
asked by anonymous 18.12.2018 / 22:18

1 answer

20
  

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

Can not instantiate an abstract class

  

b) defining an attribute as static in a class, you can only 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 an abstract method in a class, all classes inheriting from that class to instantiate objects are required to implement this method.

Exactly, an abstract method has no implementation, so it can not 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 a method with the final modifier, in a child class, in a base class of a class hierarchy, this method can only be overwritten with public visibility.

final indicates that the method will not overlap, so the rest of the statement is incorrect.

  

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

It can be protected or public . I think the first one is obvious that it can. Just as a daughter class can have new members that it did not have in the mother it becomes clear that an existing method with restricted visibility can appear with a more liberal visibility, ie it is as if it were a new member with total visibility, public . This already demonstrates an error in the question. But the opposite indicates more error yet. A daughter class can not fail to have a member that existed in the parent class, to be a object equal to the mother it needs to have everything that already existed before. When you decrease the visibility you make with a member that was quite visible is not visible in all situations, so it is common if that member ceases to exist in certain scenarios, which would render the object incompatible with what is expected of it. This is formally defined by the Liskov principle .

But in addition to this the polymorphism only makes sense in members proteced and public , a private member by definition is an implementation details and belongs only to the class in which it was defined, can not pass to another, in this case even in parent class this would be a problem.

If it did not exist polymorphism could do this because they would be distinct methods and there Liskov does not apply. But the existence of the superscript term in the text makes it clear that there is polymorphism.

    
18.12.2018 / 22:55