0% found this document useful (0 votes)
2 views

Oops in Java

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Java, including encapsulation, inheritance, polymorphism, abstraction, constructors, and access specifiers. Each concept is defined, categorized, and illustrated with code examples. The document serves as a guide for understanding the fundamental principles of OOP in Java.
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)
2 views

Oops in Java

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Java, including encapsulation, inheritance, polymorphism, abstraction, constructors, and access specifiers. Each concept is defined, categorized, and illustrated with code examples. The document serves as a guide for understanding the fundamental principles of OOP in Java.
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/ 9

OOPs in Java – Full Concept with Types & Code

OOP (Object-Oriented Programming) is a programming style based on "objects" —


instances of classes that bundle data and methods.

BY:Coding_error1

1. ENCAPSULATION
Definition:

Encapsulation is the process of wrapping variables (data) and methods (code) together as a
single unit.
Also hides the internal state of the object using private and allows access via
getters/setters.

Type:

Data Hiding

Code Example:
java

class Person {
private String name; // private variable

public void setName(String name) { // setter


this.name = name;
}

public String getName() { // getter


return name;
}
}

public class Main {


public static void main(String[] args) {
Person p = new Person();
p.setName("Aarav");
System.out.println(p.getName()); // Aarav
}
}

BY: coding_error1
2. INHERITANCE
Definition:

Inheritance allows one class to inherit fields and methods from another. Promotes code
reusability.

Types of Inheritance in Java:

Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance – Not supported via classes, but via interfaces

Code Examples:

Single Inheritance:

java

class Animal {
void eat() {
System.out.println("Eating...");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Barking...");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}

Multilevel Inheritance:

java

class Animal {
void eat() {

BY: coding_error1
System.out.println("Eating...");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Barking...");
}
}

class Puppy extends Dog {


void weep() {
System.out.println("Weeping...");
}
}

public class Main {


public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}

Hierarchical Inheritance:

java

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


void meow() {
System.out.println("Cat meows");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.sound();
d.bark();
c.sound();
c.meow();
}
}

BY: coding_error1
3. POLYMORPHISM
Definition:

Polymorphism means “many forms”. The same method behaves differently based on the
object.

Types of Polymorphism:

Compile-time (Static) Method Overloading


Runtime (Dynamic) Method Overriding

Code Examples:

Compile-Time (Method Overloading):

java

class MathOp {
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) {
MathOp m = new MathOp();
System.out.println(m.add(2, 3)); // 5
System.out.println(m.add(1, 2, 3)); // 6
}
}

Runtime (Method Overriding):

java

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Lion extends Animal {


@Override
void sound() {

BY: coding_error1
System.out.println("Lion roars");
}
}

public class Main {


public static void main(String[] args) {
Animal a = new Lion();
a.sound(); // Lion roars
}
}

4. ABSTRACTION
Definition:

Abstraction hides internal details and shows only essential features to the user.

Types of Abstraction:

Abstract Class – abstract keyword


Interface – interface keyword (100% abstraction)

Code Examples:

Abstract Class:

java

abstract class Shape {


abstract void draw(); // abstract method

void message() {
System.out.println("This is a shape");
}
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing Circle");
}
}

public class Main {


public static void main(String[] args) {
Shape s = new Circle();
s.draw();
s.message();
}
}

BY: coding_error1
Interface:

java

interface Drawable {
void draw(); // implicitly public and abstract
}

class Rectangle implements Drawable {


public void draw() {
System.out.println("Drawing Rectangle");
}
}

public class Main {


public static void main(String[] args) {
Drawable d = new Rectangle();
d.draw();
}
}

5. CONSTRUCTORS in Java
Definition:

A constructor is a special method used to initialize objects. It has the same name as the class
and no return type.

Types of Constructors:

Default Constructor
Parameterized Constructor
Copy Constructor (custom-made in Java)

Examples:

Default Constructor:

java

class Car {
Car() {
System.out.println("Car object created!");
}
}

public class Main {


public static void main(String[] args) {
Car c1 = new Car(); // invokes default constructor

BY: coding_error1
}
}

Parameterized Constructor:

java

class Car {
String model;

Car(String model) {
this.model = model;
}

void showModel() {
System.out.println("Model: " + model);
}
}

public class Main {


public static void main(String[] args) {
Car c1 = new Car("Tesla");
c1.showModel(); // Output: Model: Tesla
}
}

Copy Constructor (Custom in Java):

java

class Car {
String model;

Car(String model) {
this.model = model;
}

Car(Car c) { // copy constructor


this.model = c.model;
}

void show() {
System.out.println("Model: " + model);
}
}

public class Main {


public static void main(String[] args) {
Car c1 = new Car("BMW");
Car c2 = new Car(c1);
c2.show(); // Output: Model: BMW
}
}

BY: coding_error1
6. ACCESS SPECIFIERS / MODIFIERS
Definition:

Access specifiers define visibility/scope of classes, variables, constructors, and methods.

Types of Access Specifiers:

Specifier Within Class Within Package Subclass Outside


private
default
protected
public

Examples:

Private:

java

class Person {
private String name = "Riya"; // only accessible in this class

private void display() {


System.out.println(name);
}
}

Default (No keyword used):

java

class Car {
int speed = 100; // default access

void show() {
System.out.println("Speed: " + speed);
}
}

Protected:

BY: coding_error1
java

class Animal {
protected void sound() {
System.out.println("Animal sound");
}
}

class Dog extends Animal {


void bark() {
sound(); // Accessible because it's protected
}
}

Public:

java

public class Hello {


public void greet() {
System.out.println("Hello World");
}
}

BY: coding_error1

You might also like