top of page

I'm OOP And I'm Surrounded Around You

  • Writer: Ajay Kedia
    Ajay Kedia
  • Jan 31, 2016
  • 5 min read

Programming itself is a difficult thing to do and learning to program is an ongoing process. No matter how much they read and put into practice, you will always be two steps behind the perfection. Lets Learn the Java programming in this blog the funny way...!!!


Classes

A Class is a plan which describes the object.We call it as a blue print of, how the object should be represented. Example#1- An architect will have the blueprints for a house.Those blueprints will be plans that explains exactly what properties the house will have and how they are all layed out.However it is just the blueprint, you can't live in it. Builders will look at the blueprints and use those blueprints to make a physical house.They can use the same blueprint to make as many houses as they want. Each house will have the same layout and properties and can accommodate it's own family. There are some OOPS principle that need to be looked in while creating any of the class. This principle is called as SOLID where each letter has some specification. SRP (The Single Responsibility Principle) A class should have one, and only one responsibility OCP (The Open Closed Principle) You should be able to extend a classes behavior, without modifying it. (Inheritance) LSP (The Liskov Substitution Principle) Derived classes must be substitutable for their base classes. (Polymorphism) ISP (The Interface Segregation Principle) Make fine chopped interface instead of huge interface as client cannot be forced to implement an interface which they don’t use. DIP (The Dependency Inversion Principle) Depend on abstractions, not on concretions. (Abstraction)


Object

Any real world entity which can have some characteristics or which can perform some work is called as Object.This object is also called as an instance i.e. - a copy of entity in programming language. Example#1- A mobile manufacturing company, at a time manufactures lacs of pieces of each model which are actually an instance. This objects are differentiated from each other via some identity or its characteristics. This characteristics is given some unique name.


1) Polymorphism

Polymorphism can be defined as the ability of doing the same operation but with different type of input. More precisely we say it as ‘many forms of single entity’.


Example#1- If a girl is married and is mother of 2 children and doing job in school as a teacher. Then her role will be different at different places. At home, She will be playing a role of mother for her childrens, as a wife for hubby, as a teacher in school. Example#2- A software engineer can perform different task at different instance of time depending on the task assigned to him . He/She can done coding , testing, analysis and designing depending on the task assigned and the requirement. Types 1. Compile time polymorphism / Static polymorphism Compile time (static) polymorphism occurs when a method is overloaded; i:e, when the argument used with the method is changed. The compiler knows which overloaded method it is going to call.Compiler checks the type and number of parameters passed to the method and decides which method to call at compile time. Example#1- Let’s say Samsung mobile have the 5MP camera available i.e. – it is having a functionality of CameraClick(). Now same mobile is having Panorama mode available in camera, so functionality would be same but with mode. See the example below:

  1. public class Samsumg : Mobile

  2. {

  3. public void GetWIFIConnection()

  4. {

  5. Console.WriteLine("WIFI connected");

  6. }

  7. //This is one method which shows camera functionality

  8. public void CameraClick()

  9. {

  10. Console.WriteLine("Camera clicked");

  11. }

  12. //This is one overloaded method which shows camera functionality as well

  13. //but with its camera's different mode(panaroma)

  14. public void CameraClick(string CameraMode)

  15. {

  16. Console.WriteLine("Camera clicked in " + CameraMode + " Mode");

  17. }

  18. }

Example#2- Suppose you wanted to multiply two numbers together:

  1. public int mulNum (int x, int y) {

  2. //Statements

  3. }

But what if you had other numbers to multiple, but they had “double” values instead of “int” values? You would just change the argument:

  1. public int mulNum (double x, double y) {

  2. //Statements

  3. }

2. Runtime Polymorphism / Dynamic Polymorphism Run time polymorphism occurs when the methods itself are changed.

Example#1 Suppose a base class "Mobile" has a SendMessage function which was intended to send message to single person at a time, but suppose Nokia had given provision for sending message to a group at once. i.e. - Overriding the functionality to send message to a group. This type is called Runtime polymorphism.

  1. public class Nokia Mobile

  2. {

  3. public void GetBlueToothConnection()

  4. {

  5. Console.WriteLine("Bluetooth connected");

  6. }

  7. //New implementation for this method which was available in Mobile Class

  8. //This is runtime polymorphism

  9. public override void SendMessage()

  10. {

  11. Console.WriteLine("Message Sent to a group");

  12. }

  13. }

2) Inheritance

Ability to extend the functionality from base entity in new entity belonging to same group.

Example#1 Taking the example of human beings, children inherits some of the features from their parents along with their own extra features. Example#2 consider the Windows operating system. Windows 98 had certain properties and methods that were used in Windows XP. Windows XP was derived from Windows 98, but it was still different from it.


3) Data Abstraction

Data abstraction refers to the process of only displaying relevant properties and methods to handle an object, while hiding the rest.Once a class has been declared as abstract, it cannot be instantiated, but can be used through through sub-class. You can create an abstract class using the abstract keyword. An abstract class may or may or may not contain abstract methods but,a class containing abstract methods must be declared abstract.


Example#1- You can operate your phone as long as you have a keypad and a screen. You don’t to know about its micro circuitry, antenna, software or other technology to operate it.


4) Encapsulation

Encapsulation is defined as the process of enclosing one or more details from outside world through access right. It says how much access should be given to particular details.


Example#1- Talking about Bluetooth which we usually have it in our mobile. When we switch on the Bluetooth I am able to connect another mobile but not able to access the other mobile features like dialing a number, accessing inbox etc. This is because, Bluetooth feature is given some level of abstraction. Example#2- Let’s create a basic class that deals with an employee and his tasks at the office:

  1. public class Employeedetails {

  2. private String employeeName;

  3. private String employeeDept;

  4. private int salary;

  5. private int hoursperday;


  6. public void work () {

  7. //Statements

  8. }


  9. public void train () {

  10. //Statements

  11. }

  12. }

I specified the name of the employee, his salary and hours he/she worked per day as private, which means only the members of the program can access this data.







Comments


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square

copyright© all right reserved.  created by ajaykedia.

  • w-facebook
  • White LinkedIn Icon
  • Twitter Clean
  • White Blogger Icon
bottom of page