0% found this document useful (0 votes)
11 views5 pages

LC0: Object-Oriented Programming (OOP) in Java

The document explains Object-Oriented Programming (OOP) in Java, highlighting key concepts such as classes, objects, constructors, and the four pillars of OOP: encapsulation, inheritance, polymorphism, and abstraction. It provides examples to illustrate each concept, such as defining a Dog class and creating objects from it. Additionally, it includes practice questions to reinforce understanding of OOP principles.

Uploaded by

nilbarua51
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)
11 views5 pages

LC0: Object-Oriented Programming (OOP) in Java

The document explains Object-Oriented Programming (OOP) in Java, highlighting key concepts such as classes, objects, constructors, and the four pillars of OOP: encapsulation, inheritance, polymorphism, and abstraction. It provides examples to illustrate each concept, such as defining a Dog class and creating objects from it. Additionally, it includes practice questions to reinforce understanding of OOP principles.

Uploaded by

nilbarua51
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
You are on page 1/ 5

LC0: Object-Oriented Programming (OOP) in Java

What is OOP?

OOP means we design programs by thinking in terms of real-world objects — like dogs,
cars, books — each with properties (data) and actions (methods).

Java is built around OOP.

1. Class – The Blueprint

A class is like a recipe or blueprint. It defines what something is and can do, but it’s not the
thing itself.

Example:

class Dog {

String name;

int age;

void bark() {

System.out.println(name + " says Woof!");

This defines a Dog with:

 Two properties: name, age

 One action: bark()

2. Object – The Real Thing

An object is an actual dog made from the Dog class.


Example:

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog(); // create object

myDog.name = "Bruno"; // assign values

myDog.age = 5;

myDog.bark(); // call method

Output:

Bruno says Woof!

3. Constructor – Special Method to Build Objects

Constructors help initialize objects automatically.

Example:

class Dog {

String name;

int age;

// Constructor

Dog(String n, int a) {

name = n;

age = a;

void bark() {

System.out.println(name + " barks!");

Then in main:

Dog d = new Dog("Shadow", 3); // no need to assign values manually

d.bark();
4 OOP Pillars in Java

Pillar Meaning

Encapsulation Hides the internal details and exposes only what’s needed

Inheritance One class can inherit properties/methods from another

Polymorphism Same method name, di erent behavior (like overloading)

Abstraction Hiding complex reality, showing only what matters

Encapsulation Example:

class Person {

private String name; // private = hidden

public void setName(String newName) {

name = newName;

public String getName() {

return name;

Inheritance Example:

class Animal {

void makeSound() {

System.out.println("Some sound");

class Cat extends Animal {

void makeSound() {

System.out.println("Meow");

}
Polymorphism Example:

class MathUtils {

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

Abstraction Example:

abstract class Shape {

abstract void draw();

class Circle extends Shape {

void draw() {

System.out.println("Drawing a Circle");

Practice Questions:

Q1: Create a class Car with brand, model, and a method drive()

Q2: Make a Bird class that inherits from Animal and overrides makeSound() to print
“Tweet!”

Q3: Use encapsulation to protect a person’s age and create setAge() and getAge()
methods.
TL;DR Summary:

Concept What It Does

Class Blueprint for objects

Object Real thing created from a class

Constructor Sets initial values

Encapsulation Keeps data safe

Inheritance Passes features to child classes

Polymorphism One thing, many forms

Abstraction Hides complex stu , shows essentials

You might also like