Inheritance class Object

Asked

Viewed 141 times

6

How the compiler implicitly makes all classes inherit from object?

This is a behavior adopted in languages such as C#, Java and others.

public class Funcionario 
{

}

public class Funcionario : Object
{

}

The above example is redundant. But how does the compiler know that all created classes should take this pattern? Is it possible for the programmer to force the compiler to implement such behavior? Perhaps even forcing the inheritance of other classes?

2 answers

7

There is no way the programmer can avoid the inheritance of Object in the language, and as far as I know, not even at a lower level.

It is no secret to do so. Every class that has no other inheritance, and the compiler places there the inheritance of Object. He makes the syntactic and semantic analysis what is written and decides what to do. The compiler already knows that a programmer has programmed it like this.

When there is the inheritance of another class, he does not need to do this because, since all classes inherit from Object, it is certain that the inheritance of another class will make this inherit from Object by hierarchy.

  • Sorry, buddy, I didn’t see your answer before I put mine ^^.

  • @bigwon, thank you for the answer. I believe that the syntactic and semantic analysis are the key point to my question. Is it possible to simulate such behavior via C# ? For example, I create a framework and wish all classes to inherit from object2...

  • @Pedro has no way of forcing under normal conditions. Of course he can change the compiler of the C# or make some extra tool to "guarantee it" provided you use the tool or its compiler version. But it is not recommended.

  • @Pedro in compiled languages is not possible without the compiler itself supporting this operation, which is not the case with C# or Java. But you can make some very interesting ganbiarras in some interpreted languages (e.g. Ruby)

5


But how does the compiler know that all created classes should take this pattern?

It’s the pattern that was created. During the development of languages, it was defined that, at the top hierarchical inheritance, there should be a common super class.

Is it possible for the programmer to force the compiler to implement such behavior? Perhaps even forcing the inheritance of other classes?

No (as far as I know). It would be illogical to the contrary, as the compiler would not know how to treat the artifact. You can force any inheritance you want, but you’ll always have as your first inheritance, the Object.

Browser other questions tagged

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