Java OOP Introduction šŸŽÆ

beginner
19 min

Java OOP Introduction šŸŽÆ

Welcome to the Java Object-Oriented Programming (OOP) Introduction lesson! šŸŽ‰ In this tutorial, we'll explore the fundamentals of Java OOP, making it easy for both beginners and intermediates to understand and apply these concepts.

What is Object-Oriented Programming (OOP)? šŸ“

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, objects, and methods. It encourages modularity, reusability, and a clean separation of concerns.

Key Concepts in OOP:

  • Classes: Templates for creating objects that have properties (attributes) and behaviors (methods).
  • Objects: Instances of classes, each with its own state (attributes' values).
  • Inheritance: A way to create new classes based on existing ones, allowing code reuse and hierarchy.
  • Polymorphism: The ability of objects to take multiple forms, enhancing flexibility and extensibility.
  • Encapsulation: Hiding the internal details of an object, promoting data privacy and integrity.

Creating a Simple Class in Java šŸ’”

Let's start by creating a simple class to represent a Person.

java
public class Person { String name; int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Method public void displayDetails() { System.out.println("Name: " + name + ", Age: " + age); } }

šŸ’” Pro Tip: A class in Java starts with an uppercase letter, and methods and variables are camelCase.

Creating and Using an Object šŸ’”

Now, let's create an instance of our Person class and use it to display details.

java
public class Main { public static void main(String[] args) { Person johnDoe = new Person("John Doe", 30); johnDoe.displayDetails(); } }

When you run this code, you'll see the following output:

Name: John Doe, Age: 30

Inheritance in Java šŸ“

Inheritance allows one class (the subclass) to inherit properties and methods from another class (the superclass). Here's an example with a Student class inheriting from the Person class.

java
public class Student extends Person { String studentId; // Constructor public Student(String name, int age, String studentId) { super(name, age); // Call the Person constructor this.studentId = studentId; } }

Polymorphism in Java šŸ“

Polymorphism allows objects to take multiple forms. In Java, it can be achieved through method overriding and method overloading.

java
public class Vehicle { String name; public void move() { System.out.println("Moving..."); } } public class Car extends Vehicle { @Override public void move() { System.out.println("Driving..."); } } public class Main { public static void main(String[] args) { Vehicle car = new Car(); car.move(); // Output: Driving... } }

Encapsulation in Java šŸ“

Encapsulation hides the internal details of an object, promoting data privacy and integrity.

java
public class Account { private int balance; public void deposit(int amount) { balance += amount; } public int getBalance() { return balance; } }

šŸ’” Pro Tip: Use the private keyword to mark variables that should be encapsulated.

Quiz: Polymorphism šŸŽÆ

Quick Quiz
Question 1 of 1

What is polymorphism in Java?

Wrapping Up šŸŽÆ

Congratulations on completing the Java OOP Introduction lesson! You've learned the basics of classes, objects, inheritance, polymorphism, and encapsulation. With this knowledge, you're well on your way to mastering Java Object-Oriented Programming.

Keep coding, and happy learning! šŸš€šŸŒŸ