SlideShare a Scribd company logo
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Inheritance
Q3M1
Dudy Fathan Ali, S.Kom (DFA)
2016
CEP - CCIT
Fakultas Teknik Universitas Indonesia
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.
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.
Inheritance
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
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 :
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:
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
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:
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:
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:
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:
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:
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.
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.
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:
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.
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Thank You!
Dudy Fathan Ali S.Kom
dudy.fathan@eng.ui.ac.id
Ad

More Related Content

What's hot (20)

Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
An Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented ProgrammingAn Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented Programming
Krzysztof Opałka
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
C Basics
C BasicsC Basics
C Basics
Sunil OS
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Lecture5
Lecture5Lecture5
Lecture5
Sunil Gupta
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
java vs C#
java vs C#java vs C#
java vs C#
Shivalik college of engineering
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
An Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented ProgrammingAn Introduction to Game Programming with Flash: Object Oriented Programming
An Introduction to Game Programming with Flash: Object Oriented Programming
Krzysztof Opałka
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
Liju Thomas
 

Viewers also liked (16)

Java CRUD Mechanism with SQL Server Database
Java CRUD Mechanism with SQL Server DatabaseJava CRUD Mechanism with SQL Server Database
Java CRUD Mechanism with SQL Server Database
Dudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
 
Database Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL ServerDatabase Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
 
Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Information System Security - Akuntabilitas dan Akses Kontrol
Information System Security - Akuntabilitas dan Akses KontrolInformation System Security - Akuntabilitas dan Akses Kontrol
Information System Security - Akuntabilitas dan Akses Kontrol
Dudy Ali
 
Information System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses KontrolInformation System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses Kontrol
Dudy Ali
 
Information System Security - Komponen Intranet dan Ekstranet
Information System Security - Komponen Intranet dan EkstranetInformation System Security - Komponen Intranet dan Ekstranet
Information System Security - Komponen Intranet dan Ekstranet
Dudy Ali
 
Information System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen KeamananInformation System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen Keamanan
Dudy Ali
 
Information System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen KeamananInformation System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen Keamanan
Dudy Ali
 
Diagram Konteks dan DFD Sistem Informasi Penjualan
Diagram Konteks dan DFD Sistem Informasi PenjualanDiagram Konteks dan DFD Sistem Informasi Penjualan
Diagram Konteks dan DFD Sistem Informasi Penjualan
Ricky Kusriana Subagja
 
MIS BAB 10
MIS BAB 10MIS BAB 10
MIS BAB 10
Riza Nurman
 
Information System Security - Konsep dan Kebijakan Keamanan
Information System Security - Konsep dan Kebijakan KeamananInformation System Security - Konsep dan Kebijakan Keamanan
Information System Security - Konsep dan Kebijakan Keamanan
Dudy Ali
 
Information System Security - Serangan dan Pengawasan
Information System Security - Serangan dan PengawasanInformation System Security - Serangan dan Pengawasan
Information System Security - Serangan dan Pengawasan
Dudy Ali
 
Perancangan (diagram softekz, dfd level 0,1,2)
Perancangan (diagram softekz, dfd level 0,1,2)Perancangan (diagram softekz, dfd level 0,1,2)
Perancangan (diagram softekz, dfd level 0,1,2)
Joel Marobo
 
Information System Security - Kriptografi
Information System Security - KriptografiInformation System Security - Kriptografi
Information System Security - Kriptografi
Dudy Ali
 
Information System Security - Keamanan Komunikasi dan Jaringan
Information System Security - Keamanan Komunikasi dan JaringanInformation System Security - Keamanan Komunikasi dan Jaringan
Information System Security - Keamanan Komunikasi dan Jaringan
Dudy Ali
 
Java CRUD Mechanism with SQL Server Database
Java CRUD Mechanism with SQL Server DatabaseJava CRUD Mechanism with SQL Server Database
Java CRUD Mechanism with SQL Server Database
Dudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
 
Database Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL ServerDatabase Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
 
Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Information System Security - Akuntabilitas dan Akses Kontrol
Information System Security - Akuntabilitas dan Akses KontrolInformation System Security - Akuntabilitas dan Akses Kontrol
Information System Security - Akuntabilitas dan Akses Kontrol
Dudy Ali
 
Information System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses KontrolInformation System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses Kontrol
Dudy Ali
 
Information System Security - Komponen Intranet dan Ekstranet
Information System Security - Komponen Intranet dan EkstranetInformation System Security - Komponen Intranet dan Ekstranet
Information System Security - Komponen Intranet dan Ekstranet
Dudy Ali
 
Information System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen KeamananInformation System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen Keamanan
Dudy Ali
 
Information System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen KeamananInformation System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen Keamanan
Dudy Ali
 
Diagram Konteks dan DFD Sistem Informasi Penjualan
Diagram Konteks dan DFD Sistem Informasi PenjualanDiagram Konteks dan DFD Sistem Informasi Penjualan
Diagram Konteks dan DFD Sistem Informasi Penjualan
Ricky Kusriana Subagja
 
Information System Security - Konsep dan Kebijakan Keamanan
Information System Security - Konsep dan Kebijakan KeamananInformation System Security - Konsep dan Kebijakan Keamanan
Information System Security - Konsep dan Kebijakan Keamanan
Dudy Ali
 
