Here, we are inheriting the derived class Dog from the base class Animal. The Dog class can now access the fields and methods of Animal class. C Inheritance. In the above example, we have derived a subclass Dog from the superclass Animal.
Notice the statements,. Here, we are using labrador object of Dog to access the name and display of the Animal class. This is possible because the derived class inherits all fields and methods of the base class. In C , inheritance is an is-a relationship. We use inheritance only if there is an is-a relationship between two classes. For example,. We can derive Dog from Animal class. Similarly, Apple from Fruit class and Car from Vehicle class.
When we declare a field or method as protected , it can only be accessed from the same class and its derived classes. In the above example, we have created a class named Animal. The class includes a protected method eat. Since the protected method can be accessed from derived classes, we are able to access the eat method from the Dog class.
In multilevel inheritance, a derived class inherits from a base and then the same derived class acts as a base class for another class. In multiple inheritance, a single derived class inherits from multiple base classes. C doesn't support multiple inheritance.
However, we can achieve multiple inheritance through interfaces. Hybrid inheritance is a combination of two or more types of inheritance. The combination of multilevel and hierarchical inheritance is an example of Hybrid inheritance.
If the same method is present in both the base class and the derived class, the method in the derived class overrides the method in the base class. This is called method overriding in C. In the above example, the eat method is present in both the base class and derived class. This tutorial uses Visual Studio Code , although you can use any code editor of your choice. To create and run the examples in this tutorial, you use the dotnet utility from the command line.
Follow these steps for each example:. Enter the dotnet new console command at a command prompt to create a new. NET Core project. Enter the dotnet restore command from the command line to load or restore the project's dependencies.
You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new , dotnet build , dotnet run , dotnet test , dotnet publish , and dotnet pack. To disable implicit restore, use the --no-restore option. The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.
For information about how to manage NuGet feeds, see the dotnet restore documentation. Enter the dotnet run command to compile and execute the example. Inheritance is one of the fundamental attributes of object-oriented programming.
It allows you to define a child class that reuses inherits , extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class.
C and. NET support single inheritance only. That is, a class can only inherit from a single class. However, 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 the base class type A. Because inheritance is transitive, the members of type A 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 constructors , which you call to create a new instance of the class. Each class must define its own constructors. Finalizers , which are called by the runtime's garbage collector to destroy instances of a class. While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility.
A member's accessibility affects its visibility for 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 that derives from A , and C derives from A. The private A.
However, if you remove the comments from the C. GetValue method and attempt to compile the example, it produces compiler error CS "'A. Protected members are visible only in derived classes. Internal members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class.
Public members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class. In the following example, class A defines a method named Method1 , and class B inherits from class A. The example then calls Method1 as if it were an instance method on B. Derived classes can also override inherited members by providing an alternate implementation.
In order to be able to override a member, the member in the base class must be marked with the virtual keyword. By default, base class members are not marked as virtual and cannot be overridden. In some cases, a derived class must override the base class implementation. Base class members marked with the abstract keyword require that derived classes override them.
Inheritance applies only to classes and interfaces. Other type categories structs, delegates, and enums do not support inheritance. Because of these rules, attempting to compile code like the following example produces compiler error CS "Type 'ValueType' in interface list is not an interface. Besides any types that they may inherit from through single inheritance, all types in the. NET type system implicitly inherit from Object or a type derived from it.
The common functionality of Object is available to any type. To see what implicit inheritance means, let's define a new class, SimpleClass , that is simply an empty class definition:. You can then use reflection which lets you inspect a type's metadata to get information about that type to get a list of the members that belong to the SimpleClass type. Although you haven't defined any members in your SimpleClass class, output from the example indicates that it actually has nine members.
One of these members is a parameterless or default constructor that is automatically supplied for the SimpleClass type by the C compiler. The remaining eight are members of Object , the type from which all classes and interfaces in the. NET type system ultimately implicitly inherit. Implicit inheritance from the Object class makes these methods available to the SimpleClass class:. The public ToString method, which converts a SimpleClass object to its string representation, returns the fully qualified type name.
In this case, the ToString method returns the string "SimpleClass". Three methods that test for equality of two objects: the public instance Equals Object method, the public static Equals Object, Object method, and the public static ReferenceEquals Object, Object method. By default, these methods test for reference equality; that is, to be equal, two object variables must refer to the same object.
The public GetHashCode method, which computes a value that allows an instance of the type to be used in hashed collections. The protected Finalize method, which is designed to release unmanaged resources before an object's memory is reclaimed by the garbage collector. The protected MemberwiseClone method, which creates a shallow clone of the current object.
Because of implicit inheritance, you can call any inherited member from a SimpleClass object just as if it was actually a member defined in the SimpleClass class. For instance, the following example calls the SimpleClass.
ToString method, which SimpleClass inherits from Object. The following table lists the categories of types that you can create in C and the types from which they implicitly inherit. Each base type makes a different set of members available through inheritance to implicitly derived types. Control Statements. OOP Concepts. Table of Contents. Improve Article. Save Article.
Like Article. WriteLine "GeeksforGeeks" ;. Previous C Constructors. Next C Encapsulation. Recommended Articles.
0コメント