I have a class header:
public class Person : Human
What does the :
mean?
Is it something like the extends in Java? And what do I need it for?
I have a class header:
public class Person : Human
What does the :
mean?
Is it something like the extends in Java? And what do I need it for?
The ":" means "extends" if you're comparing it to java. Every class extends object by default. You need it to extend a class, I'm assuming you already know what extending is, if not feel free to ask.
In this case, the colon operator is separating the class name "Person" from the base class "Human". The colon here implies inheritance, so you are right! Person inherits the fields/methods of Human. You can also add other methods to Person to provide it with additional functionality.
Here is a similar post with other functions available with the colon operator: In C# what category does the colon " : " fall into, and what does it really mean?
The colon operator (:
) is used to extend a class, like in c++. This means that a Person
is a specific kind of Human
. It can participate in any context that relates to a Human
(such as being passed as a method argument), but may have specific behaviors (i.e., it may override some of Human
's method, or add new ones).
The ':' operator is used for inheriting from a super class(Synonymous to the 'extends' keyword in Java).
Why is inheritance needed - It's a really powerful concept for modelling the relations in the world around us. Technically, the subclasses tend to be more specific and the superclasses are more generalized.
For example - Consider Student : Person
. Here the Student is the subclass and the Person is the super class.
class Person
{
public string name;
}
class Student : Person
{
public string id;
}
This translates to the fact that Every Student is also a Person and a student will inherently have 'name' as an attribute. But every Person needn't be a student and won't have 'id' implicitly.