SlideShare a Scribd company logo
Programming in C#
Inheritance and Polymorphism
Renas R. Rekany
2018
Inheritance
▪ Inheritance allows a software developer to derive a new
class from an existing one.
▪ The existing class is called the parent, super, or base class.
▪ The derived class is called a child or subclass.
▪ The child inherits characteristics of the parent.
▪ Methods and data defined for the parent class.
▪ The child has special rights to the parents methods and data.
▪ Public access like anyone else
▪ Protected access available only to child classes.
▪ The child has its own unique behaviors and data.
Inheritance
▪ Inheritance relationships
are often shown
graphically in a class
diagram, with the arrow
pointing to the parent
class.
▪ Inheritance should create a
relationship, meaning the
child is a more specific
version of the parent.
Animal
Bird
Declaring a Derived Class
▪ Define a new class DerivedClass which extends
BaseClass
class BaseClass
{
// class contents
}
class DerivedClass : BaseClass
{
// class contents
}
Controlling Inheritance
▪ A child class inherits the methods and data defined for the
parent class; however, whether a data or method member of
a parent class is accessible in the child class depends on the
visibility modifier of a member.
▪ Variables and methods declared with private visibility are
not accessible in the child class
▪ However, a private data member defined in the parent class is still
part of the state of a derived class.
▪ Variables and methods declared with public visibility are
accessible; but public variables violate our goal of
encapsulation
▪ There is a third visibility modifier that helps in inheritance
situations: protected.
Inheritance
class color
{ public color() {MessageBox.Show("color"); }
public void fill(string s) { (Messagebox.Show(s); }
}
class green : color
{ public green() {MessageBox.Show ("green"); } }
private void button1_Click(object sender, EventArgs e)
{ green g = new green();
g.fill("red");
}
Inheritance
class animal
{ public animal() {MessageBox.Show("animal"); }
public void talk() {MessageBox.Show("animal talk"); }
public void greet() {MessageBox.Show("animal say hello"); } }
class dog : animal
{ public dog() {MessageBox.Show("dog"); }
public void sing() {MessageBox.Show("dog can sing"); } }
private void button1_Click(object sender, EventArgs e)
{ animal a = new animal();
a.talk();
a.greet();
dog d = new dog();
d.sing();
d.talk();
d.greet();
}
Notes about Inheritance
➢ Constructor can't be inheritance, they just invoked.
➢ First call base then derived
➢ Destructor can't be inheritance they just invoked.
➢ First call derived then base
Example
➢ Calculation
Add, Sub
➢ Calculation2
Mult, Div
Calculation1
Calculation2
Inheritance
class animal
{ public animal() {MessageBox.Show("animal"); }
public void talk() {MessageBox.Show("animal talk"); }
public void greet() {MessageBox.Show("animal say hello"); }
~animal() {MessageBox.Show("animal Destructor"); } }
class dog : animal
{ public dog() {MessageBox.Show("dog"); }
public void sing() {MessageBox.Show("dog can sing"); }
~dog() {MessageBox.Show("dog Destructor"); } }
private void button1_Click(object sender, EventArgs e)
{ animal a = new animal();
a.talk();
a.greet();
dog d = new dog();
d.sing();
d.talk();
d.greet(); }
Examples:
Base Classes and Derived Classes
Examples:
Base Classes and Derived Classes
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
Single Inheritance
▪ Some languages, e.g., C++, allow Multiple
inheritance, which allows a class to be
derived from two or more classes, inheriting
the members of all parents.
▪ C# and Java support single inheritance,
meaning that a derived class can have only
one parent class.
Class Hierarchies
▪ A child class of one parent can be the parent
of another child, forming a class hierarchy
Animal
Reptile Bird Mammal
Snake Lizard BatHorseParrot
Class Hierarchies
CommunityMember
Employee Student Alumnus
Faculty Staff
Professor Instructor
GraduateUnder
Class Hierarchies
Shape
TwoDimensionalShape ThreeDimensionalShape
Sphere Cube CylinderTriangleSquareCircle
Class Hierarchies
▪ An inherited member is continually passed
down the line
▪ Inheritance is transitive.
▪ Good class design puts all common features
as high in the hierarchy as is reasonable.
Avoids redundant code.
References and Inheritance
▪ An object reference can refer to an object of its
class, or to an object of any class derived from it by
inheritance.
▪ For example, if the Holiday class is used to derive a
child class called Friday, then a Holiday reference
can be used to point to a Fridayobject.
Holiday day;
day = new Holiday();
…
day = new Friday();
Executive
- bonus : double
+ AwardBonus(execBonus : double) : void
+ Pay() : double
Hourly
- hoursWorked : int
+ AddHours(moreHours : int) : void
+ ToString() : string
+ Pay() : double
Volunteer
+ Pay() : double
Employee
# socialSecurityNumber : String
# payRate : double
+ ToString() : string
+ Pay() : double
Polymorphism via Inheritance
StaffMember
# name : string
# address : string
# phone : string
+ ToString() : string
+ Pay() : double
Type of Polymorphism
 At run time, objects of a derived class may be treated as objects of a
base class in places such as method parameters and collections or arrays.
When this occurs, the object's declared type is no longer identical to its
run-time type.
 Base classes may define and implement virtual methods, and derived
classes can override them, which means they provide their own
definition and implementation. At run-time, when client code calls the
method, the CLR looks up the run-time type of the object, and invokes
that override of the virtual method. Thus in your source code you can
call a method on a base class, and cause a derived class's version of the
method to be executed.
Overriding Methods
 C# requires that all class definitions
communicate clearly their intentions.
 The keywords virtual, override and new provide
this communication.
 If a base class method is going to be overridden
it should be declared virtual.
 A derived class would then indicate that it
indeed does override the method with the
override keyword.
Overriding Methods
▪ A child class can override the definition of an
inherited method in favor of its own
▪ That is, a child can redefine a method that it
inherits from its parent
▪ The new method must have the same signature
as the parent's method, but can have a different
implementation.
▪ The type of the object executing the method
determines which version of the method is
invoked.
Overriding Methods
 If a derived class wishes to hide a method in
the parent class, it will use the new keyword.
This should be avoided.
Overloading vs. Overriding
▪ Overloading deals with
multiple methods in the
same class with the same
name but different
signatures
▪ Overloading lets you
define a similar
operation in different
ways for different data
▪ Example:
▪ int foo(string[] bar);
▪ int foo(int bar1, float a);
▪ Overriding deals with two
methods, one in a parent
class and one in a child
class, that have the same
signature
▪ Overriding lets you define
a similar operation in
different ways for different
object types
▪ Example:
▪ class Base {
▪ public virtual int foo() {}
}
▪ class Derived {
▪ public override int foo()
{}}
Example
public class Base
{
public virtual void Show()
{
MessageBox.Show("Show From Base Class.");
} }
public class Derived : Base
{
public override void Show()
{
MessageBox.Show("Show From Derived Class.");
} }

More Related Content

What's hot

Ruby and DCI
Ruby and DCIRuby and DCI
Ruby and DCI
Simon Courtois
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
tayyaba nawaz
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
HarshitaAshwani
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
VINOTH R
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
monikadeshmane
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#Svetlin Nakov
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
Python advance
Python advancePython advance
Python advance
Mukul Kirti Verma
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 

What's hot (20)

Ruby and DCI
Ruby and DCIRuby and DCI
Ruby and DCI
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Java unit2
Java unit2Java unit2
Java unit2
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Class 7 2ciclo
Class 7 2cicloClass 7 2ciclo
Class 7 2ciclo
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Python advance
Python advancePython advance
Python advance
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 

Similar to Object oriented programming inheritance

Inheritance
InheritanceInheritance
Inheritance
SangeethaSasi1
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
Anant240318
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
Ólafur Andri Ragnarsson
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
raahulwasule
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
Infocampus Logics Pvt.Ltd.
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
sinhacp
 
Inheritance
InheritanceInheritance
Inheritance
Mustafa Khan
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.ppt
Ravi Kumar
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
Ólafur Andri Ragnarsson
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
Shambhavi Vats
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
Abou Bakr Ashraf
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
Atikur Rahman
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
Tushar Desarda
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 

Similar to Object oriented programming inheritance (20)

Inheritance
InheritanceInheritance
Inheritance
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.ppt
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
Segundo avance
Segundo avanceSegundo avance
Segundo avance
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
OOPS
OOPSOOPS
OOPS
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 

More from Renas Rekany

decision making
decision makingdecision making
decision making
Renas Rekany
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
Renas Rekany
 
AI heuristic search
AI heuristic searchAI heuristic search
AI heuristic search
Renas Rekany
 
AI local search
AI local searchAI local search
AI local search
Renas Rekany
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
Renas Rekany
 
C# p9
C# p9C# p9
C# p8
C# p8C# p8
C# p7
C# p7C# p7
C# p6
C# p6C# p6
C# p5
C# p5C# p5
C# p4
C# p4C# p4
C# p3
C# p3C# p3
C# p2
C# p2C# p2
C# p1
C# p1C# p1
C# with Renas
C# with RenasC# with Renas
C# with Renas
Renas Rekany
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab AsaadRenas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 

More from Renas Rekany (20)

decision making
decision makingdecision making
decision making
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
 
AI heuristic search
AI heuristic searchAI heuristic search
AI heuristic search
 
AI local search
AI local searchAI local search
AI local search
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
 
C# p9
C# p9C# p9
C# p9
 
C# p8
C# p8C# p8
C# p8
 
C# p7
C# p7C# p7
C# p7
 
C# p6
C# p6C# p6
C# p6
 
C# p5
C# p5C# p5
C# p5
 
C# p4
C# p4C# p4
C# p4
 
C# p3
C# p3C# p3
C# p3
 
C# p2
C# p2C# p2
C# p2
 
C# p1
C# p1C# p1
C# p1
 
C# with Renas
C# with RenasC# with Renas
C# with Renas
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab AsaadRenas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Object oriented programming inheritance

