Introduction to Object-Oriented Programming in Java: Learn Classes, Objects, Inheritance, Polymorphism, and OOP Concepts

 Introduction to Object-Oriented Programming in Java: Learn Classes, Objects, Inheritance, Polymorphism, and OOP Concepts

1. What is Object-Oriented Programming (OOP)?

OOP is a programming paradigm based on the concept of "objects", which contain data (fields) and behavior (methods). Java is a fully object-oriented language that encourages clean and reusable code.



2. Key Concepts of OOP

OOP in Java is based on four main pillars:

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

These principles help in building applications that are modular, scalable, and easy to maintain.

3. Understanding Classes and Objects

  • A class is a blueprint or template for creating objects.

  • An object is an instance of a class.

Example: Basic Class and Object

public class Car { String color = "Red"; int speed = 100; void drive() { System.out.println("Driving at " + speed + " km/h"); } public static void main(String[] args) { Car myCar = new Car(); // Creating object System.out.println("Car Color: " + myCar.color); myCar.drive(); } }

Output:

Car Color: Red Driving at 100 km/h

4. Encapsulation in Java

Encapsulation means hiding internal details and only exposing essential parts of an object via public methods.

Example: Encapsulation Using Getters and Setters


public class Person { private String name; public void setName(String n) { name = n; } public String getName() { return name; } public static void main(String[] args) { Person p = new Person(); p.setName("Alice"); System.out.println("Name: " + p.getName()); } }


5. Inheritance in Java

Inheritance allows one class to inherit properties and methods of another class using the extends keyword.

Example: Inheritance

class Animal { void sound() { System.out.println("This is a generic sound"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } public class TestInheritance { public static void main(String[] args) { Dog d = new Dog(); d.sound(); // Inherited method d.bark(); // Dog's own method } }

6. Polymorphism in Java

Polymorphism allows the same method to behave differently depending on the context. It can be:

  • Compile-time (Method Overloading)

  • Runtime (Method Overriding)

Example: Method Overriding


class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape { void draw() { System.out.println("Drawing circle"); } } public class TestPolymorphism { public static void main(String[] args) { Shape s = new Circle(); // Upcasting s.draw(); // Calls overridden method } }

Output:


Drawing circle

7. Abstraction in Java

Abstraction hides complex implementation and shows only essential features. It is implemented using abstract classes or interfaces.

Example: Using Abstract Class

abstract class Animal { abstract void makeSound(); } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } } public class TestAbstraction { public static void main(String[] args) { Animal a = new Cat(); a.makeSound(); } }

Conclusion

Object-Oriented Programming in Java allows developers to build real-world, maintainable applications using principles like encapsulation, inheritance, polymorphism, and abstraction. Mastering OOP is crucial for professional Java development.


This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication

Previous Post Next Post