Information System Security - Serangan dan Pengawasan
Information System Security - Serangan dan PengawasanInformation System Security - Serangan dan Pengawasan
Information System Security - Serangan dan Pengawasan
Dudy Ali
 
Perancangan (diagram softekz, dfd level 0,1,2)
Perancangan (diagram softekz, dfd level 0,1,2)Perancangan (diagram softekz, dfd level 0,1,2)
Perancangan (diagram softekz, dfd level 0,1,2)
Joel Marobo
 
Information System Security - Kriptografi
Information System Security - KriptografiInformation System Security - Kriptografi
Information System Security - Kriptografi
Dudy Ali
 
Information System Security - Keamanan Komunikasi dan Jaringan
Information System Security - Keamanan Komunikasi dan JaringanInformation System Security - Keamanan Komunikasi dan Jaringan
Information System Security - Keamanan Komunikasi dan Jaringan
Dudy Ali
 
Ad

Similar to Object Oriented Programming - Inheritance (20)

Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Interface
InterfaceInterface
Interface
vvpadhu
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
vishal choudhary
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
shubhra chauhan
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
maznabili
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Interface
InterfaceInterface
Interface
vvpadhu
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
vishal choudhary
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
maznabili
 
Ad

More from Dudy Ali (20)

Understanding COM+
Understanding COM+Understanding COM+
Understanding COM+
Dudy Ali
 
Distributed Application Development (Introduction)
Distributed Application Development (Introduction)Distributed Application Development (Introduction)
Distributed Application Development (Introduction)
Dudy Ali
 
Review Materi ASP.NET
Review Materi ASP.NETReview Materi ASP.NET
Review Materi ASP.NET
Dudy Ali
 
XML Schema Part 2
XML Schema Part 2XML Schema Part 2
XML Schema Part 2
Dudy Ali
 
XML Schema Part 1
XML Schema Part 1XML Schema Part 1
XML Schema Part 1
Dudy Ali
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML Document
Dudy Ali
 
Pengantar XML
Pengantar XMLPengantar XML
Pengantar XML
Dudy Ali
 
Pengantar XML DOM
Pengantar XML DOMPengantar XML DOM
Pengantar XML DOM
Dudy Ali
 
Pengantar ADO.NET
Pengantar ADO.NETPengantar ADO.NET
Pengantar ADO.NET
Dudy Ali
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
Dudy Ali
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTXML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
Dudy Ali
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma PengurutanAlgorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - PengantarAlgorithm & Data Structure - Pengantar
Algorithm & Data Structure - Pengantar
Dudy Ali
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman WebWeb Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
 
Web Programming Syaria - PHP
Web Programming Syaria - PHPWeb Programming Syaria - PHP
Web Programming Syaria - PHP
Dudy Ali
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management KnowledgeSoftware Project Management - Project Management Knowledge
Software Project Management - Project Management Knowledge
Dudy Ali
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen ProyekSoftware Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen Proyek
Dudy Ali
 
Software Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen ProyekSoftware Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen Proyek
Dudy Ali
 
System Analysis and Design - Unified Modeling Language (UML)
System Analysis and Design - Unified Modeling Language (UML)System Analysis and Design - Unified Modeling Language (UML)
System Analysis and Design - Unified Modeling Language (UML)
Dudy Ali
 
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan SistemSystem Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
 
Understanding COM+
Understanding COM+Understanding COM+
Understanding COM+
Dudy Ali
 
Distributed Application Development (Introduction)
Distributed Application Development (Introduction)Distributed Application Development (Introduction)
Distributed Application Development (Introduction)
Dudy Ali
 
Review Materi ASP.NET
Review Materi ASP.NETReview Materi ASP.NET
Review Materi ASP.NET
Dudy Ali
 
XML Schema Part 2
XML Schema Part 2XML Schema Part 2
XML Schema Part 2
Dudy Ali
 
XML Schema Part 1
XML Schema Part 1XML Schema Part 1
XML Schema Part 1
Dudy Ali
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML Document
Dudy Ali
 
Pengantar XML
Pengantar XMLPengantar XML
Pengantar XML
Dudy Ali
 
Pengantar XML DOM
Pengantar XML DOMPengantar XML DOM
Pengantar XML DOM
Dudy Ali
 
Pengantar ADO.NET
Pengantar ADO.NETPengantar ADO.NET
Pengantar ADO.NET
Dudy Ali
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
Dudy Ali
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTXML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
Dudy Ali
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma PengurutanAlgorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - PengantarAlgorithm & Data Structure - Pengantar
Algorithm & Data Structure - Pengantar
Dudy Ali
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman WebWeb Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
 
Web Programming Syaria - PHP
Web Programming Syaria - PHPWeb Programming Syaria - PHP
Web Programming Syaria - PHP
Dudy Ali
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management KnowledgeSoftware Project Management - Project Management Knowledge
Software Project Management - Project Management Knowledge
Dudy Ali
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen ProyekSoftware Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen Proyek
Dudy Ali
 
Software Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen ProyekSoftware Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen Proyek
Dudy Ali
 
System Analysis and Design - Unified Modeling Language (UML)
System Analysis and Design - Unified Modeling Language (UML)System Analysis and Design - Unified Modeling Language (UML)
System Analysis and Design - Unified Modeling Language (UML)
Dudy Ali
 
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan SistemSystem Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
 

Recently uploaded (20)

AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 

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.
  • 4. Inheritance Q3M1 – OOP C# Dudy Fathan Ali S.Kom
  • 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 [email protected]