Class, Superclass and Subclass

5

Regarding inheritance by code reuse in C # I have the following doubt:

What is the difference between classes , superclasses and subclasses ? Does this change anything when doing code reuse?

Additional Information:

Terminology Meaning: "Object Oriented"

p>

What are the pillars of object-oriented programming?

    
asked by anonymous 01.09.2017 / 15:43

2 answers

9
Inheritance is a feature of object-oriented programming languages that allows the definition of a base class that in turn provides specific functionality (data and behavior), and the definition of derived classes that inherit or override this functionality . The superclass is the one that is inherited by a subclass.

C # and .NET only support unique 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 of base class A. Since inheritance is transitive, type A members are available in 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 constructors, which you call to create a new instance of the class. Each class should define its own constructors.

  • Finalizers, which are called by the run-time garbage collector to destroy the instances of a class.

While all other members of a base class are inherited by derived classes, the fact that they are visible or not depends on their accessibility. The accessibility of a member affects its visibility to derived classes as follows:

  • Private Members are only visible in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, AB is a nested class derived from A, and C is derived from A. The private A.value field is visible in AB However, if you remove the comments from the C.GetValue method and try to compile the example, it will produce a compiler error CS0122: "'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 derived classes.

  • Internal Members are visible only in derived classes located in 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 public interface of the derived class. Inherited public members can be called as if they have been defined in the derived class.

SOURCE: Microsoft DOCs Inheritance Tutorial

    
01.09.2017 / 15:59
7

Class is the general term, it is a model of the data structure, specifically in C # is always a type by reference. The inheritance is just creating a class that is based on another already existing class reusing the already written code.

Superclass is the class that will be derived, is the parent class or base as it is also called.

Subclass is the derived class, it is the child class that was inherited from a superclass.

public class SuperClasse {}
public class Subclasse : SuperClasse {}

The example helps to understand but is not of the best because this subclass can also be a superclass so it could do:

public class NovaSubClasse : SubClasse {} //aqui SubClasse não deixa de ser uma superclasse.
    
01.09.2017 / 15:57