You are on page 1of 17

Inheritance

Q3M1

CEP - CCIT
Fakultas Teknik Universitas Indonesia

Dudy Fathan Ali, S.Kom (DFA)


2016

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
o Inheritance?
• Property by which the objects of a derived class
possess copies of the data members and the
member functions of the base class.
o A class that inherits or derives attributes from another
class is called the derived class.
o The class from which attributes are derived is called the
base class.
o In OOP, the base class is actually a superclass and the
derived class is a subclass.

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
There are various relationships between objects of different classes
in an object-oriented environment:
o Inheritance relationship
o Each class is allowed to inherit from one class and each class can be
inherited by unlimited number of classes.
o Composition relationship
o OOP allows you to form an object, which includes another object as its
part. (e.g. Car and Engine)
o Utilization relationship
o OOP allows a class to make use of another class. (e.g. Car and Driver,
Student and Pencil)
o Instantiation relationship
o Relationship between a class and an instance of that class.

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
The following figure is an example of inheritance in OOP :

Pegawai
IDPegawai
Nama
Alamat

Pegawai Paruh Pegawai Tetap


Waktu

GajiPerJam GajiBulanan
TotalJamKerja BonusTahunan

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Class Pegawai Class PegawaiTetap : Pegawai
{ {
protected string idpegawai; private int gajibulanan;
protected string nama; private int bonustahunan;
protected string alamat
public void setValue()
public void displayData() {
{ idpegawai = “P001”;
Console.Write(nama + “-” + nama = “Bambang”;
alamat + “-” + idpegawai) alamat = “Jakarta”;
} gajibulanan = “5000000”;
} bonustahunan = “10000000”;
}
Code Structure:
Output :
Class <derived class> : <base public void display()
class> {
{ Console.WriteLine(nama);
... // ...
} }
}

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Abstract Class

C# enables you to create abstract classes that are used to provide partial
class implementation of an interface. You can complete implementation
by using the derived classes.

The rules for abstract class are :


o Cannot create an instance of an abstract class
o Cannot declare an abstract method outside an abstract class
o Cannot be declared sealed

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Abstract Class
Consider the following code :

abstract class Example class Program


{ {
//... static void main(string[]
} args)
{
Example ex = new Example();
Code Structure:
}
}
abstract class <class name>
{

}
This code will provide an error
because abstract class cannot be
instantiated.

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Abstract Method

Abstract methods are the methods without any body. The


implementation of an abstract method is done by the derived class.
When a derived class inherits the abstract method from the abstract
class, it must override the abstract methods.

Code Structure:

[access-specifier] abstract [return-type] method-name


([parameter]);

Example:

public abstract void displayData();

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Abstract Method
Abstract Class: Derived Class:

abstract class Animal class Carnivorous : Animal


{ {
protected string foodtype; public override void
public abstract void foodhabit(); foodhabit()
} {
foodtype = “meat”;
Console.WriteLine(foodtype);
}
Abstract method FoodHabits() is declared in
}
the abstract class Animal and then the abstract
method is inherited in Carnivorous and
Herbivorous class. class Herbivorous : Animal
{
public Output
override
: void
Abstract methods cannot contain any method foodhabit()
body. {
foodtype = “plants”;
Console.WriteLine(foodtype);
}
}

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Abstract Method
Abstract Class: Derived Class:

abstract class Animal class Carnivorous : Animal


{ {
protected string foodtype; public override void
public abstract void foodhabit(); foodhabits()
} {
foodtype = “meat”;
Console.WriteLine(foodtype);
}
}
This code will provide an error
because derived class does not Consider the following code:
implement inherited abstract
class Herbivorous : Animal
class. { Output :
public void display()
{
Console.Write(“Eat Plants!”);
}
}

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Sealed Class

There may be times when you do not need a class to be extended.


You could restrict users from inheriting the class by sealing the class
using the sealed keyword.

Example:

sealed class TestSealed


{
private int x;
public void MyMethod()
{
//...
}
}

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Sealed Method
Abstract Class: Derived Class:

abstract class Animal class Carnivorous : Animal


{ {
protected string foodtype; public override sealed void
public abstract void foodhabit(); foodhabit()
} {
foodtype = “meat”;
Console.WriteLine(foodtype);
In C# a method can't be declared as sealed. }
However, when we override a method in a }
derived class, we can declare the overridden
method as sealed. By declaring it as sealed, we
can avoid further overriding of this method. class Herbivorous : Carnivorous
{ Output :
public override void foodhabit()
This code will provide an error because {
Console.WriteLine(“Herbi!”);
derived class does not implement }
inherited abstract class. }

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Interfaces

Interface is used when you want a standard structure of methods


to be followed by the classes, where classes will implement the
functionality.
Declaring Interfaces:

interfaces Ipelanggan
{
void acceptPelanggan();
void displayPelanggan();
}

You cannot declare a variable in interfaces.

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Interfaces
interface declaration: implementation of interface by the classes:

interface iPelanggan class Member : iPelanggan


{ {
void acceptData(); public void acceptData()
void displayData(); {
} // ...
}
Interfaces declare methods, which are public void displayData()
implemented by classes. A class can inherit from {
single class but can implement from multiple // ...
interfaces. }
}
Output :

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Inheritance
Using Interfaces
interface declaration: implementation of interface by the classes:

interface iPelanggan class Member : iPelanggan


{ {
void acceptData(); public void acceptData()
void displayData(); {
} // ...
}
Interfaces declare methods, which are public void display()
implemented by classes. A class can inherit from {
single class but can implement from multiple // ...
interfaces. }
}
Output :
This code will provide an error because
class Member does not implement
interface member.

Q3M1 – OOP C# Dudy Fathan Ali S.Kom


Thank You!
Dudy Fathan Ali S.Kom
dudy.fathan@eng.ui.ac.id

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

You might also like