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

More Related Content

What's hot (20)

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

Viewers also liked (16)

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

Similar to Object Oriented Programming - Inheritance (20)

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

More from Dudy Ali (20)

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

Recently uploaded (20)

PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Biography of Daniel Podor.pdf
Daniel Podor
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 

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]