

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Object Creation of Inherited Class
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.
The derived class inherits the base class member variables and member methods. Therefore, the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.
Here you can see object is created for the inherited class.
Example
using System; namespace Demo { class Rectangle { protected double length; protected double width; public Rectangle(double l, double w) { length = l; width = w; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class Tabletop : Rectangle { private double cost; public Tabletop(double l, double w) : base(l, w) { } public double GetCost() { double cost; cost = GetArea() * 70; return cost; } public void Display() { base.Display(); Console.WriteLine("Cost: {0}", GetCost()); } } class ExecuteRectangle { static void Main(string[] args) { Tabletop t = new Tabletop(3, 8); t.Display(); Console.ReadLine(); } } }
Output
Length: 3 Width: 8 Area: 24 Cost: 1680
- Related Questions & Answers
- Java Object Creation of Inherited Class
- What all is inherited from parent class in C++?
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Can inherited properties of objects be generalized?
- Method of Object class in Java
- PHP Basics of Class and Object
- Creation of virtual environments using Python
- Is constructor inherited in Java?
- Object class in Java
- Matrix creation of n*n in Python
- Get super class of an object in Java
- PHP Inherit the properties of non-class object?
- Can constructors be inherited in Java?
- Are static methods inherited in Java?
- Is final method inherited in Java?
Advertisements