• Question no-01: What is Object? Explain Object Oriented Concept with Examples.

Answer:

According to Wikipedia:

In the class-based object-oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.

According to Cambridge Dictionary:

An Object is  a thing that you can see or touch but that is not usually a living animalplant, or person.

Simply:

An Object is a Software bundle of variables and related methods that must have two properties: i) State and ii) Behavior

If we are talking about a real life example of an object then we can talk about a bicycle:

A bicycle has i) State: current gear, two wheals, no of gear etc.

And   ii) Behavior: Braking, Accelerating, breaking gears

So, we can say that a bicycle is an Object.

Python OOP Car Class and Object

From the above figure we can easily understand that what an Object is.

An Object can be different in size ,color, shape but it always made up according to as it’s Class where all the Characteristics of that Object has been Implemented/described. We can make thousands of cars from our main car class. So all the new cars are the objects of our main car class.

~ Generic attributes/properties/variables of a car: brand, speed, height, weight, colour etc.
~ Generic methods/functions/behaviors of a car: activateHorn, moveForward, moveBack, turnRight, turnLeft etc.

Note that; for different objects, the values of attributes and methods can varies or be similar but behave differently.

Examples of Real-World Objects that could be modeled into computer with OOP coding style:

 

1) ATM
2) Bank Account
3) Fruit
4) Family
5) Cars
6) Shapes
7) Animal
8) Employee
9) User Registration Form
10) Hero
11) Football Club
12) Programmers Gallery etc.

Object Oriented Concept:

Object-Oriented Programming(OOP) refers to a programming methodology based on objects, instead of just functions and procedures.

These objects are organized into classes, which allow individual objects to be group together.

Object oriented programming makes it easier for programmers to structure and organize software programs. Because individual objects can be modified without affecting other aspects of the program, it is also easier to update and change programs written in object oriented languages. As software programs have grown larger over the years, OOP has made developing these large programs more manageable.

The four major principles of object orientation are:

  • Encapsulation

  • Data Abstraction

  • Polymorphism

  • Inheritance

For better Understanding this OOP Concept we need to discuss an Example regarding this.

Example: We have a class defined for vehicles. Create two new vehicles called car1 and car2. Set car1 to be a red convertible worth $60,000.00 with a name of Fer, and car2 to be a blue van named Jump worth $10,000.00.

Implemented with Python Programming Language:

01
0203

In this carproblem.py script we can see that we have defined a class called Vehicle and we have 4 objects (just initiated) and a function within this. The desc_str function will return the object values. Outside this Vehicle class we have given the values of that initiated objects. And then we printed the values of cars that we have given before. Thus, we can make thousands of cars from vehicle class.

So, our Output is exactly what we actually wanted. Our output will be changed every time when we will change the values of that objects

Inheritance: Classes can inherit other classes. A class can inherit attributes and behavior (methods) from other classes, called super-classes. A class which inherits from super-classes is called a Sub-class. Super-classes are sometimes called ancestors as well. There exists a hierarchy relationship between classes.

Question no-02: Describe Encapsulation.

Answer:

Encapsulation:

Encapsulation is the mechanism for restricting the access to some of an object’s components, this means that the internal representation of an object can’t be seen from outside of the objects definition. Access to this data is typically only achieved through special methods: Getters and Setters.

In an object-oriented python program, you can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as encapsulation.

oop new

In this Encapsulation Example we can see there are three modifiers of encapsulation: i) public ii) protected iii) privet

The basic differences between them:

Name

Notation

Behaviour

name

Public

Can be accessed from inside and outside

_name

Protected

Like a public member, but they shouldn’t be directly accessed from outside.

__name

Private

Can’t be seen and accessed from outside

In the above example we see how the encapsulation actually works.

Question no-03: The Differences between Overloading and Overriding.

Answer:

Overloading: Same Method name and different parameter within a class is called Overloading.

Overriding: Same Method name and same parameter within a class is called Overloading.

If we can show an example then the concept will be more cleared.

Overloading is when same method name is used for 2 or more methods in the same class.
Here is a simple example:

public class Sum {
public void add(int a, int b) {
return a+b;
}
public void add(int a, int b, int c) {
return a+b+c;
}
}

For method overloading, the method signature number of arguments, type of arguments and order of arguments  should be different. Return type is not a part of the signature.

Overriding is when same function name from a base class can be used in a derived class.
For example: consider the class:

public class Shape {
public void draw() {
// Do something
}
}

Consider class Circle which extends Shape:

public class Circle extends Shape {
public void draw() {
// Do something different than class Shape
}
}

In class Circle, if draw() method is not created, then implementation from class Shape will be used when we call draw() on a derived class object.
But in above code, method draw() in class Circle overrides implementation of class Shape.
This is function overriding.

To eliminate this confliction problem in overriding,  A common solution for this in languages like Java is to use double dispatch and the visitor pattern.

Personally, I’m not a huge fan of this. I prefer to try to design my way around it. That’s not always possible, of course, but it’s worth at least attempting.