0% found this document useful (0 votes)
14 views3 pages

Day - 4 - Nested Loop (10), Define Scope of A Variable (10), Object Orientation, Encapsulation, Abstraction Etc., (15)

Uploaded by

saikrishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Day - 4 - Nested Loop (10), Define Scope of A Variable (10), Object Orientation, Encapsulation, Abstraction Etc., (15)

Uploaded by

saikrishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object Oriented Programming Concepts using JAVA

Topic:

Nested Loop (10), Define Scope of a Variable (10), Object Orientation, encapsulation, Abstraction etc., (15),
Know How to read and write object fields (15),

Nested Loops

A nested loop in Java is a loop inside another loop. The inner loop will complete all its iterations for each iteration of the outer
loop.

Syntax:

java

for (initialization; condition; update) {


for (initialization; condition; update) {
// code to be executed
}
}

Example:

java

for (int i = 1; i <= 3; i++) {


for (int j = 1; j <= 3; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}

Explanation:

 The outer loop runs 3 times, and for each iteration, the inner loop runs 3 times, resulting in 9 outputs in total.

Scope of a Variable

The scope of a variable in Java defines the region of the program where the variable is accessible. Java has three types of variable
scopes:

 Local Scope: Variables declared inside a method or block.


 Instance Scope: Variables declared inside a class but outside any method, and they belong to an instance of the class.
 Class/Static Scope: Variables declared as static within a class, accessible by all instances of the class.

Example:

java

public class VariableScope {


int instanceVariable = 10; // Instance scope

public void method() {


int localVariable = 20; // Local scope
System.out.println("Instance Variable: " + instanceVariable);
System.out.println("Local Variable: " + localVariable);
Object Oriented Programming Concepts using JAVA

public static void main(String[] args) {


VariableScope obj = new VariableScope();
obj.method();
}
}

Object Orientation: Encapsulation and Abstraction

Encapsulation

Encapsulation is the concept of wrapping data (variables) and methods that operate on the data into a single unit, called a class. It
also restricts direct access to some of an object’s components, which is a way of hiding the internal state of the object.

Example:

java

public class Person {


private String name; // Encapsulated field

public void setName(String name) { // Setter method


this.name = name;
}

public String getName() { // Getter method


return name;
}
}

Explanation:

 The name variable is private, meaning it cannot be accessed directly from outside the class. Instead, you use getter and
setter methods.

Abstraction

Abstraction involves hiding complex implementation details and showing only the essential features of an object. It's achieved
using abstract classes and interfaces.

Example:

java

abstract class Animal {


abstract void sound(); // Abstract method

public void sleep() {


System.out.println("Animal is sleeping");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Woof");
}
}
Object Oriented Programming Concepts using JAVA

public class TestAbstraction {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Outputs: Woof
dog.sleep(); // Outputs: Animal is sleeping
}
}

Explanation:

 Animal is an abstract class with an abstract method sound(). The Dog class provides the implementation for the
sound() method.

Reading and Writing Object Fields

Reading and writing object fields refers to accessing and modifying the attributes of an object.

Example:

java

public class Car {


public String model; // Public field

public void displayModel() {


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

public class Main {


public static void main(String[] args) {
Car car = new Car(); // Creating an object
car.model = "Tesla Model S"; // Writing to the field
car.displayModel(); // Reading the field
}
}

Explanation:

 The model field of the Car class is accessed directly. The field is assigned a value ("Tesla Model S"), and then it's
read using the displayModel() method.

Summary

 Nested Loops: A loop inside another loop, allowing complex iteration patterns.
 Variable Scope: Defines where a variable can be accessed within the code.
 Encapsulation: Wrapping data and methods into a single unit and restricting access.
 Abstraction: Hiding implementation details and showing only the essential features.
 Reading/Writing Object Fields: Accessing and modifying the attributes of an object using direct access or methods.

You might also like