Inheritance & Polymorphism
Inheritance & Polymorphism
Inheritance & Polymorphism
class Animal {
public int Legs {get;set;}
public int Age {get;set;}
}
Inheritance of Methods
A derived class inherits all the members of the base class,
including its methods.
For example:
class Person {
public void Speak(){
Console.WriteLine(”Hi there”);
}
}
class Student : Person {
int number;
}
static void Main(string[] args){
Student s = new Student();
s.Speak();
In the example above a Student object was created and called the Speak method,
which was declared in the base class Person, was called.
Protected modifier
The protected modifier differs from private modifier, by the
means that it can be accessed in a derived class as well as in
the base class.
For example:
class Person{
protected int Name {get;set;}}
class Student : Person {
public Student(string Name){
this.Name = Name;}
public void Speak(){
Console.WriteLine(”Name: {0}” Name);}}
static void Main(string[] args){
Student s = new Student(”David”);
s.Speak();}
As it is seen, it is possible to modify the Name property of the base class
from the derived class.
Sealed modifier
A class can prevent other classes from inherting it, or any of
its members, by using the sealed modifier.
For example:
The code above will result in an error as the sealed class Animal can not be
inherited.
Polymorphism
The word polymorphism means “having many forms”.
For example:
class Shape {
public virtual void Draw(){
Console.WriteLine(”Base draw”);
}
}
The virtual keyword allows methods to be overridden in derived classes.
As we can see, the Draw() method has been defined in the base class, but has
been redefined in derived classes to serve the needs of that class. This is
possible, because the Draw() method has been declared as virtual.
Abstract
As described above, polymorphism is when there are different
derived classes with the same method, which has different
implementations in each class. This behavior is achieved through
virtual methods that are overridden in the derived classes.
These methods are thus defined using the abstract keyword, and
specify that the derived classes must define that method on
their own.
For example:
Abstract Classes
An abstract class is intended to be a base class of other
classes. It acts like a template for its derived classes.
For example:
Interfaces
An interface is a completely abstract class, which contains
only abstract members.
Interface Implementation
When a class implements an interface, it must also implement,
or define, all of its methods.
For example:
For example:
class Car {
string name;
public Car(string name){
this.name = name;
Motor m = new Motor();
}
public class Motor {
// Some code
}
}
Here the Motor class is nested in the Car class and can be used similar to
other members of the class.
Namespaces
When a blank project is created, it has the following
structure:
using System;
namespace ProjectName
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(”Hello World!”);
}
}
}
The whole program is inside a namespace.
Namespaces declare a scope that contains a set of related
objects. It is possible to use a namespace to organize code
elements.
System.Console.WriteLine(”Hello”);