0% found this document useful (0 votes)
13 views2 pages

Java OOP Fundamentals Explained

The document introduces the fundamental concepts of Object-Oriented Programming (OOP) in Java, likening it to building with LEGO. It covers key principles such as encapsulation, inheritance, polymorphism, and various programming constructs like loops, arrays, and conditionals. Additionally, it explains access specifiers, constructors, and how to combine these elements to create organized and reusable code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Java OOP Fundamentals Explained

The document introduces the fundamental concepts of Object-Oriented Programming (OOP) in Java, likening it to building with LEGO. It covers key principles such as encapsulation, inheritance, polymorphism, and various programming constructs like loops, arrays, and conditionals. Additionally, it explains access specifiers, constructors, and how to combine these elements to create organized and reusable code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ Welcome to Java OOP Land: The Building Blocks of Great Code

Think of Java like LEGO — you build cool stuff by snapping together classes, objects, and logic.
The four magical building blocks of OOP are what make your code organized, reusable, and less
likely to explode in errors.

---

■ 1. Encapsulation — The “Keep Your Secrets Safe” Principle

Encapsulation hides data and allows controlled access using methods.

Example: class BankAccount { private double balance = 0; public void deposit(double amount) { if
(amount > 0) balance += amount; } public void withdraw(double amount) { if (amount <= balance)
balance -= amount; else [Link]("Not enough money, mate!"); } public double
getBalance() { return balance; } }

public class Main { public static void main(String[] args) { BankAccount acc = new BankAccount();
[Link](1000); [Link](500); [Link]("Remaining Balance: " +
[Link]()); } }

---

■ 2. Inheritance — “Like Parent, Like Child” Inheritance lets a class acquire traits and behavior from
another.

Example: class Animal { void eat() { [Link]("I eat food."); } }

class Dog extends Animal { void bark() { [Link]("Woof! Woof!"); } }

---

■ 3. Polymorphism — “Same Thing, Different Behavior” Polymorphism allows methods to behave


differently based on the object.

Example: class Animal { void speak() { [Link]("Some generic animal sound."); } } class
Dog extends Animal { void speak() { [Link]("Bark! Bark!"); } } class Cat extends Animal {
void speak() { [Link]("Meow!"); } }

---

■ 4. User Input — Let the Human Speak Back! Use Scanner to take input.

import [Link];

public class Main { public static void main(String[] args) { Scanner sc = new Scanner([Link]);
[Link]("Enter your name: "); String name = [Link](); [Link]("Hello, " +
name + "!"); } }

---

■ 5. Loops — The “Keep Doing It” Machines For, While, and Do-While loops repeat actions.

for (int i = 1; i <= 5; i++) { [Link]("I can count to " + i); }

---

■ 6. Arrays — “Organized Storage Shelves” Arrays store multiple values in one variable.
int[] scores = {85, 90, 75, 100}; for (int i = 0; i < [Link]; i++) { [Link]("Score " + i +
": " + scores[i]); }

---

■■ 7. Conditionals — The “Make Decisions” Brain

int marks = 82; if (marks >= 90) [Link]("A+"); else if (marks >= 80)
[Link]("B+"); else [Link]("Keep trying!");

---

■ 8. Access Specifiers — “Who Can See What”

private → Only in same class default → Same package protected → Package + subclasses public
→ Everyone

---

■■ 9. Constructors and Destructors — Birth and Death of Objects

class Student { String name; int age; Student(String n, int a) { name = n; age = a; } void intro() {
[Link]("Hi! I'm " + name + ", age " + age); } }

public class Main { public static void main(String[] args) { Student s1 = new Student("Zara", 19);
[Link](); } }

---

■ 10. Putting It All Together

class Hero { private String name; private int power; Hero(String name, int power) { [Link] =
name; [Link] = power; } void display() { [Link](name + " has power level " + power);
} } class SuperHero extends Hero { SuperHero(String name, int power) { super(name, power); } void
attack() { [Link]("Attacking with super strength ■"); } }

You might also like