Can a subclass of another subset inherit a method from a base class in C #

Say class A is a base class and then you have class B subclass A (class B: A). Then you have class C: B.

The question is, do you have a method in class A, can class C use this method?

+3


source to share


4 answers


Yes, you can, if declared public

or protected

. but not private

.

Thanks to @DavidL .. I forgot to mention a special case internal

.

From the C # manual that comes with Visual Studio (2012 edition here) ::

  • • Inheritance is transitive. If C is derived from B and B is derived from A, then C inherits the members declared in B as well as the members declared in A. • A derived class extends its direct base class. A derived class can add new members to those it inherits, but it cannot remove the definition of an inherited member. • Example constructors, destructors, and static constructors are not inherited, but all other members, regardless of their declared accessibility (§ 3.5). However, depending on their declared availability, inherited members may not be available in the derived class. • A derived class can hide (§3.7.1.2) inherited members by declaring new members with the same name or signature. Please note, however,that hiding an inherited member doesn't remove that member - it just makes that member inaccessible directly through the derived class. • An instance of a class contains the collection of all instance fields declared in a class, and its base classes and implicit conversion (§6.1.6) exist from a derived class type to any of its base class types. Thus, a reference to an instance of some derived class can be thought of as a reference to an instance of any of its base classes. • A class can declare virtual methods, properties, and indexes, and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.• An instance of a class contains the collection of all instance fields declared in a class, and its base classes and implicit conversion (§6.1.6) exist from a derived class type to any of its base class types. Thus, a reference to an instance of some derived class can be thought of as a reference to an instance of any of its base classes. • A class can declare virtual methods, properties, and indexes, and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.• An instance of a class contains the collection of all instance fields declared in a class, and its base classes and implicit conversion (§6.1.6) exist from a derived class type to any of its base class types. Thus, a reference to an instance of some derived class can be thought of as a reference to an instance of any of its base classes. • A class can declare virtual methods, properties, and indexes, and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.6) exists from a derived class type for any of its base class types. Thus, a reference to an instance of some derived class can be thought of as a reference to an instance of any of its base classes. • A class can declare virtual methods, properties, and indexes, and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.6) exists from a derived class type for any of its base class types. Thus, a reference to an instance of some derived class can be thought of as a reference to an instance of any of its base classes. • A class can declare virtual methods, properties, and indexes, and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.and derived classes can override the implementation of these functions. This allows you to exhibit polymorphic behavior in which the actions performed by calling a member function change depending on the type of runtime instance through which that member of the function is called.


Also this is the new hide feature, if I'm right, in C # 5.0:

The nested type can hide (§3.7.1) the underlying element. The new modifier is allowed for nested type declarations, so hiding can be explicitly expressed. Example

using System;

class Base
{
    public static void M() {
        Console.WriteLine("Base.M");
    }
}
class Derived: Base 
{
    new public class M 
    {
        public static void F() {
            Console.WriteLine("Derived.M.F");
        }
    }
}
class Test 
{
    static void Main() {
        Derived.M.F();
    }
}

      

shows a nested class M that hides the method M defined in Base.

+2


source


It depends.



  • When a method of class A is private: no
  • When class A method is protected: yes
  • When a method of class A is public: yes
  • When a method of class A is public or protected and virtual, class C can override that method.
+1


source


Yes, when you inherit from a class, you get all of its methods, including everything you inherited from others.

0


source


public class A
{
    public int Id { get; set; }
    protected int protectedId { get; set; }
    private int privateId;
}

public class B : A
{
}

public class C : B
{
    public C()
    {
        int temp = Id; // works
        int temp1 = protectedId; // works
        int temp2 = privateId; // does NOT work
    }
}

      

and in some other class;

public void SomeMethod()
{
    C c = new C();
    int i = c.Id; // works
    int j = c.protectedId; // does NOT work
    int k = c.privateId; // does NOT work
}

      

0


source







All Articles