Java OOPS concept
what is OOPS?
Object-oriented programming System (OOPS) is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.(Wikipedia).
” Note: Be clear with one thing that java is not purely Object-oriented programming because it supports Primitive datatype such as int, byte, long… etc, which are not objects.
Small-talk is a pure OOP language, where there are no primitive types. Everything boolean, int and methods are represented as objects. “
Object Oriented Programming System in Java
Object and Class
Object means a real word entity such as laptop, chair, room, students, Animal etc. Object-Oriented Programming is a methodology of designing a program using classes and objects.
The concept of oops simplifies software development and maintenance by providing some concepts:
- Classes and Objects
1. Classes and Objects
Objects: Any entity that has state and behavior is known as an object. For example: laptop, pen, students, keyboard, bike etc. It can be physical and logical. Every object is having state and behavior.
- State: is defined by its attributes. An object’s attributes are usually static, and the values of the attributes are usually dynamic.
- Behavior: refers to how objects interact with each other, and it is defined by the operations an object can perform.
For examples if we take a dog as an object, then its state is – name, breed, color, and the behavior is – barking, running.
Class: A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. we can also say that a class is a collection of objects. Suppose animal is a class then Dog, bear, Lion are the objects.
Polymorphism in java is a concept by which we can perform a single action by many different ways. So polymorphism means many forms. It plays an important role in allowing objects having different internal structures to share the same external interface.
There are two types of polymorphism in java: compile time polymorphism and run-time polymorphism.
- Method overloading is the example of Compile time polymorphism also known as static binding.
- Method Overriding is the example of Run-time polymorphism also known as dynamic binding.
Inheritance allows a class to use methods and properties of other class(parent class). In other words an object acquires the properties and behavior of the parent object.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
The idea behind implementation of the inheritance is to create a new class that is built upon the existing classes. After inhering the parent or base class you can reuse the properties of parent class and also add some new properties to it.
So the use of inheritance in java is:
- For code re-usability
- For method overriding (to archive run-time polymorphism ).
Encapsulation in java is the concept of wrapping up the data(variables) and the code(methods) together in a single unit,
Basically you can say that wrapping of data and code like a capsule.
In other words, Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class.
The term encapsulation is often used interchangeably with information hiding. However, there is a thin difference between the two. Encapsulation is a language feature whereas information hiding is a widely considered as a design principle.
Abstraction is process of hiding the implementation details and showing only the functionality. In other words we can say that, it deals with the outside view of an object (interface and abstract classes).
Abstraction in java is achieved by using interface and abstract class.
Interface gives complete abstraction and abstract class gives 0-100% abstraction. A class that is declared as abstract is known as abstract class.
Hope You Like the presentation and explanation. Like and Subscribe for more article like this….