Introduction
to OOP
ADVANTAGES OF OOP
Faster and easier to execute
Provides a clear structure for the programs
Makes the code easier to maintain, modify and debug
Makes it possible to create reusable applications with less code and
shorter development time.
OOPS CONCEPTS
Class
Objects
Inheritance
Abstraction
Encapsulation
Polymorphism
INHERITANCE
Classes divided into subclasses.
The Animals can be divided into mammals, amphibians, insects, birds, etc.
The vehicles can be divided into cars, trucks, buses, motorcycles, etc.
A new class of object derives properties and characteristics from another
class.
It inherits properties from other classes.
No need to write all the properties and functions again and again, as these
can be inherited from another class that possesses it.
Allows the user to reuse the code whenever possible and reduce its
redundancy
INHERITANCE
The capability of a class to derive properties and characteristics from
another.
Inheritance is one of the most important feature of Object-Oriented
Programming.
Inheritance is also called an is-a hierarchy.
Inheritance is the key for code reuse.
–If a base class has been created, then a derived class can be
created to add more information
EXAMPLE : INHERITANCE
EXAMPLE
Consider a group of vehicles.
You need to create classes for Bus, Car and Truck.
The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the
three classes.
If we create these classes avoiding inheritance then we have to write all of these
functions in each of the three classes.
INHERITANCE - TERMINOLOGY
Sub Class:
–The class that inherits properties from another class is called Sub
class or Derived Class or Child Class.
Super Class:
–The class whose properties are inherited by sub class is called
Super Class or Base Class or Parent Class.
IMPLEMENTING INHERITANCE IN C#
To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the fields and methods from the Vehicle class
(parent)
OUTPUT ?
OUTPUT ?
OUTPUT ?
MULTILEVEL INHERITANCE
A class can also be derived
from one class, which is
already derived from another
class.
In the following example,
MyGrandChild is derived from
class MyChild (which is derived
from MyClass).
MULTIPLE INHERITANCE
A class can also be derived from
more than one base class, using a
comma-separated list:
WHY USE INHERITANCE?
Code Reusability: Inheritance allows you to reuse code that is already
written in the base class. This reduces redundancy and makes your
code easier to maintain.
Hierarchical Relationships: It models real-world relationships naturally.
Extensibility: You can extend the functionality of an existing class
without modifying it.
SEE YOU ON
NEXT WEEK