1. Class and Object
Class:
A class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
public class Car {
// Fields (variables)
String color;
String model;
int year;
// Methods
void displayInfo() {
System.out.println("Model: " + model);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
}
}
Object:
An object is an instance of a class. When a class is instantiated, memory is allocated, and a reference to that memory is returned.
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
myCar.color = "Red";
myCar.model = "Toyota";
myCar.year = 2021;
myCar.displayInfo();
}
}
2. Encapsulation
Encapsulation is the mechanism of restricting access to some of the object's components and protecting the object's state from unauthorized access and modification. This is achieved through access modifiers (private, public, protected).
public class Person {
// Private fields
private String name;
private int age;
// Public getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
}
3. Inheritance
Inheritance is a mechanism wherein a new class inherits the properties and behavior (methods) of an existing class. The existing class is called the superclass, and the new class is called the subclass.
// Superclass
public class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
public class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method
myDog.bark(); // Subclass method
}
}
4. Polymorphism
Polymorphism means "many forms," and it allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Polymorphism can be achieved through method overriding and method overloading.
Method Overriding:
When a subclass provides a specific implementation of a method that is already defined in its superclass.
public class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks
}
}
Method Overloading:
When two or more methods in the same class have the same name but different parameters.
public class MathOperation {
// Method overloading by changing the number of parameters
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
MathOperation math = new MathOperation();
System.out.println(math.add(5, 3)); // Outputs: 8
System.out.println(math.add(5, 3, 2)); // Outputs: 10
}
}
5. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object. This can be achieved using abstract classes and interfaces.
Abstract Class:
An abstract class cannot be instantiated and can contain abstract methods which are methods without implementation.
abstract class Shape {
abstract void draw();
}
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(); // Outputs: Drawing a circle
}
}
Interface:
An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
interface Animal {
void eat();
void sleep();
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog eats");
}
@Override
public void sleep() {
System.out.println("Dog sleeps");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Outputs: Dog eats
myDog.sleep(); // Outputs: Dog sleeps
}
}
These are the core concepts of OOP in Java. By understanding and applying these principles, you can create well-structured, reusable, and maintainable code.
Top comments (1)
So, the best object-oriented system is a system with only one object ----
warehouse/workshop model
.Systematic Best Practices for Java's New Features: Java has abandoned OOP in favor of my theory!
The unification of Microservice, DB, OOP, FP and Warehouse/Workshop Model
Traditional IT theory (OOP, FP and hardware architecture, etc.) are pseudoscience. they belong to what physicist Wolfgang Pauli said "Not Even Wrong".