In this page, we will learn about the basics of OOPs. As the name suggests, Object-Oriented Programming Or OOPs refer to programming languages that use objects in development. Object is key source to implements what is to happen in application development.
Object means a real-word entity i.e car, table, employee, pen etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:
- Object
- Class
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Object:
It is a basic unit of Object-Oriented Programming.Any entity that has state and behaviour is known as an object. So Object has:
State: A state is represented by its attributes and properties.
Behaviour: A behaviour is represented by the methods of an object.
Identity: It’s gives a unique name to an object and enables one object to interact with another object.
Example of an object: Car
- Identity: Name of the car
- State: Color, Brand , variant
- Behaviour: Drive, Horn, Break, AC
Class:
Class is not real-work entity, its logical entity. Collection of objects is called class. A class can also be defined as blueprint from which you can create an individual object. Class contains data member, method, constructor, nestled class and interface.
Syntax to declare a class:
access_modifier class_name { data member; method; constructor; nested class; interface; }
A class is blueprint of or prototype from which object are created. It represent the set of properties or method that are common to all object of one type.
- Modifiers: A class can be public or has default access
- Class keyword: class keyword is used to create a class.
- Class name: The name should begin with an initial letter
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
- Body: The class body is surrounded by braces, { }.
Abstractions :
Abstraction is a process of hiding unnecessary data and showing only relevant data. Out of an ocean of data, we are only maintaining the transparency of some data to the user. This important concept in object-oriented programming will reduce the complexity of the code and increases the readability.
Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects
Encapsulation:
Encapsulation is data hiding(information hiding) while Abstraction is detailed hiding(implementation hiding).
Encapsulation is binding the data members with member variables. This will avoid the direct access of variables, because direct access of variables may violate privacy, and hiding of the implementation will not be possible. While encapsulation groups together data and methods that act upon the data, data abstraction deal with exposing the interface to the user and hiding the details of implementation
In summary encapsulation is a procedure that takes place at the implementation level, while abstraction is a design-level process
Inheritance:
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs. In Java, inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.
Importance of Java inheritance
- Code Reusability: Inheritance minimizes the complexity of a code by minimizing duplicate code. If the same code has to be used by another class, it can simply be inherited from that class to its sub-class. Hence, the code is better organized
- Polymorphism: Method Overriding is achievable only through Inheritance. It is one of the ways by which java achieves Run Time Polymorphism.
- Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user.
Polymorphism:
Polymorphism is the ability of an object to take on different forms. n Java, polymorphism refers to the ability of a class to provide different implementations of a method, depending on the type of object that is passed to the method.
Polymorphism in Java is the task that performs a single action in different ways
Real-Life Examples of Polymorphism
A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.
In Java polymorphism is mainly divided into two types:
- Compile-time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
Compile-time polymorphism : Compile-time polymorphism is also know as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading. Method Overloading When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.
Runtime Polymorphism: It is also known as Dynamic Method Dispatch. Method overriding is the process when the subclass or a child class has the same method as declared in the parent class
Excellent explanation. A must read for all beginner developers who are starting out to learn about Oops.
ReplyDelete