OOP Theory With Real World Examples
OOP Theory With Real World Examples
Real World Example: Every time you log into your email account( GMail, Yahoo,
Hotmail or official mail), you have a whole lot of processes taking place in the backend,
that you have no control over. So your password, would probably be retrieved in an
encyrpted form, verified and only then you are given access. You do not have any
control, over how the password is verified, and this keeps it safe from misuse.
Abstraction is a process of hiding the implementation from the user, only the
functionality is exposed here. So you are aware only of what the application does, not
how it does it.
Real World Example: When you log into your email, compose and send a mail. Again
there is a whole lot of background processing involved, verifying the recipient, sending
request to the email server, sending your email. Here you are only interested in
composing and clicking on the send button. What really happens when you click on the
send button, is hidden from you.
For example, TV remote is an object. Users are provided with only interface which is
nothing but keys. Users only know which key to press for what function. User will not
know what happens inside when we press the key. Same is the case in Java. Abstraction
is achieved with the help of interface. Interface expose only methods to end users.
Sometimes When we use a third party library we are been given only the interface to
access methods in it. You don’t know the implementation. For example, take Apache poi
library, it gives methods to create excel sheet, but we don’t know how internally it is
implemented. So the implementation is hidden. This is called abstraction.
Real-time Example 1:
School bag is one of the most real examples of Encapsulation. School bag can
keep our books, pens, etc.
Real-time Example 2:
When you log into your email accounts such as Gmail, Yahoo mail, or Rediff
mail, there is a lot of internal processes taking place in the backend and you
have no control over it.
When you enter the password for logging, they are retrieved in an encrypted form
and verified and then you are given the access to your account. You do not have
control over it that how the password has been verified. Thus, it keeps our
account safe from being misused.
Real-time Example 3:
Suppose you have an account in the bank. If your balance variable is declared as
a public variable in the bank software, your account balance will be known as
public, In this case, anyone can know your account balance. So, would you like
it? Obviously No.
So, they declare balance variable as private for making your account safe, so
that anyone cannot see your account balance. The person who has to see his
account balance, he will have to access private members only through methods
defined inside that class and this method will ask your account holder name or
user Id, and password for authentication.
Thus, We can achieve security by utilizing the concept of data hiding. This is
called Encapsulation.
1. The encapsulated code is more flexible and easy to change with new
requirements.
2. It prevents the other classes to access the private fields.
4. It keeps the data and codes safe from external inheritance. Thus,
Encapsulation helps to achieve security.
A constructor is a special method that is used to initialize a newly created object and is called
just after the memory is allocated for the object.
i.e Consider human as a class. The birth of a child is like initialising an object for that
class.
There is no kid in this world without a name. So naming ceremony is the functionality
inside a constructor, which will be done at the time of initialisation. Hence every kid gets
a name, necessarily!
Overloading Methods. A major topic in OOP is overloading methods, which lets you
define the same method multiple times so that you can call them with different argument
lists (a method's argument list is called its signature)
Assume, you are supposed just perform the function of talking. Say, you have to tell the
story of your day, to a total stranger. Your function will get over pretty quickly. Say, now
you are telling the same to your beloved. You will go through more details as compared
to the previous one. What has happened here, is, you have performed the same
function, but based on the parameter, stranger/beloved, your way of implementing the
function changed!
1. class You
2. {
3. void talk(Stranger obj)
4. {
5. sysout("Hi, my day was great!");
6. }
7. void talk(Beloved obj)
8. {
9. sysout("Hi, my day was great! You won't believe what happened
today! Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah!Blah! ");
10. }
11. }
Overriding: Happens in the inheritance hierarchy!
We learn a lot from our parents! We learn to cook to some extent. The same delicacy
with same ingredients is prepared by your mother with a different taste and you with a
different taste (assuming). That is overriding, same function (cooking), same parameters
(ingredients), but different algorithms (cooking style).
Or, your learnt driving from you dad! But you both drive the same vehicle differently!
That is overriding.
1. class Father
2. {
3. void drive()
4. {
5. //drive cautiously :-)
6. }
7. }
8. class You
9. {
10. void drive()
11. {
12. //drive rash! :P
13. }
14. }
Programming perspective:
WHAT IS INHERITANCE?
Inheritance is one of the most important pillars of Object Oriented Programming.
Inheritance allows a class to access all the property of the Base class and also Base
class can have its own behaviour. Using Inheritance a class can be act as a template
for other classes. A class that allows inheritance is called as Base Class and the class
that inherits other class is called as derived class.
IS-A Relationship.
HAS-A Relationship
Inheritance is used when your classes have IS-A Relationship. Such as Triangle IS-
A Shape. A Rectangle is a Shape. Suppose I have a class that calculates the area of
the shape then the relationship between the Shape and ShapeArea will be HAS-A.
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
The virtual keyword is used to modify a method, property, indexer, or event declaration and
allow for it to be overridden in a derived class. For example, this method can be overridden
by any class that inherits it:
When a virtual method is invoked, the run-time type of the object is checked for an
overriding member. The overriding member in the most derived class is called, which might
be the original member, if no derived class has overridden the member.
You cannot use the virtual modifier with the static, abstract, private, or override modifiers.
Singleton Pattern is one of the best-known patterns in software engineering.
Essentially, a singleton is a class which only allows a single instance of itself to be
created, and usually gives simple access to that instance. Most commonly, singletons
don't allow any parameters to be specified when creating the instance - as otherwise a
second request for an instance but with a different parameter could be problematic!
Static method in C# is a method that keeps only one copy of the method at the Type level, not the
object level. That means, all instances of the class share the same copy of the method and its data. The
last updated value of the method is shared among all objects of that Type.
3. Any main() method is shared through the entire class scope so it always appears with static
keyword.
Partial Classes in C#
A partial class is a special feature of C#. It provides a special ability to implement the
functionality of a single class into multiple files and all these files are combined into a
single class file when the application is compiled. A partial class is created by using
a partial keyword. This keyword is also useful to split the functionality of methods,
interfaces, or structure into multiple files.
Syntax :
public partial Clas_name
{
// code
}
Important points:
When you want to chop the functionality of the class, method, interface, or
structure into multiple files, then you should use partial keyword and all the files are
mandatory to available at compile time for creating final file.
The partial modifier can only present instantly before the keywords like struct,
class, and interface.
Every part of the partial class definition should be in the same assembly
and namespace, but you can use different source file name.
Every part of the partial class definition should have the same accessibility like
private, protected, etc.
If any part of the partial class is declared as an abstract, sealed, or base, then the
whole class is declared of the same type.
The user is also allowed to use nested partial types.
Dissimilar part may have dissimilar base types, but the final type must inherit all
the base types.
Example: Here, we are taking a class named as Geeks and split the definition of Geeks
class into two different files named as Geeks1.cs, and Geeks2.cs as shown below:
In Geeks1.cs, and Geeks2.cs, a partial class is created using the partial keyword and
each file contains different functionality of Geeks class as shown below.
Geeks1.cs
filter_none
brightness_4
public partial class Geeks {
private string Author_name;
private int Total_articles;
Partial Methods in C#
C# contains a special method is known as a partial method, which contains declaration
part in one partial class and definition part in another partial class or may contain both
declaration and definition in the same partial class.
Basically, partial methods exist in the partial class, or in the struct. A partial method may
or may not contain implementation if a partial method doesn’t contain an implementation
in any part then the compiler will not create that method in the final class or driver class.
A partial method is declared with the help of the partial keyword as shown below.
Syntax:
partial void method_name
{
// Code
}
Important Points:
The declaration of the partial method must begin with partial modifier.
The partial method may contain ref.
The partial method does not contain out parameters.
It is implicitly private method.
It can be a static method.
Partial method is generic.
It can have only void return type.
A partial method is created only in partial class or in partial struct.
Example: We have a class named as Circle. The functionality of the Circle class is
chopped into two different files named as circle1.cs and circle2.cs.
These circle1.cs and circle2.cs files contain the partial class of the Circle class and
partial method, i.e area. The circle1.cs file contains the declaration of the partial area()
method and circle2.cs file contains the implementation of the area method as shown
below:
circle1.cs
filter_none
brightness_4
public partial class Circle {
circle2.cs
filter_none
brightness_4
public partial class Circle {
area(int a);
// partial method
int A = 3.14 * r * r;
When we execute the above code, then compiler combines circle1.cs and circle2.cs into
a single file, i.e. circle as shown below.
circle
filter_none
brightness_4
public class Circle {
{
area(int a);
int A = 3.14 * r * r;
Composition
Inheritance is a hierarchy of relationships between objects. For example, car is a vehicle. So,
class Vehicle
//.....
//......
Where as, Composition denotes "is-a-part-of" relationship between objects. For example,
class Engine
//....
}
class Car
//.....
Composition and inheritance are two concepts which has to be applied at appropriate scenarios.