2. Class
• In an OOP model, some of the objects exhibit identical characteristics
(information structure and behavior)
• We say that they belongs to same class.
• In object-oriented programming, a class is a blueprint for
creating objects (a particular data structure), providing initial values
for state (member variables or attributes), and implementations of
behavior (member functions or methods).
• The class is a blueprint that defines a nature of a future object.
3. Class
• An instance is a specific object created from a particular class. Classes
are used to create and manage new objects and
support inheritance—a key ingredient in object-oriented
programming and a mechanism of reusing code.
• A class is a way of organizing information about a type of data
so a programmer can reuse elements when making multiple
instances of that data type—for example, if a programmer
wanted to make three instances of Car, maybe a BMW, a
Ferrari, and a Ford instance.
• The Car class would allow the programmer to store similar
information that is unique to each car (they are different
models, and maybe different colors, etc.) and associate the
appropriate information with each car.
4.
5.
6. Inheritance
• The capability of a class to derive properties and characteristics from
another class is called Inheritance.
• Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class:
• The class that inherits properties from another class is called Sub class or
Derived Class.
• Super Class:
• The class whose properties are inherited by sub class is called Base Class or
Super class
7. Why and when to use inheritance?
• 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 as shown in figure
(next slide)
8. Inheritance : Example
You can clearly see that above process results in duplication of same
code 3 times.
9. Inheritance
• This increases the chances of error and data
redundancy.
• To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three
functions in it and inherit the rest of the classes from
the vehicle class, then we can simply avoid the
duplication of data and increase re-usability.
10. Inheritance
• Look at the below diagram in which the three classes are inherited
from vehicle class: