How is a static method different from an instance method?
Static method has to be defined outside a class. It can be called without an object.
Instance method is defined within a class and has to be invoked on an object.
Explain Data Hiding with respect to OOP.
Data hiding can be defined as the mechanism of hiding the data of a class from the outside world or to be precise, from other classes. Data hiding is achieved by making the members of the class private. Access to private members is restricted and is only available to the member functions of the same class. However the public part of the object is accessible outside the class.
What is Object Oriented Programming? List some of its advantages.
OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. Advantages:
- Extensibility and Maintainability
Differentiate between an object and a class.
A class is a collection of objects of similar type.
For example, mango, apple and orange are members of the class fruit. Classes are user-defined data types and behave like the built-in types of a programming language.
The syntax used to create an object is not different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit.
Explain polymorphism with an example.
Polymorphism is the ability for a message or data to be processed in more than one form. An operation may exhibit different behaviors in different instances. For example, consider the operation of addition of two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation.
List three features that make an important charac-teristic of OOP.
- Capability to express closeness with the real- world models.
- Reusability-allows addition of new features to an existing one.
- Transitivity-changes in one class get automatically reflected across.
How do we implement abstract method in python? Give an example for the same.
Abstract Method can be used to enable parent class method execution.
Class Shape (object):
def findArea (self): pass
Class Square (Shape):
def init (self, side):
self, side = side def findArea self.side*self.side
List few disadvantages of OOP.
- Classes tend to be overly generalized.
- Relationship among classes might become artificial.
- Program design is tricky and complicated.
- More skills and thinking in terms of objects is required.
Explain Function overloading with an example.
When several function declarations are specified for a single function name in the same scope, the function is said to be overloaded. In other languages, the same function name can be used to define multiple functions with different number and type of arguments, def test(): #function 1 print “hello”
def test(a, b): #function 2
def test(a, b, c): #function 3
return a+b+c 2
What is inheritance? Explain with an example.
Inheritance is the capability of one class to acquire the properties or capabilities of another class. For example, the bird ‘Parrot’ is a part of the class ‘Flying Bird’ which is again a part of the class ‘Bird’.