OOPS Concepts – OOPS Concepts Example:
What is OOPS? To implement the real world things or
we can say real world objects in to programming world we required programming
techniques that is called Object Oriented Programming System. OOPS is used to
write programs based on the real world objects.
The states and behaviors of an objects are represented as
the member variables and methods. In oops programming programs are organized around
objects and data rather than actions and logic.
What are the advantages of OOPS concepts? Major
advantages of OOPS programming are;
- Simplicity:
OOPS programming objects model real world objects, so the complexity is
reduced and the program structure is clear.
- Modularity:
Each object forms a separate entity whose internal workings are decoupled
from other parts of the system.
- Modifiability:
It is easy to make minor changes in the data representation or the
procedures in an OO program. Changes inside a class do not affect any
other part of a program, since the only public interface that the external
world has to a class is through the use of methods.
- Extensibility:
Adding new features or responding to changing operating environments can
be solved by introducing a few new objects and modifying some existing
ones.
- Maintainability:
Objects can be maintained separately, making locating and fixing problems
easier.
- Reusability:
Objects can be reused in different programs.
What is the difference between Procedural programming and
OOPS? Procedural language is based on functions but object oriented
language is based on real world objects.
- Procedural
language gives importance on the sequence of function execution but object
oriented language gives importance on states and behaviors of the objects.
- Procedural
language exposes the data to the entire program but object oriented
language encapsulates the data.
- Procedural
language follows top down programming paradigm but object oriented
language follows bottom up programming paradigm.
- Procedural
language is complex in nature so it is difficult to modify, extend and
maintain but object oriented language is less complex in nature so it is
easier to modify, extend and maintain.
- Procedural
language provides less scope of code reuse but object oriented language
provides more scope of code reuse.
What are the core concepts of OOPS? OOPS core
concepts are;
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Composition
- Association
- Aggregation
What is Abstraction? Abstraction is the concept of
hiding the internal details and describing things in simple terms. For example,
a method that adds two integers. The internal processing of the method is
hidden from the outer world. There are many ways to achieve abstraction in
object-oriented programing’s, such as encapsulation and inheritance.
Abstraction main goal is to handle complexity by
hiding unnecessary details from the user. That enables the user to implement
more complex logic on top of the provided abstraction without understanding or
even thinking about all the hidden complexity. That’s a very generic concept
that’s not limited to object-oriented programming. You can find it everywhere
in the real world.
For Example Abstraction in the real world. I’m a
coffee addict. So, when I wake up in the morning, I go into my kitchen, switch
on the coffee machine and make coffee. Sounds familiar? Making coffee with a
coffee machine is a good example of abstraction.
You need to know how to use your coffee machine to make
coffee. You need to provide water and coffee beans, switch it on and select the
kind of coffee you want to get.
The thing you don’t need to know is how the coffee machine
is working internally to brew a fresh cup of delicious coffee. You don’t need
to know the ideal temperature of the water or the amount of ground coffee you
need to use.
Someone else worried about that and created a coffee machine
that now acts as an abstraction and hides all these details. You just interact
with a simple interface that doesn’t require any knowledge about the internal
implementation.
You can use the same concept in object-oriented programming
languages
What is Encapsulation? Encapsulation is the technique
used to implement abstraction in object-oriented programming. Encapsulation is
used for access restriction to class members and methods. Access modifier
keywords are used for encapsulation in object oriented programming.
For example encapsulation is achieved using private,
protected and public keywords.
What is Polymorphism? Polymorphism is the concept
where an object behaves differently in different situations. There are two
types of polymorphism – compile time polymorphism and runtime polymorphism.
Compile-time polymorphism is achieved by method overloading.
All the methods name is same and arguments are different. Here compiler will be
able to identify the method to invoke at compile-time, hence it’s called
compile-time polymorphism.
Runtime polymorphism is implemented when we have
“IS-A” relationship between objects. This is also called a method overriding
because subclass has to override the superclass method for runtime polymorphism.
If we are working in terms of the superclass, the actual implementation class
is decided at runtime. The compiler is not able to decide which class method
will be invoked. This decision is done at runtime, hence the name as runtime
polymorphism or dynamic method dispatch.
What is Static Binding and Dynamic Binding? Static or
early binding is resolved at compile time. Method overloading is an
example of static binding. Dynamic or late or virtual binding is resolved at
run time. Method overriding is an example of dynamic binding.
What is Inheritance? Inheritance is the object-oriented
programming concept where an object is based on another object. Inheritance is
the mechanism of code reuse. The object that is getting inherited
is called superclass and the object that inherits the superclass is
called subclass.
What is Association? Is the OOPS concept to define
the relationship between objects. The association defines the multiplicity
between objects. Association is a relationship between two objects
with multiplicity.
For example Teacher and Student objects. There is a one-to-many
relationship between a teacher and students. Similarly, a student can have
a one-to-many relationship with teacher objects. However, both student and
teacher objects are independent of each other.
What is Aggregation? Aggregation is a special type of
association. In aggregation, objects have their own life cycle but there is
ownership. Whenever we have “HAS-A” relationship between objects and ownership
then it’s a case of aggregation. Aggregation is also known as “HAS-A”
relationship.
For Example When class Car has a member reference
variable of type Wheel then the relationship between the classes Car and Wheel
is known as Aggregation. Aggregation can be understood as “whole to its parts”
relationship. Car is the whole and Wheel is part. Wheel can exist without the
Car. Aggregation is a weak association.
What is Composition? The composition is a special
case of aggregation. The composition is a more restrictive form of
aggregation. When the contained object in “HAS-A” relationship can’t exist on
its own, then it’s a case of composition. Composition is a special form of Aggregation
where the part cannot exist without the whole. Composition is a strong
Association.
For example House has-a Room. Here the room can’t
exist without the house and House cannot exists without room. Composition is
said to be better than inheritance.
What is the Diamond problem in inheritance? In case
of multiple inheritance, suppose class A has two subclasses B and C, and a
class D has two super classes B and C. If a method present in A is overridden
by both B and C but not by D then from which class D will inherit that method B
or C?
This problem is known as diamond problem.
What is multiple inheritance? A child class
inheriting states and behaviors from multiple parent classes is known as
multiple inheritance.
What is Dependency? When one class depends on another
because it uses that at some point in time then this relationship is known as
Dependency. One class depends on another if the independent class is a
parameter variable or local variable of a method of the dependent class. A
Dependency is drawn as a dotted line from the dependent class to the
independent class with an open arrowhead pointing to the independent class.
What is the difference between Association and
Dependency? The main difference between Association and Dependency is in
case of Association one class has an attribute or member variable of the other
class type but in case of Dependency a method takes an argument of the other
class type or a method has a local variable of the other class type.
What is a Class? A class is the specification or
template of an object. First of all, a class is a group of related methods and
variables. Everything in C# is built upon classes.
What is an Object? Object is instance of class. In
other words, an instance of a class is an object defined by that particular
class. Creating a new instance, or an object, is called instantiation. In
memory, you can create an object using the “new” keyword. Objects are reference
types. Objects are stored in the heap. An object is a section of memory
that has been configured according to the class blueprint.
What is Object attributes? Object attributes is the
data bundled in an instance of a class. The object attributes are called
instance variables or member fields. An instance variable is a variable defined
in a class, for which each object in the class has a separate copy.
No comments:
Post a Comment