  • 1. Programming in C# Inheritance and Polymorphism Renas R. Rekany 2018
  • 2. Inheritance ▪ Inheritance allows a software developer to derive a new class from an existing one. ▪ The existing class is called the parent, super, or base class. ▪ The derived class is called a child or subclass. ▪ The child inherits characteristics of the parent. ▪ Methods and data defined for the parent class. ▪ The child has special rights to the parents methods and data. ▪ Public access like anyone else ▪ Protected access available only to child classes. ▪ The child has its own unique behaviors and data.
  • 3. Inheritance ▪ Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class. ▪ Inheritance should create a relationship, meaning the child is a more specific version of the parent. Animal Bird
  • 4. Declaring a Derived Class ▪ Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class contents }
  • 5. Controlling Inheritance ▪ A child class inherits the methods and data defined for the parent class; however, whether a data or method member of a parent class is accessible in the child class depends on the visibility modifier of a member. ▪ Variables and methods declared with private visibility are not accessible in the child class ▪ However, a private data member defined in the parent class is still part of the state of a derived class. ▪ Variables and methods declared with public visibility are accessible; but public variables violate our goal of encapsulation ▪ There is a third visibility modifier that helps in inheritance situations: protected.
  • 6. Inheritance class color { public color() {MessageBox.Show("color"); } public void fill(string s) { (Messagebox.Show(s); } } class green : color { public green() {MessageBox.Show ("green"); } } private void button1_Click(object sender, EventArgs e) { green g = new green(); g.fill("red"); }
  • 7. Inheritance class animal { public animal() {MessageBox.Show("animal"); } public void talk() {MessageBox.Show("animal talk"); } public void greet() {MessageBox.Show("animal say hello"); } } class dog : animal { public dog() {MessageBox.Show("dog"); } public void sing() {MessageBox.Show("dog can sing"); } } private void button1_Click(object sender, EventArgs e) { animal a = new animal(); a.talk(); a.greet(); dog d = new dog(); d.sing(); d.talk(); d.greet(); }
  • 8. Notes about Inheritance ➢ Constructor can't be inheritance, they just invoked. ➢ First call base then derived ➢ Destructor can't be inheritance they just invoked. ➢ First call derived then base
  • 9. Example ➢ Calculation Add, Sub ➢ Calculation2 Mult, Div Calculation1 Calculation2
  • 10. Inheritance class animal { public animal() {MessageBox.Show("animal"); } public void talk() {MessageBox.Show("animal talk"); } public void greet() {MessageBox.Show("animal say hello"); } ~animal() {MessageBox.Show("animal Destructor"); } } class dog : animal { public dog() {MessageBox.Show("dog"); } public void sing() {MessageBox.Show("dog can sing"); } ~dog() {MessageBox.Show("dog Destructor"); } } private void button1_Click(object sender, EventArgs e) { animal a = new animal(); a.talk(); a.greet(); dog d = new dog(); d.sing(); d.talk(); d.greet(); }
  • 11. Examples: Base Classes and Derived Classes
  • 12. Examples: Base Classes and Derived Classes Base class Derived classes Student GraduateStudent UndergraduateStudent Shape Circle Triangle Rectangle Loan CarLoan HomeImprovementLoan MortgageLoan Employee FacultyMember StaffMember Account CheckingAccount SavingsAccount
  • 13. Single Inheritance ▪ Some languages, e.g., C++, allow Multiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents. ▪ C# and Java support single inheritance, meaning that a derived class can have only one parent class.
  • 14. Class Hierarchies ▪ A child class of one parent can be the parent of another child, forming a class hierarchy Animal Reptile Bird Mammal Snake Lizard BatHorseParrot
  • 15. Class Hierarchies CommunityMember Employee Student Alumnus Faculty Staff Professor Instructor GraduateUnder
  • 17. Class Hierarchies ▪ An inherited member is continually passed down the line ▪ Inheritance is transitive. ▪ Good class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code.
  • 18. References and Inheritance ▪ An object reference can refer to an object of its class, or to an object of any class derived from it by inheritance. ▪ For example, if the Holiday class is used to derive a child class called Friday, then a Holiday reference can be used to point to a Fridayobject. Holiday day; day = new Holiday(); … day = new Friday();
  • 19. Executive - bonus : double + AwardBonus(execBonus : double) : void + Pay() : double Hourly - hoursWorked : int + AddHours(moreHours : int) : void + ToString() : string + Pay() : double Volunteer + Pay() : double Employee # socialSecurityNumber : String # payRate : double + ToString() : string + Pay() : double Polymorphism via Inheritance StaffMember # name : string # address : string # phone : string + ToString() : string + Pay() : double
  • 20. Type of Polymorphism  At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type.  Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.
  • 21. Overriding Methods  C# requires that all class definitions communicate clearly their intentions.  The keywords virtual, override and new provide this communication.  If a base class method is going to be overridden it should be declared virtual.  A derived class would then indicate that it indeed does override the method with the override keyword.
  • 22. Overriding Methods ▪ A child class can override the definition of an inherited method in favor of its own ▪ That is, a child can redefine a method that it inherits from its parent ▪ The new method must have the same signature as the parent's method, but can have a different implementation. ▪ The type of the object executing the method determines which version of the method is invoked.
  • 23. Overriding Methods  If a derived class wishes to hide a method in the parent class, it will use the new keyword. This should be avoided.
  • 24. Overloading vs. Overriding ▪ Overloading deals with multiple methods in the same class with the same name but different signatures ▪ Overloading lets you define a similar operation in different ways for different data ▪ Example: ▪ int foo(string[] bar); ▪ int foo(int bar1, float a); ▪ Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature ▪ Overriding lets you define a similar operation in different ways for different object types ▪ Example: ▪ class Base { ▪ public virtual int foo() {} } ▪ class Derived { ▪ public override int foo() {}}
  • 25. Example public class Base { public virtual void Show() { MessageBox.Show("Show From Base Class."); } } public class Derived : Base { public override void Show() { MessageBox.Show("Show From Derived Class."); } }