From this section on-wards we will be studying about the concepts of OOPs in java and their implementation.
Since Java is all about classes and object, this section will help you grasp all concepts but you should have properly studied the previous basic Java section to make it easy to understand.
So geeks lets study some study concept about classes and object. But before that we should know what Object Oriented Programming is.
OOPs In Java (Object Oriented Programming System) : The Discussion
The basic idea behind using OOPs in java or any other language is because of its unique ability to provide interactivity among objects.
Object-oriented programming (OOP) is a logical model based upon “Objects”. Objects are considered as physical real world entity.
Also, these objects are directly responsible for storing data, facts and figures in form of attributes(fields) and methods(code).
OOPs in java can be considered as models real world well hence makes easy to design a real world problem based on class and objects.
OOPs in java provide some concepts which help in simplifying software development. These concepts are somehow logical and can be implemented easily which results in better function-ability of the software. These concepts are :
OOPs In Java : Concepts
Okay! Now what you think is a class and what might be an object? Any guesses? We shall see this below.
OOPs In Java : Object
Any physical entity around you is an object. For example : pen, mobile phone , table, chair, you yourself etc. are objects.
So you can extract some attributes from the objects around you and they are their state, behavior and identity.
Hence Object can be defined as a real world entity that has identity, state and behavior.
Let’s take an example :
BMW 5 series is a car. Its identity is BMW 5 series, state is its black color and behavior is that it runs on road.
Now collection of these cars have same state and behavior and belong to the same class called BMW. Hence BMW is a class and BMW 5 series is an object of BMW class.
Note:Objects are physical entity.
OOPs In Java : Class
Seeing above example we can say that class is a group of similar type of objects.
Class is a user defined data type too. Class is a blueprint which contains fields and methods that define state and behaviour of objects.
Note : Class is logical.
OOPs In Java : Inheritance
When one class called child class derive the properties of another class called parent class , then this is called inheritance.
Inheritance provides code re usability, helps in extending the properties of a class and helps in achieving run-time polymorphism.
For example : Children inherit from parents, their properties and behavior.
OOPs In Java : Polymorphism
Polymorphism is defined as the ability of a message to take more than one from. ‘Polymorphism’ is a Greek word meaning ‘poly’ means many and ‘morphs’ means form hence called ‘many forms’.
In Java we can achieve polymorphism either by method overloading (Compile time Polymorphism) or by method overriding (Run-time Polymorphism).
For example : Animals can create many sound like howl, meow, growl, grrrr etc. Hence sound have many forms.
OOPs In Java : Data Abstraction
Data abstraction refers to the way of showing only necessary information to the use and hiding any complex implementation details.
Abstraction in Java is achieved by interface and abstract class. We will discuss these later.
For example : When driving a car driver just turn steering, changes gear but driver don’t know about how steering is working inside, how gears are shifting. These details have been hidden from the driver and only important details like steering, gear lever, brake etc. are shown.
OOPs In Java : Encapsulation
Wrapping up of fields, objects , and method within a single unit called class is known as encapsulation.
In Java encapsulation is achieved by access modifiers eg. private, protected etc.
For example : The components of your smartphone are wrapped (encapsuled) up into single unit and is presented as a whole phone to the user.