2

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?

Omar
  • 16,329
  • 10
  • 48
  • 66
atjahfjvda
  • 99
  • 2
  • 11
  • While it has been already answered I believe that it's a so basic question and you shouldn't come to StackOverflow to ask something you can check on MSDN... – Matías Fidemraizer May 14 '16 at 13:08

4 Answers4

6

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.

mrousavy
  • 311
  • 1
  • 8
1

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?

Community
  • 1
  • 1
Kylecrocodyle
  • 489
  • 1
  • 3
  • 9
0

The colon operator (:) is used to extend a class, like in . 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).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

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.

Gautham
  • 766
  • 7
  • 15