SlideShare a Scribd company logo
1 of 17
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

More Related Content

What's hot

Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad 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 ProgrammingKrzysztof Opałka
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen 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 classTech_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
 

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
 
C++ oop
C++ oopC++ oop
C++ oop
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
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
 
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
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
C Basics
C BasicsC Basics
C Basics
 
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
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Lecture5
Lecture5Lecture5
Lecture5
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
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
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
java vs C#
java vs C#java vs C#
java vs C#
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
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++
 

Viewers also liked

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 DatabaseDudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationDudy 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 ServerDudy Ali
 
Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVADudy 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 KontrolDudy Ali
 
Information System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses KontrolInformation System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses KontrolDudy 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 EkstranetDudy Ali
 
Information System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen KeamananInformation System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen KeamananDudy Ali
 
Information System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen KeamananInformation System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen KeamananDudy 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 PenjualanRicky 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 KeamananDudy Ali
 
Information System Security - Serangan dan Pengawasan
Information System Security - Serangan dan PengawasanInformation System Security - Serangan dan Pengawasan
Information System Security - Serangan dan PengawasanDudy 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 - KriptografiDudy 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 JaringanDudy Ali
 

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
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
 
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
 
Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
 
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
 
Information System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses KontrolInformation System Security - Teknik Akses Kontrol
Information System Security - Teknik Akses Kontrol
 
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
 
Information System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen KeamananInformation System Security - Prinsip Manajemen Keamanan
Information System Security - Prinsip Manajemen Keamanan
 
Information System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen KeamananInformation System Security - Konsep Manajemen Keamanan
Information System Security - Konsep Manajemen Keamanan
 
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
 
MIS BAB 10
MIS BAB 10MIS BAB 10
MIS BAB 10
 
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
 
Information System Security - Serangan dan Pengawasan
Information System Security - Serangan dan PengawasanInformation System Security - Serangan dan Pengawasan
Information System Security - Serangan dan Pengawasan
 
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)
 
Information System Security - Kriptografi
Information System Security - KriptografiInformation System Security - Kriptografi
Information System Security - Kriptografi
 
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
 

Similar to Object Oriented Programming - Inheritance

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
Interfacevvpadhu
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro 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.pptrani 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 classvishal choudhary
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principlesmaznabili
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 

Similar to Object Oriented Programming - Inheritance (20)

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#
 
Interface
InterfaceInterface
Interface
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
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
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Interface
InterfaceInterface
Interface
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 

More from 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.NETDudy Ali
 
XML Schema Part 2
XML Schema Part 2XML Schema Part 2
XML Schema Part 2Dudy Ali
 
XML Schema Part 1
XML Schema Part 1XML Schema Part 1
XML Schema Part 1Dudy Ali
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML DocumentDudy Ali
 
Pengantar XML
Pengantar XMLPengantar XML
Pengantar XMLDudy Ali
 
Pengantar XML DOM
Pengantar XML DOMPengantar XML DOM
Pengantar XML DOMDudy Ali
 
Pengantar ADO.NET
Pengantar ADO.NETPengantar ADO.NET
Pengantar ADO.NETDudy Ali
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBCDudy Ali
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTXML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTDudy Ali
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma PengurutanAlgorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma PengurutanDudy Ali
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - PengantarAlgorithm & Data Structure - Pengantar
Algorithm & Data Structure - PengantarDudy Ali
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman WebWeb Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman WebDudy Ali
 
Web Programming Syaria - PHP
Web Programming Syaria - PHPWeb Programming Syaria - PHP
Web Programming Syaria - PHPDudy Ali
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management KnowledgeSoftware Project Management - Project Management Knowledge
Software Project Management - Project Management KnowledgeDudy Ali
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen ProyekSoftware Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen ProyekDudy Ali
 
Software Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen ProyekSoftware Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen ProyekDudy 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 SistemDudy Ali
 

More from Dudy Ali (20)

Understanding COM+
Understanding COM+Understanding COM+
Understanding COM+
 
Distributed Application Development (Introduction)
Distributed Application Development (Introduction)Distributed Application Development (Introduction)
Distributed Application Development (Introduction)
 
Review Materi ASP.NET
Review Materi ASP.NETReview Materi ASP.NET
Review Materi ASP.NET
 
XML Schema Part 2
XML Schema Part 2XML Schema Part 2
XML Schema Part 2
 
XML Schema Part 1
XML Schema Part 1XML Schema Part 1
XML Schema Part 1
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML Document
 
Pengantar XML
Pengantar XMLPengantar XML
Pengantar XML
 
Pengantar XML DOM
Pengantar XML DOMPengantar XML DOM
Pengantar XML DOM
 
Pengantar ADO.NET
Pengantar ADO.NETPengantar ADO.NET
Pengantar ADO.NET
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLTXML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma PengurutanAlgorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma Pengurutan
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - PengantarAlgorithm & Data Structure - Pengantar
Algorithm & Data Structure - Pengantar
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman WebWeb Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman Web
 
Web Programming Syaria - PHP
Web Programming Syaria - PHPWeb Programming Syaria - PHP
Web Programming Syaria - PHP
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management KnowledgeSoftware Project Management - Project Management Knowledge
Software Project Management - Project Management Knowledge
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen ProyekSoftware Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen Proyek
 
Software Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen ProyekSoftware Project Management - Pengenalan Manajemen Proyek
Software Project Management - Pengenalan Manajemen Proyek
 
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)
 
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
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
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.
  • 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 dudy.fathan@eng.ui.ac.id