L02. C# Fundamentals
L02. C# Fundamentals
L02. C# Fundamentals
• Abstraction
– Abstract or identify the objects involved in the problem
• Encapsulation
– Packaging data and behaviour into a single unit - the class
• Inheritance
– Reuse of code through extending program units
• Polymorphism
– Multiple implementations of the same behaviour
Classes in C#
using System;
• Declaration
• Constructors
using System;
• Members can be
– static (common) or specific for a given object
Class Definition and Members
class class_name {
... methods...
... constructors...
... properties...
Constructors do not have return values and always have the same name
as the class.
Or
ClassName objectName;
14
using System;
class OutputClass
{
private string myString;
// Constructor
public OutputClass(string inputString)
{
myString = inputString;
}
Continued …
// Program start class
class ExampleClass
{
// Main begins program execution.
public static void Main()
{
// Instance of OutputClass
OutputClass outCl = new OutputClass("This is printed
by the output class.");
new class_name(parameters)
• Properties are not bound to a class field – they can be used calculate
a value dynamically
public class Rectangle
{
private double width;
private double height;
1. Create an instance
– Initialize its fields
It should be able to view and change the name and the breed of the
dog → get and set methods
• Task is as follows:
– Create 3 dogs
– Put all dogs in an array
– Iterate through the array elements and ask each dog to bark
– Note:
• Use the Dog class from the previous example
31
public class Doggies
static void Main()
{
// Use the Dog constructor to set name and breed
A derived (or child) class inherits from a base (or parent) class
The derived class inherits all the attributes and behaviour of the
base class.
The derived class may implement also its own attributes and
behavior.
Any derived class inherits all the data and methods of its base
class
- Including private data and methods
- Cannot use or modify private data and methods directly
using System; public class ChildClass : ParentClass
{
public class ParentClass public ChildClass()
{ {
public ParentClass() Console.WriteLine("Child Constructor.");
{ }
Console.WriteLine("Parent
Constructor."); public static void Main()
} {
ChildClass child = new ChildClass();
public void print()
{ child.print();
Console.WriteLine("I'm a Parent Class."); }
} }
}
• Can define your own classes from which other classes can
inherit
Then a derived Dog class is defined - the Dog class inherits from the Animal
class.
Animal myAnimal = new Animal();
myAnimal.Greet();
Even though we have not defined a Greet() method for the Dog class, it still
knows how to greet us, because it inherits this method from the Animal class.
May also define a Greet() method in the Dog class … that overrides the one in
the parent class.
public class Animal
{
public virtual void Greet()
{
Console.WriteLine("Hello, I'm some sort of animal!");
}
}
public class Dog : Animal
{
public override void Greet()
{
Console.WriteLine("Hello, I'm a dog!");
}
}
Besides the added method on the Dog class, you should notice two things:
I have added the virtual keyword to the method in the Animal class, and on the
method in the Dog class, I use the override keyword.
In C#, you are not allowed to override a member of a class unless it is marked as
virtual.
If you want to, you can still access the inherited method, even when you override
it, using the base keyword.
Inheritance is not only from one class to another - you can have a whole hierarchy
of classes, which inherits from each other.
- For instance, we could create a Puppy class, which inherits from our Dog
class, which in turn inherits from the Animal class.
What you cannot do in C#, is to let one class inherit from several other classes at
the same time.
- Multiple inheritance, as it is called, is not supported by C#.
Accessing Base Class Methods from a Derived Class
• Example of polymorphism
– ToString( ) method can have many different definitions
– ToString( ) uses the virtual modifier, implying that any class
can override it
• Derived classes inherit from a base class
– Also called subclasses or child classes
• Protected access modifiers
– Access only to classes that derived from them
– Access to change data in the base class