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

Recommended

Object Oriented Programming - File Input & Output
Object Oriented Programming - File Input & Output
Dudy Ali
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
Dudy Ali
 
Object Oriented Programming - Value Types & Reference Types
Object Oriented Programming - Value Types & Reference Types
Dudy Ali
 
C++ questions And Answer
C++ questions And Answer
lavparmar007
 
C++ polymorphism
C++ polymorphism
FALLEE31188
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
Ameen Sha'arawi
 
2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 
Constructors and destructors
Constructors and destructors
Vineeta Garg
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
C++ oop
C++ oop
Sunil OS
 
Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Method 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 Programming
Krzysztof Opałka
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
C Basics
C Basics
Sunil OS
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Lecture5
Lecture5
Sunil Gupta
 
Abstract class in c++
Abstract class in c++
Sujan Mia
 
Virtual base class
Virtual base class
Tech_MX
 
Object 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#
OpenSource Technologies Pvt. Ltd.
 
Constructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
java vs C#
java vs C#
Shivalik college of engineering
 
OOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
Java CRUD Mechanism with SQL Server Database
Java CRUD Mechanism with SQL Server Database
Dudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
 

More Related Content

What's hot (20)

Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
C++ oop
C++ oop
Sunil OS
 
Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Method 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 Programming
Krzysztof Opałka
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
C Basics
C Basics
Sunil OS
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Lecture5
Lecture5
Sunil Gupta
 
Abstract class in c++
Abstract class in c++
Sujan Mia
 
Virtual base class
Virtual base class
Tech_MX
 
Object 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#
OpenSource Technologies Pvt. Ltd.
 
Constructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
java vs C#
java vs C#
Shivalik college of engineering
 
OOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
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, polymorphism
Jawad Khan
 
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 Programming
Krzysztof Opałka
 
Chapter 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
Kamlesh Makvana
 
I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Abstract class in c++
Abstract class in c++
Sujan Mia
 
Virtual base class
Virtual base class
Tech_MX
 
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 Database
Dudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
 
Database Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
 
Network Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Information 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 Kontrol
Dudy Ali
 
Information 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 Keamanan
Dudy Ali
 
Information 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 Penjualan
Ricky Kusriana Subagja
 
MIS BAB 10
MIS BAB 10
Riza Nurman
 
Information 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 Pengawasan
Dudy Ali
 
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 - Kriptografi
Dudy Ali
 
Information 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 Database
Dudy Ali
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
 
Database Introduction - Akses Data dengan SQL Server
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
 
Network Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Information 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 Kontrol
Dudy Ali
 
Information 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 Keamanan
Dudy Ali
 
Information 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 Penjualan
Ricky Kusriana Subagja
 
Information 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 Pengawasan
Dudy Ali
 
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 - Kriptografi
Dudy Ali
 
Information System Security - Keamanan Komunikasi dan Jaringan
Information System Security - Keamanan Komunikasi dan Jaringan
Dudy Ali
 
Ad

Similar to Object Oriented Programming - Inheritance (20)

Object oriented programming inheritance
Object oriented programming inheritance
Renas Rekany
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
Abou Bakr Ashraf
 
Inheritance.pptx
Inheritance.pptx
PRIYACHAURASIYA25
 
Inheritance
Inheritance
abhay singh
 
Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
Akhil Mittal
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
RitikAhlawat1
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Inheritance
Inheritance
prabhat kumar
 
29csharp
29csharp
Sireesh K
 
29c
29c
Sireesh K
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
chetanpatilcp783
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NET
Sabith Byari
 
Ganesh groups
Ganesh groups
Ganesh Amirineni
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
Anant240318
 
Unit 7 inheritance
Unit 7 inheritance
atcnerd
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 8 Inheritance
Chapter 8 Inheritance
Amrit Kaur
 
Object oriented programming
Object oriented programming
Saiful Islam Sany
 
Object oriented programming inheritance
Object oriented programming inheritance
Renas Rekany
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
Abou Bakr Ashraf
 
Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
Akhil Mittal
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
RitikAhlawat1
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NET
Sabith Byari
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
Anant240318
 
Unit 7 inheritance
Unit 7 inheritance
atcnerd
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
Chapter 8 Inheritance
Chapter 8 Inheritance
Amrit Kaur
 
Ad

More from Dudy Ali (20)

Understanding COM+
Understanding COM+
Dudy Ali
 
Distributed Application Development (Introduction)
Distributed Application Development (Introduction)
Dudy Ali
 
Review Materi ASP.NET
Review Materi ASP.NET
Dudy Ali
 
XML Schema Part 2
XML Schema Part 2
Dudy Ali
 
XML Schema Part 1
XML Schema Part 1
Dudy Ali
 
Rendering XML Document
Rendering XML Document
Dudy Ali
 
Pengantar XML
Pengantar XML
Dudy Ali
 
Pengantar XML DOM
Pengantar XML DOM
Dudy Ali
 
Pengantar ADO.NET
Pengantar ADO.NET
Dudy Ali
 
Database Connectivity with JDBC
Database Connectivity with JDBC
Dudy Ali
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
Dudy Ali
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - Pengantar
Dudy Ali
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
 
Web Programming Syaria - PHP
Web Programming Syaria - PHP
Dudy Ali
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management Knowledge
Dudy Ali
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen Proyek
Dudy Ali
 
Software 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)
Dudy Ali
 
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
 
Understanding COM+
Understanding COM+
Dudy Ali
 
Distributed Application Development (Introduction)
Distributed Application Development (Introduction)
Dudy Ali
 
Review Materi ASP.NET
Review Materi ASP.NET
Dudy Ali
 
XML Schema Part 2
XML Schema Part 2
Dudy Ali
 
XML Schema Part 1
XML Schema Part 1
Dudy Ali
 
Rendering XML Document
Rendering XML Document
Dudy Ali
 
Pengantar XML
Pengantar XML
Dudy Ali
 
Pengantar XML DOM
Pengantar XML DOM
Dudy Ali
 
Pengantar ADO.NET
Pengantar ADO.NET
Dudy Ali
 
Database Connectivity with JDBC
Database Connectivity with JDBC
Dudy Ali
 
XML - Displaying Data ith XSLT
XML - Displaying Data ith XSLT
Dudy Ali
 
Algorithm & Data Structure - Algoritma Pengurutan
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
 
Algorithm & Data Structure - Pengantar
Algorithm & Data Structure - Pengantar
Dudy Ali
 
Web Programming Syaria - Pengenalan Halaman Web
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
 
Web Programming Syaria - PHP
Web Programming Syaria - PHP
Dudy Ali
 
Software Project Management - Project Management Knowledge
Software Project Management - Project Management Knowledge
Dudy Ali
 
Software Project Management - Proses Manajemen Proyek
Software Project Management - Proses Manajemen Proyek
Dudy Ali
 
Software 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)
Dudy Ali
 
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
 

Recently uploaded (20)

No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 

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]