0% found this document useful (0 votes)
5 views12 pages

OOP Concepts in Java: Encapsulation & Inheritance

The document covers Java concepts such as encapsulation, constructors, and inheritance, providing definitions, advantages, and code examples. It emphasizes the importance of controlled access to data through getters and setters, as well as constructor overloading and chaining. Additionally, it includes problem statements and hints for practical implementation exercises related to these topics.

Uploaded by

omglokesh
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)
5 views12 pages

OOP Concepts in Java: Encapsulation & Inheritance

The document covers Java concepts such as encapsulation, constructors, and inheritance, providing definitions, advantages, and code examples. It emphasizes the importance of controlled access to data through getters and setters, as well as constructor overloading and chaining. Additionally, it includes problem statements and hints for practical implementation exercises related to these topics.

Uploaded by

omglokesh
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

CS & IT

ENGINEERING
Java With OOPS
OOPs using Java

Lecture No-03 By- Aditya sir


Recap of Previous Lecture

Topic
0
Constructors, this & Intro to Inheritance
Topics to be Covered

Topic Constructors, this & Intro to Inheritance

Encapsulation

3
Topic : Encapsulation

Definition:
Encapsulation bundles data (fields) and the methods that operate on that data into a
single unit, restricting direct access to some components.
Advantages
▪ Data Integrity: Prevents invalid states.
▪ Maintainability: Internal representation can evolve without breaking clients.
▪ Security: Sensitive fields remain hidden.
▪ Controlled Access: Validation, logging, or side-effects can be placed in accessors.
Java Mechanisms:
▪ Mark fields private.
▪ Expose behavior via public, protected, or package-private methods.
▪ Prefer composition over exposing mutable fields.
Topic : Encapsulation

Snippet ([Link]):
Output:
public class EncapsulatedPoint {
private int x; // hidden from outside
Point: (2, 3)
private int y; // hidden from outside
public EncapsulatedPoint(int x, int y) {
this.x = x;
this.y = y;
}
// Controlled access
public int getX() { return x; }
public int getY() { return y; }
public static void main(String[] args) {
EncapsulatedPoint p = new EncapsulatedPoint(2, 3);
[Link]("Point: (" + [Link]() + ", " + [Link]() + ")");
}
}
Topic : Theory – Getters & Setters

▪ Purpose: Provide controlled field access and mutation.


▪ Getter (Accessor):
• Signature: public Type getFieldName()
• Can include caching, lazy initialization, or logging.
▪ Setter (Mutator):
• Signature: public void setFieldName(Type value)
• Can enforce validation, trigger notifications, or maintain invariants.
▪ JavaBeans Conventions:
• Field foo → getFoo(), setFoo(...).
• Boolean field → isFoo(), setFoo(...).
▪ Advanced Uses:
• Immutable Objects: Omit setters.
• Fluent Interfaces: Setters return this.
• Computed Properties: Getter computes value on demand.
Topic : Example 3 – Constructor Overloading & Chaining

Problem Statement:
• Implement Student with three constructors:
1. No-arg → defaults
2. One-arg (name)
3. Two-arg (name, age)
Hint:
• Use this(...) to chain from simpler to more detailed constructors.
Topic : Example 3 – Constructor Overloading & Chaining

Code ([Link]): public Student(String name, int age) {


public class Student { [Link] = name;
[Link] = age;
String name;
[Link](name + " is " +
int age; age + " years old.");
public Student() { }
this("Unknown", 0); public static void main(String[] args) {
} new Student();
new Student("Alice");
public Student(String name) {
new Student("Bob", 22);
this(name, 0); }
} }
Topic : Example 5 – Basic Inheritance & Method Overriding

Problem Statement:
• Define Animal with methods sound() and move(). Create Dog to override both
and add fetch().
Hint:
Use @Override and call [Link]() inside the override if desired.
Topic : Example 7 – Multilevel Inheritance & Initialization Order

Problem Statement:
• Trace initialization across Device → Computer → Laptop, including instance
blocks.
Hint:
• Add instance initializer blocks to see execution order.
Topic : Basic Getter & Setter
Example 1: HI
Problem Statement:
Create class Person with private name, public getter/setter. In main, set and print name.
Code ([Link]):
public class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { [Link] = name; }
public static void main(String[] args) {
Person p = new Person();
[Link]("Alice");
[Link]("Name: " + [Link]());
}
}
Output:
Name: Alice
THANK - YOU

You might also like