0% found this document useful (0 votes)
35 views7 pages

Java OOP Concepts Quick Revision Notes

The document provides a concise overview of key Java OOP concepts, including type conversion, program structure, string allocation, common string methods, bitwise operators, conditional statements, arrays, encapsulation, access modifiers, polymorphism, and abstraction. It includes examples for each concept to illustrate their usage. These notes serve as a quick revision guide for core Java OOP topics.
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)
35 views7 pages

Java OOP Concepts Quick Revision Notes

The document provides a concise overview of key Java OOP concepts, including type conversion, program structure, string allocation, common string methods, bitwise operators, conditional statements, arrays, encapsulation, access modifiers, polymorphism, and abstraction. It includes examples for each concept to illustrate their usage. These notes serve as a quick revision guide for core Java OOP topics.
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

Java OOPs Concepts - Notes for Quick Revision

---

Type Conversion in Java

1. Widening Conversion (Implicit):

- Converts a smaller data type to a larger one.

- Example: int to long, float to double

int x = 10;

long y = x; // Implicit conversion

2. Narrowing Conversion (Explicit):

- Converts a larger data type to a smaller one using casting.

- Example: double to int

double a = 9.7;

int b = (int) a; // Explicit casting

---

Basic Java Program Structure

public class Main {

public static void main(String[] args) {

[Link]("Hello, World!");

- public class Main: Declares a public class named Main.


- public static void main(String[] args): Main method (entry point).

- [Link](): Prints text to the console.

String[] args:

- Accepts input from the command line.

public class Example {

public static void main(String[] args) {

[Link]("Argument: " + args[0]);

---

String Allocation

String name = "Java"; // Allocated in String pool

String another = new String("Java"); // Allocated in heap memory

- String Pool: Saves memory by storing one copy of identical string literals.

Example:

String a = "Hello";

String b = "Hello";

[Link](a == b); // true (same reference from pool)

String c = new String("Hello");

[Link](a == c); // false (different object in heap)


---

Common String Methods

String s = "Hello";

[Link](); // 5

[Link](1); // 'e'

[Link]("ell"); // true

[Link]("l"); // 2

[Link](1, 4); // "ell"

---

Bitwise Operators

Operator | Meaning | Example

-------- | ----------- | ---------------

& | AND |5&3=1

| | OR |5|3=7

^ | XOR |5^3=6

~ | NOT | ~5 = -6

<< | Left shift | 5 << 1 = 10

>> | Right shift | 5 >> 1 = 2

---

Conditional Statements

if (condition) {

// code
} else if (otherCondition) {

// code

} else {

// code

// switch

switch (value) {

case 1: break;

case 2: break;

default: break;

// ternary

String result = (age > 18) ? "Adult" : "Minor";

---

Arrays in Java

- Used to store multiple values of the same type.

int[] arr = new int[5];

arr[0] = 10;

int[] arr2 = {1, 2, 3, 4};

- Stored in heap memory.

Example:
for (int i = 0; i < [Link]; i++) {

[Link](arr2[i]);

---

Encapsulation

- Binding data and methods into a single unit (class).

- Achieved by:

- Making variables private

- Providing public getters and setters

Example:

class Person {

private String name;

public String getName() {

return name;

public void setName(String name) {

[Link] = name;

---

Access Modifiers
Modifier | Class | Package | Subclass | World

---------- | ----- | ------- | -------- | ------

private | Yes | No | No | No

default | Yes | Yes | No | No

protected | Yes | Yes | Yes | No

public | Yes | Yes | Yes | Yes

---

Polymorphism

1. Method Overloading (Compile-time):

class Calculator {

int add(int a, int b) { return a + b; }

int add(int a, int b, int c) { return a + b + c; }

2. Method Overriding (Runtime):

class Animal {

void sound() { [Link]("Animal sound"); }

class Dog extends Animal {

void sound() { [Link]("Dog barks"); }

---
Abstraction

1. Abstract Class:

abstract class Shape {

abstract void draw();

void display() { [Link]("Displaying shape"); }

2. Interface:

interface Drawable {

void draw();

interface Paintable {

void paint();

class Circle implements Drawable, Paintable {

public void draw() { [Link]("Drawing Circle"); }

public void paint() { [Link]("Painting Circle"); }

---

These notes cover core Java OOP topics.

Common questions

Powered by AI

Access modifiers in Java determine the visibility and accessibility of classes, methods, and variables. They define where an element can be accessed from. The key access modifiers are: - `private`: accessible only within the declaring class. - `default` (package-private): accessible within classes in the same package. - `protected`: accessible within the same package and subclasses. - `public`: accessible from any other class. This control helps in encapsulating the class details, reducing unintended interactions between classes, thus enhancing modularity and maintainability .

Widening conversion in Java is an implicit process where a smaller data type is converted to a larger one without explicit casting. For example, converting an int to a long as shown: `int x = 10; long y = x;` . Narrowing conversion is explicit and requires casting, converting a larger data type to a smaller size, such as converting a double to an int: `double a = 9.7; int b = (int) a;` .

Switch statements and if-else statements both provide conditional branch execution but differ in syntax and use cases. An if-else statement evaluates a boolean condition or statement block and executes the corresponding code if true, allowing for complex conditions with logical operators. Syntax: `if (condition) { block } else { block }` . A switch statement evaluates a single expression and matches it against specified case labels, executing the associated block accordingly. It's more concise for multiple discrete values. Syntax: `switch (value) { case 1: break; }` . Switch is ideal for simple equality checks, whereas if-else is suited for evaluating complex conditions.

Encapsulation in Java is the process of wrapping data (variables) and code (methods) together into a single unit, known as a class. It is achieved by declaring class variables as private and providing public getter and setter methods to control access and modification. For example: `class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }` . Encapsulation improves code maintenance and security by restricting direct access to the class variables, ensuring that only intended changes occur to the state of the object.

Abstract classes and interfaces both provide a way to achieve abstraction in Java, but they differ in key ways. An abstract class can have fields, constructors, and methods with concrete implementations alongside abstract methods to be defined by subclasses. For instance, `abstract class Shape { abstract void draw(); void display() { System.out.println("Displaying shape"); } }` . In contrast, an interface is purely abstract, containing no data fields and only method signatures: `interface Drawable { void draw(); }` . Interfaces support multiple inheritance, allowing a class to implement multiple interfaces, whereas a class can only extend one abstract class. Abstract classes are preferred when sharing a default functionality between related classes, while interfaces are ideal for defining contracts for disparate classes to follow.

Bitwise operators perform operations on individual bits of integer types. Common operators include: - `&` (AND): Both bits must be 1 to result in 1, e.g., `5 & 3` yields `1`. - `|` (OR): At least one bit must be 1 to result in 1, e.g., `5 | 3` yields `7`. - `^` (XOR): Result is 1 if bits are different, e.g., `5 ^ 3` yields `6`. - `~` (NOT): Inverts all bits of the operand, e.g., `~5` yields `-6`. - `<<` (Left shift): Shifts bits left, e.g., `5 << 1` yields `10`. - `>>` (Right shift): Shifts bits right, e.g., `5 >> 1` yields `2`. These operators are significant for low-level programming tasks, such as manipulating data in binary form for tasks like graphics processing or cryptography .

Polymorphism in Java is achieved in two primary ways: method overloading and method overriding. Method overloading occurs at compile-time and involves defining multiple methods with the same name but different parameter lists within the same class. For example, `class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }` . Method overriding, on the other hand, occurs at runtime and allows a subclass to provide a specific implementation for a method that is already defined in its superclass. For example, `class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } }` . The key difference is the timing; overloading resolves method calls at compile time, while overriding resolves them at runtime.

Arrays in Java store multiple values of a single type in a contiguous block of memory. Declaring an array involves specifying the data type and initializing its size or elements, for example, `int[] arr = new int[5]; arr[0] = 10;` for direct size allocation or `int[] arr2 = {1, 2, 3, 4};` for initialization with values . Arrays are stored in heap memory, which allows dynamic allocation at runtime. Iterating through arrays is commonly done using loops to access each element, for example `for (int i = 0; i < arr2.length; i++) { System.out.println(arr2[i]); }` . Arrays provide efficient element access and manipulation through indexed addressing but have a fixed size once created.

The String Pool in Java optimizes memory usage by storing only one copy of each distinct string literal. When a string is created using double quotes, it checks the pool for an existing entry with the same value. If it exists, the existing reference is returned; otherwise, a new string is added to the pool. For example, `String a = "Hello"; String b = "Hello"; System.out.println(a == b); // true` demonstrates that `a` and `b` point to the same object in the String Pool, saving memory . This optimization is not applied when strings are created with `new String("Hello")`, leading to different objects in heap memory.

A basic Java program consists of a class definition and a main method. The structure commonly starts with `public class` followed by the class name, enclosing methods and variables. The main method `public static void main(String[] args)` is crucial as it acts as the entry point for the program. The JVM calls this method when the program runs. `System.out.println()` within the main method outputs messages to the console. `String[] args` allows for command-line arguments to be passed into the program .

You might also like