Java Object-Oriented Programming (OOP) Tutorial

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure and organize code. Java is a fully object-oriented language, and understanding OOP concepts is essential for writing clean, modular, and reusable code. This tutorial will introduce you to the core principles of OOP in Java: Classes and Objects, Inheritance, Polymorphism, Encapsulation, and Abstraction.

By the end of this tutorial, you'll understand how to use OOP principles to design and implement robust Java applications.

Classes and Objects

A class is a blueprint for creating objects, while an object is an instance of a class. Classes define properties (attributes) and behaviors (methods) that objects can have.

  • Defining a Class:
    public class Car {
        // Attributes (fields)
        String brand;
        String color;
        int year;
    
        // Constructor
        public Car(String brand, String color, int year) {
            this.brand = brand;
            this.color = color;
            this.year = year;
        }
    
        // Method
        public void displayInfo() {
            System.out.println("Brand: " + brand + ", Color: " + color + ", Year: " + year);
        }
    }
  • Creating Objects:
    public class Main {
        public static void main(String[] args) {
            // Create an object of the Car class
            Car myCar = new Car("Toyota", "Red", 2020);
    
            // Call a method on the object
            myCar.displayInfo(); // Output: Brand: Toyota, Color: Red, Year: 2020
        }
    }

Inheritance

Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass). This promotes code reuse and establishes a relationship between classes.

  • Example:
    // Superclass
    public class Vehicle {
        String type;
    
        public Vehicle(String type) {
            this.type = type;
        }
    
        public void start() {
            System.out.println(type + " is starting.");
        }
    }
    
    // Subclass
    public class Car extends Vehicle {
        String brand;
    
        public Car(String type, String brand) {
            super(type); // Call the superclass constructor
            this.brand = brand;
        }
    
        public void displayBrand() {
            System.out.println("Brand: " + brand);
        }
    }
    
    // Main class
    public class Main {
        public static void main(String[] args) {
            Car myCar = new Car("Car", "Toyota");
            myCar.start(); // Output: Car is starting.
            myCar.displayBrand(); // Output: Brand: Toyota
        }
    }

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to behave differently based on the object that invokes them.

  • Method Overriding:
    public class Animal {
        public void sound() {
            System.out.println("Animal makes a sound");
        }
    }
    
    public class Dog extends Animal {
        @Override
        public void sound() {
            System.out.println("Dog barks");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal myAnimal = new Dog(); // Polymorphism
            myAnimal.sound(); // Output: Dog barks
        }
    }

Encapsulation

Encapsulation is the practice of bundling data (attributes) and methods that operate on the data into a single unit (class) and restricting access to the internal details. This is achieved using access modifiers like private, protected, and public.

  • Example:
    public class Person {
        private String name; // Private attribute
    
        // Public getter and setter methods
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Person person = new Person();
            person.setName("John");
            System.out.println("Name: " + person.getName()); // Output: Name: John
        }
    }

Abstraction

Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. In Java, abstraction is achieved using abstract classes and interfaces.

  • Abstract Class Example:
    abstract class Shape {
        abstract void draw(); // Abstract method
    }
    
    class Circle extends Shape {
        @Override
        void draw() {
            System.out.println("Drawing a circle");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Shape myShape = new Circle();
            myShape.draw(); // Output: Drawing a circle
        }
    }
  • Interface Example:
    interface Animal {
        void sound(); // Interface method
    }
    
    class Dog implements Animal {
        @Override
        public void sound() {
            System.out.println("Dog barks");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal myAnimal = new Dog();
            myAnimal.sound(); // Output: Dog barks
        }
    }

This tutorial covered the core principles of Object-Oriented Programming in Java. Practice using these concepts to design and implement robust and scalable applications.

0 Interaction
2.4K Views
Views
33 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home