OOPs concepts in java
In this post, we will study about the OOPs concepts i.e. Objects oriented programming concepts. As per its name, first question comes to the mind is what is an object?
What is an Object?
OK, so you will also find objects around you. In real world scenario, We can say everything is an object like car, fan, pen etc.An object has 2 characteristic i.e. State and behavior and it can be physical and logical. Let’s understand with examples.
A car has a state (name, color,model etc.) and behavior (driving,applying break, horn etc). Same way pen has a state (amount of ink,lid with on and off feature) and behavior (can be used to write etc.). These are the physical entities.
If we talk about non-physical things or logical objects, you can consider your bank account. A bank account can not be touched physically but has a state (the amount of money in it) and behavior ( depositing or withdrawing money).
How does it relate to the programming model?
Object oriented programming is programming that involves intellectual objects that helps us to achieve or resolve business problems in real world.
In real world, you will find many objects of the same kind. There are many cars in the world, but each car is built from same set of blueprints and have same components.Class is a blueprint, from which individual objects are created.Consider a class name as CARS, Your car A is an instance of the class CARS , similar way your neighbor’s car B is also instance of the class CARS.
Object oriented programming has some fundamentals principles which help to simplify the software development and provides re-usability and maintainability.These are as follows:
Inheritance: When an object in a child class acquires properties from objects in the parent class known as Inheritance. It improves code re-usability. There can be different types of cars like sports cars, small cars, big cars etc., which acquire properties from their parent class CARS.
Polymorphism: If an object is capable to take many forms, is known as polymorphic. Consider a class called Shape. Shape can be anything like rectangular, circle, square etc. Polymorphism can be achieved by overloading and overriding of the methods. We will study about overriding and overloading in details in later posts.
Abstraction: Abstraction shows functionality and hides irrelevant details that would confuse us. As an example, watching TV. User is concerned about switch the TV On and Off to watch desired shows and not bothered about its internal logic how it is displaying.
Encapsulation: Encapsulation is use to control the access to data from outside class ,also known as data hiding.Data and code will be combined in single unit. It improves maintainability, flexibility and re-usability.
Difference between Object oriented programming and object based programming: