This document discusses inheritance and polymorphism in C#. It defines inheritance as allowing a derived class to inherit characteristics from a parent class. Methods and data from the parent class can be accessed differently depending on visibility modifiers like public, private, and protected. Inheritance allows class hierarchies to be formed. Polymorphism is enabled through inheritance and overriding methods in derived classes. Examples are provided to demonstrate inheritance, overriding methods, and class hierarchies.
Inheritance allows a class to inherit characteristics from a parent class. The child class inherits methods and data from the parent class. Inheritance is shown with arrows pointing from the child class to the parent class. Common examples of inheritance hierarchies include shapes, animals, and accounts. The protected access modifier allows child classes to access protected members of the parent class. Polymorphism through inheritance allows a reference variable to refer to objects of different classes in the inheritance hierarchy.
Inheritance, interfaces, abstraction and polymorphism are key object-oriented programming concepts in C#. Inheritance allows a class to inherit characteristics from a parent class. Interfaces allow classes to implement common behaviors without inheritance. Abstract classes and methods define common behaviors for derived classes to implement. Polymorphism allows derived classes to override base class behaviors through method overriding.
The document provides an overview of C++ vs C# by Shubhra Chauhan. It discusses the key object-oriented programming concepts like classes, objects, inheritance, polymorphism, and how they are implemented in C++ and C#. It includes code examples to demonstrate class usage and inheritance in both languages. The document also compares some similarities and differences between C++ and C# like support for pointers, preprocessors, structures, and goto statements.
The document discusses object-oriented programming concepts including inheritance, abstraction, encapsulation, and polymorphism. It covers fundamental OOP principles, defining inheritance between classes, using abstract classes and interfaces, encapsulating data within classes, and allowing polymorphism through inheritance. Specific examples are provided to demonstrate each concept.
Here are the key principles of Object-Oriented Programming (OOP) in Java:
1. Encapsulation: It wraps code and data together into a single unit called class. The data in a class can be accessed and manipulated only through the methods of the class. This prevents data from direct modification which is called data hiding.
2. Inheritance: It allows one class to acquire properties of another class. The child class inherits attributes and behaviors of the parent class. This allows code reusability and is-a relationship between classes.
3. Polymorphism: It allows different classes to have same method name but with different implementations. This is achieved by method overriding in Java. Polymorphism allows one
- Classes are blueprints for objects in C#. Objects are instances of classes. Classes contain data fields, methods, and other members.
- There are different access modifiers like public, private, and protected that control access to class members. Constructors initialize new objects, and destructors perform cleanup when objects are destroyed.
- Inheritance allows classes to inherit members from base classes. Polymorphism allows classes to share common interfaces while providing different implementations. Interfaces define contracts without implementation, while abstract classes can contain partial implementations.
- Encapsulation hides implementation details within a class. Abstraction exposes only necessary details to users through public interfaces. Extension methods can add methods to existing types without creating new derived types.
Object oriented programming Fundamental ConceptsBharat Kalia
The document discusses four fundamental principles of object-oriented programming (OOP): inheritance, abstraction, encapsulation, and polymorphism. Inheritance allows classes to inherit attributes and behaviors from parent classes. Abstraction focuses on relevant properties and hides unnecessary details through techniques like abstract classes and interfaces. Encapsulation hides internal implementation details and exposes a public interface through properties and methods. Polymorphism enables classes to take on multiple forms through inheritance and method overriding.
Inheritance and polymorphism are key object-oriented programming concepts supported in C#. Inheritance allows a child class to inherit properties and behaviors from a parent class. Polymorphism refers to classes having different implementations of the same method, allowing the method to behave differently depending on the object that calls it. Virtual methods allow subclasses to override behaviors of parent classes, while the new keyword is used to hide inherited methods instead of overriding them.
Inheritance allows a class to inherit properties from another class. Subclasses inherit attributes and methods from the base/parent class without redefining them. Subclasses can add their own unique attributes as well. Abstract classes provide a common definition for subclasses to share but cannot be instantiated directly. Interfaces define a contract that subclasses must implement but provide no implementation. Polymorphism allows classes to take different forms through method overloading and overriding.
Object-oriented programming uses objects and classes to organize code and data. The key principles are encapsulation, inheritance, abstraction, and polymorphism. Encapsulation hides unnecessary details within classes and provides a clear interface. Inheritance allows classes to inherit and extend functionality from parent classes. Abstraction deals with objects based on important characteristics while ignoring details. Polymorphism allows working with different objects in the same way by defining abstract behavior implementations.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, inheritance, encapsulation, and polymorphism. It defines key terms like class and object, and explains how C# supports OOP principles such as defining classes with methods and properties, extending classes through inheritance, hiding implementation through encapsulation, and allowing polymorphic behavior through function overloading and overriding. Abstract classes and sealed modifiers are also covered. The document is intended to help explain basic OOP concepts in C# to readers.
This document provides an overview of polymorphism in object-oriented programming. It discusses:
1) Polymorphism allows the same method invocation to produce different results depending on the type of object, enabling "programming to the general type."
2) When a method is called through a superclass variable, the correct subclass version is called based on the object's type, not the variable type.
3) Polymorphism promotes extensibility by allowing new object types to respond to existing method calls without modifying existing code.
This document discusses object inheritance in systems analysis and design. It covers key concepts like inheritance, composition, aggregation, and the relationships between classes. It explains how inheritance allows classes to inherit attributes and behaviors from parent classes, and how child classes can specialize or extend parent classes through overriding and adding new functionality. The document also discusses the differences between single and multiple inheritance and how inheritance is implemented in languages like Java and .NET.
This document provides an overview of object-oriented programming (OOP) concepts including class and object, encapsulation, inheritance, polymorphism, interfaces, delegates, and access modifiers. It discusses how classes define objects, encapsulation binds data and methods into entities, inheritance creates new classes from existing ones, polymorphism allows different implementations of methods, interfaces define groups of related functionalities, delegates refer to methods, and access modifiers control visibility. Namespaces are used to organize code into unique scopes.
This document provides an introduction to object-oriented programming (OOP) concepts. It discusses procedural programming and its limitations. OOP aims to overcome these limitations through concepts like encapsulation, inheritance, polymorphism, and abstraction. Encapsulation binds data and functions together in classes. Inheritance allows deriving new classes from existing classes. Polymorphism allows the same action to be performed in different ways. Abstraction describes things at a conceptual level. Examples are provided to illustrate these core OOP concepts. The next lecture will cover implementing OOP in C# and building a game with Unity 3D.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, the class lifecycle, accessibility, static vs instance, inheritance, polymorphism, and encapsulation. It also addresses some common questions about overriding methods, overriding private methods, declaring override methods as static, and whether a class can be instantiated if its constructor is private. The document was presented by Eng. Medhat Dawoud and encourages contacting him with any other questions.
Inheritance allows classes to inherit properties and methods from parent classes. This provides code reusability and abstraction. Polymorphism allows classes to have methods with the same name but different implementations. This includes method overloading and overriding. Encapsulation wraps data and methods into a single unit to hide internal details and secure data. Abstraction hides background details and only shows required functionality to the user. Interfaces only contain method declarations while abstract classes can contain both declarations and partial implementations.
This document discusses various types of polymorphism in C#, including single inheritance, multi-level inheritance, hierarchical inheritance, and aggregation. Single inheritance allows a derived class to inherit properties and behaviors from a single base class. Multi-level inheritance involves inheriting from another inherited class. Hierarchical inheritance involves inheriting from a single base class but having multiple derived classes. Aggregation represents a "has-a" relationship where one class contains an instance of another class. The document provides examples to illustrate each type of polymorphism.
This document discusses various types of polymorphism in C#, including inheritance, base and derived classes, single inheritance, multi-level inheritance, hierarchical inheritance, and aggregation. It provides examples of each type of inheritance using C# code. Single inheritance allows a derived class to inherit properties and methods from one base class. Multi-level inheritance involves inheriting from another inherited class. Hierarchical inheritance involves inheriting from a single base class but having multiple derived classes. Aggregation represents a "has-a" relationship between classes.
Object Oriented Programming concepts that are used for all the programming languages
The OOPs contains Class and Objects
It contains four main pillars
that are
Inheritance
Encapsulation
Polymorphism
Abstraction
with the four pillars OOPs are going to make our coding more efficiently
The OOPs help us in organizing the code in a formal way
it helps in code reusability
OOP is an object-oriented programming design philosophy that uses classes and objects. Everything is modeled as self-contained objects that have properties and methods. The key concepts of OOP include encapsulation, inheritance, abstraction, and polymorphism. Encapsulation combines data and methods within an object. Inheritance allows classes to inherit properties and behaviors from parent classes. Abstraction hides unnecessary details and focuses on important aspects. Polymorphism allows the same method to operate on many different types of objects.
This chapter discusses decision making, object-oriented fundamentals, inner classes, and exception handling in Java. It describes the use of if and switch statements for decision making. It also covers access specifiers, encapsulation, inheritance, polymorphism, abstract classes, interfaces, inner classes, and exceptions.
Object-oriented programming (OOP) involves splitting a program into objects that contain both data and functions. OOP allows developers to define objects, their properties, and relationships. Classes are blueprints that define objects and don't use memory, while objects are instances of classes that hold both data and methods. Key concepts of OOP include inheritance, abstraction, polymorphism, and encapsulation.
Object Oriented Concepts
Defining class
Object of class
Access modifier
Feature of OOPs
Inheritance
Abstraction
Polymorphism
Encapsulation
2.2.1 Defining classes, class members, Interfaces, properties
2.2.2 Access modifiers, Implementation of class, interface and properties
2.2.3 Concept of hiding base class methods, Overriding
2.2.4 Event Handling
The document discusses object-oriented programming concepts including encapsulation, composition, inheritance, polymorphism, abstract classes, and interfaces. It provides examples of how these concepts are implemented in C# through classes, inheritance between base and derived classes, abstract methods, and interfaces. It also summarizes key features like polymorphism which allows treating a derived class object as its base class.
Inheritance and polymorphism are key object-oriented programming concepts supported in C#. Inheritance allows a child class to inherit properties and behaviors from a parent class. Polymorphism refers to classes having different implementations of the same method, allowing the method to behave differently depending on the object that calls it. Virtual methods allow subclasses to override behaviors of parent classes, while the new keyword is used to hide inherited methods instead of overriding them.
Inheritance allows a class to inherit properties from another class. Subclasses inherit attributes and methods from the base/parent class without redefining them. Subclasses can add their own unique attributes as well. Abstract classes provide a common definition for subclasses to share but cannot be instantiated directly. Interfaces define a contract that subclasses must implement but provide no implementation. Polymorphism allows classes to take different forms through method overloading and overriding.
Object-oriented programming uses objects and classes to organize code and data. The key principles are encapsulation, inheritance, abstraction, and polymorphism. Encapsulation hides unnecessary details within classes and provides a clear interface. Inheritance allows classes to inherit and extend functionality from parent classes. Abstraction deals with objects based on important characteristics while ignoring details. Polymorphism allows working with different objects in the same way by defining abstract behavior implementations.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, inheritance, encapsulation, and polymorphism. It defines key terms like class and object, and explains how C# supports OOP principles such as defining classes with methods and properties, extending classes through inheritance, hiding implementation through encapsulation, and allowing polymorphic behavior through function overloading and overriding. Abstract classes and sealed modifiers are also covered. The document is intended to help explain basic OOP concepts in C# to readers.
This document provides an overview of polymorphism in object-oriented programming. It discusses:
1) Polymorphism allows the same method invocation to produce different results depending on the type of object, enabling "programming to the general type."
2) When a method is called through a superclass variable, the correct subclass version is called based on the object's type, not the variable type.
3) Polymorphism promotes extensibility by allowing new object types to respond to existing method calls without modifying existing code.
This document discusses object inheritance in systems analysis and design. It covers key concepts like inheritance, composition, aggregation, and the relationships between classes. It explains how inheritance allows classes to inherit attributes and behaviors from parent classes, and how child classes can specialize or extend parent classes through overriding and adding new functionality. The document also discusses the differences between single and multiple inheritance and how inheritance is implemented in languages like Java and .NET.
This document provides an overview of object-oriented programming (OOP) concepts including class and object, encapsulation, inheritance, polymorphism, interfaces, delegates, and access modifiers. It discusses how classes define objects, encapsulation binds data and methods into entities, inheritance creates new classes from existing ones, polymorphism allows different implementations of methods, interfaces define groups of related functionalities, delegates refer to methods, and access modifiers control visibility. Namespaces are used to organize code into unique scopes.
This document provides an introduction to object-oriented programming (OOP) concepts. It discusses procedural programming and its limitations. OOP aims to overcome these limitations through concepts like encapsulation, inheritance, polymorphism, and abstraction. Encapsulation binds data and functions together in classes. Inheritance allows deriving new classes from existing classes. Polymorphism allows the same action to be performed in different ways. Abstraction describes things at a conceptual level. Examples are provided to illustrate these core OOP concepts. The next lecture will cover implementing OOP in C# and building a game with Unity 3D.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, the class lifecycle, accessibility, static vs instance, inheritance, polymorphism, and encapsulation. It also addresses some common questions about overriding methods, overriding private methods, declaring override methods as static, and whether a class can be instantiated if its constructor is private. The document was presented by Eng. Medhat Dawoud and encourages contacting him with any other questions.
Inheritance allows classes to inherit properties and methods from parent classes. This provides code reusability and abstraction. Polymorphism allows classes to have methods with the same name but different implementations. This includes method overloading and overriding. Encapsulation wraps data and methods into a single unit to hide internal details and secure data. Abstraction hides background details and only shows required functionality to the user. Interfaces only contain method declarations while abstract classes can contain both declarations and partial implementations.
This document discusses various types of polymorphism in C#, including single inheritance, multi-level inheritance, hierarchical inheritance, and aggregation. Single inheritance allows a derived class to inherit properties and behaviors from a single base class. Multi-level inheritance involves inheriting from another inherited class. Hierarchical inheritance involves inheriting from a single base class but having multiple derived classes. Aggregation represents a "has-a" relationship where one class contains an instance of another class. The document provides examples to illustrate each type of polymorphism.
This document discusses various types of polymorphism in C#, including inheritance, base and derived classes, single inheritance, multi-level inheritance, hierarchical inheritance, and aggregation. It provides examples of each type of inheritance using C# code. Single inheritance allows a derived class to inherit properties and methods from one base class. Multi-level inheritance involves inheriting from another inherited class. Hierarchical inheritance involves inheriting from a single base class but having multiple derived classes. Aggregation represents a "has-a" relationship between classes.
Object Oriented Programming concepts that are used for all the programming languages
The OOPs contains Class and Objects
It contains four main pillars
that are
Inheritance
Encapsulation
Polymorphism
Abstraction
with the four pillars OOPs are going to make our coding more efficiently
The OOPs help us in organizing the code in a formal way
it helps in code reusability
OOP is an object-oriented programming design philosophy that uses classes and objects. Everything is modeled as self-contained objects that have properties and methods. The key concepts of OOP include encapsulation, inheritance, abstraction, and polymorphism. Encapsulation combines data and methods within an object. Inheritance allows classes to inherit properties and behaviors from parent classes. Abstraction hides unnecessary details and focuses on important aspects. Polymorphism allows the same method to operate on many different types of objects.
This chapter discusses decision making, object-oriented fundamentals, inner classes, and exception handling in Java. It describes the use of if and switch statements for decision making. It also covers access specifiers, encapsulation, inheritance, polymorphism, abstract classes, interfaces, inner classes, and exceptions.
Object-oriented programming (OOP) involves splitting a program into objects that contain both data and functions. OOP allows developers to define objects, their properties, and relationships. Classes are blueprints that define objects and don't use memory, while objects are instances of classes that hold both data and methods. Key concepts of OOP include inheritance, abstraction, polymorphism, and encapsulation.
Object Oriented Concepts
Defining class
Object of class
Access modifier
Feature of OOPs
Inheritance
Abstraction
Polymorphism
Encapsulation
2.2.1 Defining classes, class members, Interfaces, properties
2.2.2 Access modifiers, Implementation of class, interface and properties
2.2.3 Concept of hiding base class methods, Overriding
2.2.4 Event Handling
The document discusses object-oriented programming concepts including encapsulation, composition, inheritance, polymorphism, abstract classes, and interfaces. It provides examples of how these concepts are implemented in C# through classes, inheritance between base and derived classes, abstract methods, and interfaces. It also summarizes key features like polymorphism which allows treating a derived class object as its base class.
Artificial intelligence Presented by JM.jmansha170
AI (Artificial Intelligence) :
"AI is the ability of machines to mimic human intelligence, such as learning, decision-making, and problem-solving."
Important Points about AI:
1. Learning – AI can learn from data (Machine Learning).
2. Automation – It helps automate repetitive tasks.
3. Decision Making – AI can analyze and make decisions faster than humans.
4. Natural Language Processing (NLP) – AI can understand and generate human language.
5. Vision & Recognition – AI can recognize images, faces, and patterns.
6. Used In – Healthcare, finance, robotics, education, and more.
Owner By:
Name : Junaid Mansha
Work : Web Developer and Graphics Designer
Contact us : +92 322 2291672
Email : [email protected]
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...EduSkills OECD
Deborah Nusche, Senior Analyst, OECD presents at the OECD webinar 'Trends Spotting: Strategic foresight for tomorrow’s education systems' on 5 June 2025. You can check out the webinar on the website https://oecdedutoday.com/webinars/ Other speakers included: Deborah Nusche, Senior Analyst, OECD
Sophie Howe, Future Governance Adviser at the School of International Futures, first Future Generations Commissioner for Wales (2016-2023)
Davina Marie, Interdisciplinary Lead, Queens College London
Thomas Jørgensen, Director for Policy Coordination and Foresight at European University Association
Slides from a Capitol Technology University presentation covering doctoral programs offered by the university. All programs are online, and regionally accredited. The presentation covers degree program details, tuition, financial aid and the application process.
Ray Dalio How Countries go Broke the Big CycleDadang Solihin
A complete and practical understanding of the Big Debt Cycle. A much more practical understanding of how supply and demand really work compared to the conventional economic thinking. A complete and practical understanding of the Overall Big Cycle, which is driven by the Big Debt Cycle and the other major cycles, including the big political cycle within countries that changes political orders and the big geopolitical cycle that changes world orders.
This presentation was provided by Nicole 'Nici" Pfeiffer of the Center for Open Science (COS), during the first session of our 2025 NISO training series "Secrets to Changing Behavior in Scholarly Communications." Session One was held June 5, 2025.
How to Create a Rainbow Man Effect in Odoo 18Celine George
In Odoo 18, the Rainbow Man animation adds a playful and motivating touch to task completion. This cheerful effect appears after specific user actions, like marking a CRM opportunity as won. It’s designed to enhance user experience by making routine tasks more engaging.
How to Manage & Create a New Department in Odoo 18 EmployeeCeline George
In Odoo 18's Employee module, organizing your workforce into departments enhances management and reporting efficiency. Departments are a crucial organizational unit within the Employee module.
Adam Grant: Transforming Work Culture Through Organizational PsychologyPrachi Shah
This presentation explores the groundbreaking work of Adam Grant, renowned organizational psychologist and bestselling author. It highlights his key theories on giving, motivation, leadership, and workplace dynamics that have revolutionized how organizations think about productivity, collaboration, and employee well-being. Ideal for students, HR professionals, and leadership enthusiasts, this deck includes insights from his major works like Give and Take, Originals, and Think Again, along with interactive elements for enhanced engagement.
Different pricelists for different shops in odoo Point of Sale in Odoo 17Celine George
Price lists are a useful tool for managing the costs of your goods and services. This can assist you in working with other businesses effectively and maximizing your revenues. Additionally, you can provide your customers discounts by using price lists.
A short update and next week. I am writing both Session 9 and Orientation S1.
As a Guest Student,
You are now upgraded to Grad Level.
See Uploads for “Student Checkin” & “S8”. Thx.
Thank you for attending our workshops.
If you are new, do welcome.
Grad Students: I am planning a Reiki-Yoga Master Course (As a package). I’m Fusing both together.
This will include the foundation of each practice. Our Free Workshops can be used with any Reiki Yoga training package. Traditional Reiki does host rules and ethics. Its silent and within the JP Culture/Area/Training/Word of Mouth. It allows remote healing but there’s limits As practitioners and masters. We are not allowed to share certain secrets/tools. Some content is designed only for “Masters”. Some yoga are similar like the Kriya Yoga-Church (Vowed Lessons). We will review both Reiki and Yoga (Master tools) in the Course upcoming.
Session Practice, For Reference:
Before starting a session, Make sure to check your environment. Nothing stressful. Later, You can decorate a space as well.
Check the comfort level, any needed resources (Yoga/Reiki/Spa Props), or Meditation Asst?
Props can be oils, sage, incense, candles, crystals, pillows, blankets, yoga mat, any theme applies.
Select your comfort Pose. This can be standing, sitting, laying down, or a combination.
Monitor your breath. You can add exercises.
Add any mantras or affirmations. This does aid mind and spirit. It helps you to focus.
Also you can set intentions using a candle.
The Yoga-key is balancing mind, body, and spirit.
Finally, The Duration can be long or short.
Its a good session base for any style.
Next Week’s Focus:
A continuation of Intuition Development. We will review the Chakra System - Our temple. A misguided, misused situation lol. This will also serve Attunement later.
For Sponsor,
General updates,
& Donations:
Please visit:
https://ldmchapels.weebly.com
Strengthened Senior High School - Landas Tool Kit.pptxSteffMusniQuiballo
Landas Tool Kit is a very helpful guide in guiding the Senior High School students on their SHS academic journey. It will pave the way on what curriculum exits will they choose and fit in.
2. C# Classes
Classes are used to accomplish:
Modularity: Scope for global (static) methods
Blueprints for generating objects or instances:
Per instance data and method signatures
Classes support
Data encapsulation - private data and
implementation.
Inheritance - code reuse
3. 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 any one else
Protected access available only to child classes (and their
descendants).
The child has its own unique behaviors and data.
4. Inheritance
Inheritance relationships
are often shown
graphically in a class
diagram, with the arrow
pointing to the parent
class.
Inheritance should create
an is-a relationship,
meaning the child is a
more specific version of
the parent.
Animal
Bird
5. 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
6. Declaring a Derived Class
Define a new class DerivedClass which
extends BaseClass
class BaseClass
{
// class contents
}
class DerivedClass : BaseClass
{
// class contents
}
7. 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.
8. + public
- private
# protected
The protected Modifier
Variables and methods
declared with protected
visibility in a parent class
are only accessible by a
child class or any class
derived from that class
Book
# pages : int
+ GetNumberOfPages() : void
Dictionary
- definition : int
+ PrintDefinitionMessage() : void
9. 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.
10. 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.
11. 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 Bat
Horse
Parrot
14. 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.
15. 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 Christmas, then a
Holiday reference can be used to point to a
Christmas object. Holiday day;
day = new Holiday();
…
day = new Christmas();
16. Dynamic Binding
A polymorphic reference is one which
can refer to different types of objects at
different times. It morphs!
The type of the actual instance, not the
declared type, determines which method
is invoked.
Polymorphic references are therefore
resolved at run-time, not during
compilation.
This is called dynamic binding.
17. Dynamic Binding
Suppose the Holiday class has a method
called Celebrate, and the Christmas
class redefines it (overrides it).
Now consider the following invocation:
day.Celebrate();
If day refers to a Holiday object, it
invokes the Holiday version of
Celebrate; if it refers to a Christmas
object, it invokes the Christmas version
18. 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.
19. 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.
20. 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() {}}
22. Widening and Narrowing
Assigning an object to an ancestor reference is
considered to be a widening conversion, and
can be performed by simple assignment
Holiday day = new Christmas();
Assigning an ancestor object to a reference can
also be done, but it is considered to be a
narrowing conversion and must be done with a
cast:
Christmas christ = new Christmas();
Holiday day = christ;
Christmas christ2 = (Christmas)day;
23. Widening and Narrowing
Widening conversions are most common.
Used in polymorphism.
Note: Do not be confused with the term widening
or narrowing and memory. Many books use short
to long as a widening conversion. A long just
happens to take-up more memory in this case.
More accurately, think in terms of sets:
The set of animals is greater than the set of parrots.
The set of whole numbers between 0-65535 (ushort)
is greater (wider) than those from 0-255 (byte).
24. Type Unification
Everything in C# inherits from object
Similar to Java except includes value types.
Value types are still light-weight and handled
specially by the CLI/CLR.
This provides a single base type for all
instances of all types.
Called Type Unification
25. The System.Object Class
All classes in C# are derived from the Object class
if a class is not explicitly defined to be the child of an existing class, it is a
direct descendant of the Object class
The Object class is therefore the ultimate root of all class
hierarchies.
The Object class defines methods that will be shared by all
objects in C#, e.g.,
ToString: converts an object to a string representation
Equals: checks if two objects are the same
GetType: returns the type of a type of object
A class can override a method defined in Object to have a
different behavior, e.g.,
String class overrides the Equals method to compare the content of two
strings