6
Regarding inheritance by code reuse in C# I have the following doubt:
What’s the difference between classes, superclasses and subclasses? This changes something when doing code reuse?
6
Regarding inheritance by code reuse in C# I have the following doubt:
What’s the difference between classes, superclasses and subclasses? This changes something when doing code reuse?
10
Inheritance is a feature of object-oriented programming languages that allows the definition of a base class that in turn provides a specific functionality (data and behavior), and the definition of derived classes that inherit or replace that functionality. To superclass is that which is inherited by a sub-class.
C# and . NET support only single inheritance. That is, a class can inherit only from a single class. However, the inheritance is transitive, which allows you to define an inheritance hierarchy for a set of types. In other words, type D can inherit from type C, which inherits from type B, which inherits from type A. As inheritance is transitive, type A members are available to type D.
Not all members of a base class are inherited by derived classes. The following members are not inherited:
Static constructors, which initialize the static data of a class.
Instance constructs, which you call to create a new instance of the class. Each class must define its own constructors.
Finishers, which are called by the runtime garbage collector to destroy instances of a class.
While all other members of a base class are inherited by derived classes, whether or not they are visible depends on their accessibility. A member’s accessibility affects its visibility to derived classes as follows:
Private members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, A.B is a nested class derived from A, and C derives from A. The private A.value field is visible in A.B. However, if you remove comments from the C.Getvalue method and try to compile the example, it will produce a CS0122 compiler error: "'A.value' is inaccessible due to its level of protection".
using System;
public class A
{
private int value = 10;
public class B : A
{
public int GetValue()
{
return this.value;
}
}
}
public class Examplo
{
public static void Main(string[] args)
{
var b = new A.B();
Console.WriteLine(b.GetValue());
}
}
// O exemplo retorna a saída :10
Protected members are visible only in derivative classes.
Internal members are visible only in derived classes located on the same Assembly as the base class. They are not visible in derived classes located in an Assembly other than the base class.
Public members are visible in derived classes and are part of the derived class’s public interface. Public inherited members may be called as if they had been defined in the derived class.
9
Class is the general term, is a data structure model, specifically in C# is always a type by reference. Inheritance is just creating a class that is based on another existing class reusing already written code.
Superclass is that class which will be derived, is the mother or base class as it is also called.
Subclass is the derived class, is the daughter class that was inherited from a superclass.
public class SuperClasse {}
public class Subclasse : SuperClasse {}
The example helps to understand but is not the best because this subclass can also be a superclass so could do:
public class NovaSubClasse : SubClasse {} //aqui SubClasse não deixa de ser uma superclasse.
Browser other questions tagged c# oop inheritance subclass superclass
You are not signed in. Login or sign up in order to post.
When it would be ideal to use them?
– Renato Junior
@Renatosilva I think you’re confusing what this is. It is an abstract concept, it is not a concrete tool, just make inheritance and will be using super and subclass. When inheritance is a little more complicated, you can write a book about it. But you can say don’t use it until it’s absolutely necessary. You can start here: https://answall.com/search?tab=votes&q=heran%C3%a7a If nothing answers ask a specific question about this.
– Maniero