0% found this document useful (0 votes)
4 views13 pages

OOPs_Concepts_in_Java

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, constructors, and the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. It also discusses types of inheritance, method overloading and overriding, abstract classes, access specifiers, and the distinction between abstraction and encapsulation. Additionally, it highlights the practical implementation of OOP in real-life applications like UI frameworks and game engines.

Uploaded by

Yash Gadakh
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)
4 views13 pages

OOPs_Concepts_in_Java

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, constructors, and the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. It also discusses types of inheritance, method overloading and overriding, abstract classes, access specifiers, and the distinction between abstraction and encapsulation. Additionally, it highlights the practical implementation of OOP in real-life applications like UI frameworks and game engines.

Uploaded by

Yash Gadakh
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/ 13

OOPs Concepts in Java with Examples

Class & Objects

class Car {
String brand = "Toyota";
void honk() {
System.out.println("Beep!");
}
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.brand);
myCar.honk();
}
}
OOPs Concepts in Java with Examples

Constructor & Types

class Car {
Car() {
System.out.println("Default Constructor");
}
Car(String brand) {
System.out.println("Car Brand: " + brand);
}
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car("Ford");
}
}
OOPs Concepts in Java with Examples

Pillars of OOPs

// 1. Encapsulation - Wrapping data and methods


// 2. Abstraction - Hiding internal details
// 3. Inheritance - Reuse of code
// 4. Polymorphism - Same name, different behavior
OOPs Concepts in Java with Examples

Types of Inheritance

// Java supports:
// - Single
// - Multilevel
// - Hierarchical

class A {}
class B extends A {} // Single Inheritance
class C extends B {} // Multilevel Inheritance
OOPs Concepts in Java with Examples

Polymorphism

// Compile-time (Method Overloading)


class Demo {
void show(int a) {
System.out.println("int: " + a);
}
void show(String b) {
System.out.println("String: " + b);
}
}

// Runtime (Method Overriding)


class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
OOPs Concepts in Java with Examples

Runtime vs Compile Time

// Compile-time: Method Overloading


// Runtime: Method Overriding using dynamic dispatch
OOPs Concepts in Java with Examples

Virtual class and functions

// In Java, all non-static methods are virtual by default.


// Method overriding uses dynamic binding.
OOPs Concepts in Java with Examples

Pure virtual class (Abstract Class)

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing Circle");
}
}
OOPs Concepts in Java with Examples

Friend functions

// Java does not support friend functions directly.


// Access control is handled using access modifiers.
OOPs Concepts in Java with Examples

Static method

class Utility {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(Utility.square(4));
}
}
OOPs Concepts in Java with Examples

Access specifiers

// public - accessible everywhere


// private - within the class only
// protected - within package and subclass
// default - package-private
OOPs Concepts in Java with Examples

Abstraction vs Encapsulation

// Abstraction: Hiding implementation using abstract classes or interfaces


// Encapsulation: Keeping data private and providing public getters/setters

class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
OOPs Concepts in Java with Examples

Implementation

// Real-life use: OOP is used in UI frameworks, game engines, enterprise apps.


class Button {
void render() {
System.out.println("Rendering button...");
}
}

You might also like