Object-Oriented Programming (OOP) in Java

Object-Oriented Programming (OOP) in Java




Step 7:

Hey supper coders ! Now that you know how to use methods, it’s time to dive into Object-Oriented Programming (OOP)—the core of Java that makes it powerful and scalable!


? What is Object-Oriented Programming (OOP)?

OOP is a programming paradigm that organizes code into objects—blueprints for real-world entities like cars, employees, or bank accounts.


??? Key Concepts of OOP in Java

1?? Classes and Objects ???

A class is a blueprint for creating objects.

An object is an instance of a class.

class Car {

    String brand = "Tesla";  

}

public class Main {

    public static void main(String[] args) {

        Car myCar = new Car();  // Creating an object

        System.out.println(myCar.brand);  // Output: Tesla

    }

}        

2?? Encapsulation ??

Encapsulation means hiding data and only allowing access through methods.

class Person {

    private String name;  // Private variable

    public void setName(String newName) {  // Setter method

        name = newName;

    }

    public String getName() {  // Getter method

        return name;

    }

}

public class Main {

    public static void main(String[] args) {

        Person p = new Person();

        p.setName("Alice");

        System.out.println(p.getName());  // Output: Alice

    }

}        

3?? Inheritance ???

Inheritance allows a class to reuse features from another class.

class Animal {

    void makeSound() {

        System.out.println("Animal makes a sound");

    }

}

class Dog extends Animal {

    void bark() {

        System.out.println("Dog barks");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog();

        myDog.makeSound();  // Output: Animal makes a sound

        myDog.bark();       // Output: Dog barks

    }

}        

4?? Polymorphism ??

Polymorphism allows one interface, multiple implementations.

class Animal {

    void makeSound() {

        System.out.println("Animal makes a sound");

    }

}

class Cat extends Animal {

    void makeSound() {

        System.out.println("Cat meows");

    }

}

public class Main {

    public static void main(String[] args) {

        Animal myAnimal = new Cat();

        myAnimal.makeSound();  // Output: Cat meows

    }

}        

5?? Abstraction ??

Abstraction hides implementation details and shows only essential features.

abstract class Vehicle {

    abstract void start();

}

class Bike extends Vehicle {

    void start() {

        System.out.println("Bike starts with a key");

    }

}

public class Main {

    public static void main(String[] args) {

        Bike myBike = new Bike();

        myBike.start();  // Output: Bike starts with a key

    }

}        


? Why is OOP Important?

? Makes code modular and scalable.

? Improves code reusability and organization.

? Helps in building real-world applications efficiently.


?? Coming Up Next:

Exception Handling—how to handle errors gracefully in Java!


Keep going, one step at a time! Stay curious, keep practicing, and let’s unlock Java together! ?

#BinaryBrains


Dulanka Gunathilaka

Undergraduate | Web Development

4 周

Very Infomative ! ??

要查看或添加评论,请登录

Nipuni Weerasuriya的更多文章

  • Exception Handling in Java

    Exception Handling in Java

    Step 8: Hey supper coders! Now that you understand Object-Oriented Programming (OOP), let’s learn about Exception…

    2 条评论
  • Methods in Java

    Methods in Java

    Step 6: Hey supper Coders! Now that you understand control flow statements, it’s time to explore methods—the foundation…

    2 条评论
  • Control Flow Statements in Java

    Control Flow Statements in Java

    Step 5: Hey supper coders! Now that you know how to use operators, it’s time to learn Control Flow Statements—the key…

    2 条评论
  • Operators in Java

    Operators in Java

    Step 4: Hey supper coders! Now that you’ve learned about variables and data types, it’s time to explore operators—the…

    1 条评论
  • Variables and Data Types in Java

    Variables and Data Types in Java

    Step 3: Hey supper coders! Now that you understand Java’s syntax and structure, let’s dive into variables and data…

    3 条评论
  • Understanding Java Syntax and Structure

    Understanding Java Syntax and Structure

    Step 2: Hey there, supper coders! It’s Nipuni Weerasuriya here again, ready to guide you through the next exciting step…

    2 条评论

社区洞察

其他会员也浏览了