Hello everyone, in the previous topic we were talking about loops, switch statements, and if-else statements in Java. Today we are going to talk about the concept of object-oriented programming language in Java(OOPS) and we are also going to discuss class and objects.
Contents
Concept of Object-Oriented Programming in Java
Concept of Object-Oriented Programming in Java
Object-oriented programming (OOP) is a high-level computer programming language that implements objects and their associated procedures within the programming context to create software programs. The object-oriented language uses an object-oriented programming technique that binds related data and functions into an object and encourages the reuse of these objects within the same and other programs.
Object-Oriented Programming (OOP) Concept in Java
Java is a class-based object-oriented programming (OOP) language that is built around the concept of objects. OOP concepts intend to improve code readability and reusability by defining how to structure a Java program efficiently.
Object-oriented programming brings together data and its behavior(methods) in a single location(object) makes it easier to understand how a program works.
Simula is considered the first object-oriented programming language. The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language. Smalltalk is considered the first truly object-oriented programming language.
The popular object-oriented languages are Java, PHP, Python, C++, etc. The main aim of object-oriented programming is to implement real-world entities, for example, objects, classes, abstraction, inheritance, polymorphism, etc.
Core OOPS concepts
The core oops concepts are discussed below-
Class
The class is a group of similar entities. It is only a logical component and not a physical entity.
For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties (data) can be the price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking, etc.
Object
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code.
Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism
If one task is performed in different ways, it is known as polymorphism. In Java, we use method overloading and method overriding to achieve polymorphism.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing. In Java, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule is wrapped with different medicines. A java class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
Advantages of OOPS
Following are the advantages of the OOPS–
- OOPS is easy to understand and has a clear modular structure for programs.
- Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development cost
- Large programs are difficult to write, but if the development and designing team follow the OOPS concept then they can better design with minimum flaws.
- It also enhances program modularity because every object exists independently.
Class
A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instructions to build a specific type of object.
Syntax of class- class <class_name>{ field; method; } |
Example of class public class (blue se) Main(red se highlight karna) { int x= 5; } |
Object
An object is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful.
Object determines the behavior of the class.
When you send a message to an object, you are asking the object to invoke or execute one of its methods.
Syntax of object ClassName ReferenceVariable = new ClassName(); |
Note– here we use the “new” keyword in Java. The new keyword is used to allocate memory at runtime. All objects get memory in the Heap memory area.
Example of object- public class Main { int x= 5; public static void main (String[] args) { main myobj= new main(); System.out.println (myobj.x); } } |
Anonymous Objects in Java
Anonymous objects are initialized but never stored in a reference variable. These types of objects are used for immediate calling and destroyed after use.
An object which has no reference is known as an anonymous object. It can be used at the time of object creation only you have to use an object only once.
Example of Anonymous Object class test { void fact(int n){ int fact=1; for(int i= 1;i<=n;i++){ fact=fact*i; } System.out.println(“factorial is “+fact); } public static void main(String args []) { new test().fact(5);//calling method with anonymous object } } |
Difference between object and class
- The object is an instance of a class whereas the Class is a blueprint or template from which objects are created.
- The object is a real-world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair, etc. whereas Class is a group of similar objects.
- The object is a physical entity whereas the Class is a logical entity.
- The object is created through a new keyword mainly e.g. Student s1=new Student(); whereas e.g. Class is declared using class keyword e.g. class Student {}
- The object is created many times as per requirement whereas the Class is declared once.
- The object allocates memory when it is created whereas Class doesn’t allocate memory when it is created.
- There are many ways to create an object in java such as new keywords, new instance() method, clone() method, factory method, and deserialization whereas there is only one way to define a class in java using the class keyword.
So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.
Thank You,
Regards
Grooming Urban
General FAQ
What is object-oriented programming (OOP) in Java?
Object-oriented programming (OOP) is a high-level computer programming language that implements objects and their associated procedures within the programming context to create software programs. The object-oriented language uses an object-oriented programming technique that binds related data and functions into an object and encourages the reuse of these objects within the same and other programs.
What are core oops concepts?
The core oops concepts are discussed below-
1. Class
2. Object
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
What is a class in core oops concept in Java?
The class is a group of similar entities. It is only a logical component and not a physical entity.
What is an object in the core oops concept in Java?
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code.
What is inheritance in the core oops concept in Java?
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
What is polymorphism in the core oops concept in Java?
If one task is performed in different ways, it is known as polymorphism. In Java, we use method overloading and method overriding to achieve polymorphism.
What is an abstraction in the core oops concept in Java?
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing. In Java, we use abstract class and interface to achieve abstraction.
What is encapsulation in the core oops concept in Java?
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule is wrapped with different medicines. A java class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
What are anonymous Objects in Java?
Anonymous objects are initialized but never stored in a reference variable. These types of objects are used for immediate calling and destroyed after use. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only you have to use an object only once.
1 thought on “Concept of Object-Oriented Programming in Java”