Chapter 2 - Object-Oriented Fundamentals in C#
Chapter 2 - Object-Oriented Fundamentals in C#
Chapter 2
Object-Oriented Fundamentals in C#
Why inheritance
Advantages of inheritance
Syntax of inheritance
Inheritance concepts.
In this example
1.DerivedClass inherits from Parent Class
2. C# supports single inheritance
3. C# supports multiple interface inheritance
4. Child class is specialization of Base class
5. Base class are automatically instantiated before
derived classes
6.Parent class Constructor executes before child class
constructor Event driving Programming by(C#) byDagne w . 7
Method Hiding in C#
Redefine the method of base class in the derived class using
the new keyword(re-implement the method of base class
using new keyword)
Method hiding and Invoking hidden base class members
Inheritance concepts
Use the new keyword to hide a base class member.
You will get a compiler warning, if you miss the new
keyword. Different ways to invoke a hidden base class
member from derived class.
1. Use base keyword
2. Cast child type to parent type and invoke the hidden
member
3. ParentClass pc =new ParentClass() pc.HiddenMethod()
How ever there are several differences classes and structs, which we will explore on
this session. A struct is a value type where as a class is a reference type.
All the differences that are applicable to value types and reference types are also
applicable to classes and structs.
Structs are stored on stack, where as classes stored on the heap.
Value types hold their value in memory where they are declared, but reference
types hold a reference to an object in memory
Value types destroyed immediately after the scope is lost, where as for reference
types only the reference variable is destroyed after the scope is lost.
2) Structs can’t have explicit parameter less constructor where as class can.
3) Struct can’t inherit from another class where as a class can, both structs and
classes can inherit from interface.
5) Struct are sealed types. Note 2:To prevent a class from being inherited use
the sealed modifier?
}
}
}
Thank you!!!