Introduction to All the OOPS Concepts in Java With Examples
Object-oriented programming is essential in modern software development, especially in Java, a widely used language. OOP focuses on objects that store data (attributes) and actions (methods). This approach moves away from traditional programming to create reusable parts. Understanding the oops concept in Java is important for writing clear and efficient code. These concepts also help structure applications, reuse code, and make programs more flexible. So, this article is here with simple examples to explore these principles, showing how they improve software design and development in Java.?
What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) organizes software design around objects and data, rather than actions and logic. It uses classes to define objects with their properties (attributes) and behaviors (methods). OOP makes code modular, reusable, and scalable by grouping data and functions into objects that interact with each other. Languages such as C++, Python as well as Java support OOP. Which is making it popular for creating complex and maintainable software.
Key OOPS Concept in Java
To provide a comprehensive overview of Java object oriented programming concepts, we'll cover the fundamental principles and illustrate each with examples:
1. Class and Object
? // Example of a Class
???public class Car {
???????// Attributes
???????String brand;
???????int year;
???????
???????// Methods
???????void displayInfo() {
???????????System.out.println("Brand: " + brand + ", Year: " + year);
???????}
???}
???
???// Creating Objects
???Car car1 = new Car();
???car1.brand = "Toyota";
???car1.year = 2020;
???car1.displayInfo(); // Output: Brand: Toyota, Year: 2024
2. Encapsulation
Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single unit (class). This oops concept in Java helps in hiding the internal state of an object and restricting direct access to it from outside the class.
? // Example of Encapsulation
???public class BankAccount {
???????private double balance;
???????
???????public void deposit(double amount) {
???????????balance += amount;
???????}
???????
???????public void withdraw(double amount) {
???????????if (amount <= balance) {
???????????????balance -= amount;
???????????} else {
???????????????System.out.println("Insufficient funds!");
???????????}
???????}
???????
???????public double getBalance() {
???????????return balance;
???????}
???}
3. Inheritance
Inheritance allows one class (subclass or derived class) to inherit the properties and behaviors of another class (superclass or base class). This OOPS concept in Java promotes code reusability and helps in implementing the "is-a" relationship.
? // Example of Inheritance
???public class Animal {
???????void eat() {
???????????System.out.println("Animal is eating");
???????}
???}
???
???public class Dog extends Animal {
???????void bark() {
???????????System.out.println("Dog is barking");
???????}
???}
4. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. This OOP Java concept can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
? // Example of Polymorphism (Method Overriding)
???class Animal {
???????void makeSound() {
???????????System.out.println("Animal makes a sound");
???????}
???}
???
???class Dog extends Animal {
???????@Override
???????void makeSound() {
???????????System.out.println("Dog barks");
???????}
???}
???
???class Cat extends Animal {
???????@Override
???????void makeSound() {
???????????System.out.println("Cat meows");
???????}
???}
5. Abstraction
Abstraction refers to the concept of hiding complex implementation details and showing only the essential features of the object. In the oops concept in Java, abstraction can be achieved using abstract classes and interfaces.
? // Example of Abstraction (Abstract Class)
???abstract class Shape {
???????abstract void draw();
???}
???
???class Circle extends Shape {
???????@Override
???????void draw() {
???????????System.out.println("Drawing Circle");
???????}
???}
???
???class Rectangle extends Shape {
???????@Override
???????void draw() {
???????????System.out.println("Drawing Rectangle");
???????}
???}
Moreover, if you want to learn object oriented programming Java then a full-stack Java full stack certification course will help you. Because it will provide you with a deep understanding of the concepts of Java programming. It will also make you future-ready for starting a career.
Conclusion
In conclusion, the oops concept in Java provides a solid framework for creating scalable and easy-to-maintain software. By focusing on objects and how they work together. Understanding concepts like classes, objects, encapsulation, inheritance, polymorphism, and abstraction helps developers. Mastering these principles enables you to build complex applications that are straightforward to modify and extend. As well as Java's strong OOP support makes it perfect for everything from small projects to large-scale systems.