- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Object-Oriented Programming (OOP)
Java is an object-oriented programming (OOP) language, which means it organizes software design around data, or objects, rather than functions and logic. Instead of writing a long list of instructions, you create "objects" that interact with one another, much like how objects interact in the real world. This approach makes code more modular, easier to maintain, and highly reusable.
Classes and Objects:
- A class is a blueprint or a template for creating objects. It defines the structure: what data the object will hold (attributes) and what it can do (methods).
- An object is a specific instance of that class. If "Car" is the blueprint, your neighbor's silver 2022 Toyota is the object.
class Car {
// Attributes (State)
String brand;
int year;
// Method (Behavior)
void drive() {
System.out.println("Driving the " + brand);
}
}
// Creating objects of the class
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.drive();
ElectricCar) and your methods using camelCase (e.g., startEngine) to follow standard Java naming conventions.
new keyword when creating an object. Writing Car myCar; only declares a reference; it doesn't actually create the object in memory. You must use new Car(); to allocate space for it.
Encapsulation:
- Encapsulation is the practice of keeping fields (attributes) private and providing access to them through public methods. Think of it like a protective shield that prevents outside code from accidentally corrupting an object's internal state.
- By using getters and setters, you can add validation logic for example, ensuring a "price" attribute is never set to a negative number.
class Student {
private String name;
private int age;
// Getter method for name
public String getName() {
return name;
}
// Setter method with logic
public void setName(String name) {
if(name != null && !name.isEmpty()) {
this.name = name;
}
}
}
private by default. Only expose what is absolutely necessary through public methods to keep your code "loosely coupled" and easier to debug.
Inheritance:
- Inheritance allows a new class (subclass) to adopt the properties and methods of an existing class (superclass). It represents an "is-a" relationship. For example, a
Dog"is-a"Animal. - This prevents code duplication. Instead of writing "eat" logic for every animal class, you write it once in the
Animalclass and let others inherit it.
class Animal {
void eat() {
System.out.println("This animal is consuming food.");
}
}
// Dog inherits from Animal
class Dog extends Animal {
void bark() {
System.out.println("The dog is barking!");
}
}
Polymorphism:
- Polymorphism means "many forms." In Java, it allows us to perform a single action in different ways.
- It is most commonly seen when a superclass reference is used to point to a subclass object. This allows for flexible code that can work with any "Animal" without needing to know if it's a Dog, Cat, or Bird at compile time.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks: Woof Woof!");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows: Meow!");
}
}
@Override annotation when overriding methods. It tells the compiler to check if the method actually exists in the parent class, helping you catch typos early.
Abstraction:
- Abstraction is about hiding complexity. It focuses on what an object does instead of how it does it.
- Think of a TV remote: you know that pressing the "Power" button turns on the TV (the "what"), but you don't need to understand the electrical circuitry inside (the "how").
- In Java, we use
abstractclasses orinterfacesto define these "contracts."
abstract class Shape {
abstract void draw(); // No body, just a requirement
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle using radius and Pi.");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing a rectangle using length and width.");
}
}
Shape myShape = new Shape(); will result in a compilation error because an abstract class is incomplete by design.
Summary
Java's OOP features Classes/Objects, Encapsulation, Inheritance, Polymorphism, and Abstraction provide a powerful framework for modern software development. By modeling your code after real-world entities, you create systems that are modular, easier to test, and ready to scale. Mastering these concepts is the single most important step in becoming a professional Java developer.