Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Object Oriented Programming - Inheritance
1. Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Inheritance
Q3M1
Dudy Fathan Ali, S.Kom (DFA)
2016
CEP - CCIT
Fakultas Teknik Universitas Indonesia
2. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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.
3. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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.
5. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Pegawai
IDPegawai
Nama
Alamat
Pegawai Tetap
GajiBulanan
BonusTahunan
Pegawai Paruh
Waktu
GajiPerJam
TotalJamKerja
The following figure is an example of inheritance in OOP :
6. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Class Pegawai
{
protected string idpegawai;
protected string nama;
protected string alamat
public void displayData()
{
Console.Write(nama + “-” +
alamat + “-” + idpegawai)
}
}
Output :
Class PegawaiTetap : Pegawai
{
private int gajibulanan;
private int bonustahunan;
public void setValue()
{
idpegawai = “P001”;
nama = “Bambang”;
alamat = “Jakarta”;
gajibulanan = “5000000”;
bonustahunan = “10000000”;
}
public void display()
{
Console.WriteLine(nama);
// ...
}
}
Class <derived class> : <base
class>
{
...
}
Code Structure:
7. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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
8. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Using Abstract Class
abstract class Example
{
//...
}
class Program
{
static void main(string[]
args)
{
Example ex = new Example();
}
}
Consider the following code :
This code will provide an error
because abstract class cannot be
instantiated.
abstract class <class name>
{
}
Code Structure:
9. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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.
[access-specifier] abstract [return-type] method-name
([parameter]);
Code Structure:
public abstract void displayData();
Example:
10. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
abstract class Animal
{
protected string foodtype;
public abstract void foodhabit();
}
Output :
class Carnivorous : Animal
{
public override void
foodhabit()
{
foodtype = “meat”;
Console.WriteLine(foodtype);
}
}
Using Abstract Method
class Herbivorous : Animal
{
public override void
foodhabit()
{
foodtype = “plants”;
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.
Abstract methods cannot contain any method
body.
Abstract Class: Derived Class:
11. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
abstract class Animal
{
protected string foodtype;
public abstract void foodhabit();
}
Output :
class Carnivorous : Animal
{
public override void
foodhabits()
{
foodtype = “meat”;
Console.WriteLine(foodtype);
}
}
Using Abstract Method
class Herbivorous : Animal
{
public void display()
{
Console.Write(“Eat Plants!”);
}
}
Abstract Class: Derived Class:
This code will provide an error
because derived class does not
implement inherited abstract
class.
Consider the following code:
12. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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.
sealed class TestSealed
{
private int x;
public void MyMethod()
{
//...
}
}
Example:
13. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
abstract class Animal
{
protected string foodtype;
public abstract void foodhabit();
}
Output :
class Carnivorous : Animal
{
public override sealed void
foodhabit()
{
foodtype = “meat”;
Console.WriteLine(foodtype);
}
}
Using Sealed Method
class Herbivorous : Carnivorous
{
public override void foodhabit()
{
Console.WriteLine(“Herbi!”);
}
}
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.
Abstract Class: Derived Class:
This code will provide an error because
derived class does not implement
inherited abstract class.
14. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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.
interfaces Ipelanggan
{
void acceptPelanggan();
void displayPelanggan();
}
Declaring Interfaces:
You cannot declare a variable in interfaces.
15. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
interface iPelanggan
{
void acceptData();
void displayData();
}
Output :
class Member : iPelanggan
{
public void acceptData()
{
// ...
}
public void displayData()
{
// ...
}
}
Using Interfaces
Interfaces declare methods, which are
implemented by classes. A class can inherit from
single class but can implement from multiple
interfaces.
interface declaration: implementation of interface by the classes:
16. Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
interface iPelanggan
{
void acceptData();
void displayData();
}
Output :
class Member : iPelanggan
{
public void acceptData()
{
// ...
}
public void display()
{
// ...
}
}
Using Interfaces
Interfaces declare methods, which are
implemented by classes. A class can inherit from
single class but can implement from multiple
interfaces.
interface declaration: implementation of interface by the classes:
This code will provide an error because
class Member does not implement
interface member.
17. Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Thank You!
Dudy Fathan Ali S.Kom
dudy.fathan@eng.ui.ac.id