Introduction to Object-Oriented Programming in Java
Introduction to Object-Oriented Programming in Java
Object-Oriented
Programming in Java
Himanya Bahri
22BCA017
What is OOP?
•Object-Oriented Programming (OOP) is a way
Example:
of writing code where you create "objects" and
public class Car {
"classes" to organize and structure your software.
String color;
This method makes your code easier to update,
String model;
reuse, and keep organized.
void start() {
•Key Concepts: System.out.println("Car started"); }
•Objects and Classes: void stop() {
•Object: An instance of a class representing System.out.println("Car stopped");
}}
a specific entity in the system.
•Class: A blueprint or template for creating OUTPUT-
Car started
objects. Defines attributes (data) and
Car stopped
methods (behavior).
Here, Car is a class, and each Car object can have
different color and model values.
Stack vs. Heap
Memory Division in Java:
•Stack Memory:
•Used for local variables and method calls.
•Follows LIFO order.
•Heap Memory:
•Used for storing objects and instance variables.
•Managed dynamically by the JVM’s garbage collector.
•Comparison:
•Stack Memory:
•Smaller, thread-specific, for variables with fixed sizes.
•Heap Memory:
•Larger, shared across threads, for dynamic or large data.
•Example:
•Classes: Dog and Cat
•Instance variables like weight and height stored in the heap.
•References to these objects stored in the stack.
Access Modifiers
• Overview:
• Access modifiers in Java control who can access certain parts of your code. They help protect your data
and methods by setting boundaries.
• Access Levels:
• Public: Accessible from any class.
• Protected: Accessible within the same package and subclasses.
• Default (Package-Private): Accessible only within the same package.
• Private: Accessible only within the defining class.
• Purpose:
• Code Integrity: Control access to class members.
• Security: Protect data from unauthorized access.
• Collaboration: Facilitate better team coding practices.
Naming Conventions
Overview:
Naming conventions in Java are rules for naming your classes, methods, and
variables to keep your code organized and readable
•Key Rules:
•Classes: Start with an uppercase letter and are typically nouns (e.g., String,
Car).
•Methods: Start with a lowercase letter and are typically verbs (e.g., start,
stop).
•Variables: Start with a lowercase letter (e.g., firstName, year).
•Importance:
•Readability: Makes code easier to understand and maintain.
•Camel Casing: Used for multi-word identifiers (e.g., firstName).
Constructors
• Introduction: •Example:
• Special methods for initializing objects. Person Class:
public class Person {
• Key Features: private String name;
o Default Constructor: Automatically provided public Person(String name) {
if no constructor is defined. this.name = name; }
o Overloading: Multiple constructors with public void wakeUp() { ... }
public void eat() { ... }
different parameters.
public void exercise() { ... }
o Initialization: Set initial values for object @Override
attributes. public String toString() {
o Constructors are essential for creating return "Person{name='" + name + “’}”;
objects, can be overloaded, and must match }
} OUTPUT-
the class name.
Person{name='Alice'}
Packages and Import
• Concept of Packages:
Group classes, interfaces, and sub-packages to organize code and avoid naming conflicts.
• Package Declaration:
Use package packageName; at the beginning of a file.
Hierarchical naming (e.g., animals.reptiles).
• Types of Packages:
Built-in Packages: e.g., java.lang, java.util.
User-Defined Packages: Created by developers.
• Importing Classes:
• Syntax:
• Import specific classes: import package.name.ClassName;
• Import all classes: import package.name.*;
• Default Imports: java.lang classes are imported by default.
Static Keyword
Overview:
•Used for memory management and sharing Example:
variables/methods across instances. java
•Static Variables and Methods: public class Car {
•Static Variables: Shared among all instances (e.g., static int numberOfCars = 0;
static String schoolName). static void increaseCarCount() {
•Static Methods: Belong to the class, not objects numberOfCars++;
(e.g., static void printDetails()). }
•Static Blocks and Nested Classes: }
•Static Blocks: Initialize static variables. OUTPUT-
•Static Nested Classes: Associated with the outer Number of cars: 2
class but do not require an instance of the outer
class.
Static Import
•Static imports allow you to use static methods or variables directly without needing to specify the
class name. For instance, if you have a `showSalary` method in an `Employee` class, you can call it
directly in your code after a static import, like `import static example.Employee.showSalary;`.
•Usage Example:
•Import specific static methods/variables:
import static example.Employee.showSalary;
•Use wildcard to import all static members:
import static java.lang.Math.*;
•Benefits:
•Cleaner code, avoids repetitive class names
Nested & Inner Classes
•Types:
•Static Nested Class:
•This class is defined with the `static` keyword and can only access static members of the outer
class. It cannot access non-static members of the outer class directly.
•Non-Static Inner Class:
•This class does not use the `static` keyword and can access both static and non-static members
of the outer class, including private members.
•Accessing Members:
•Static Nested Class: Only static members.
•Non-Static Inner Class: All members of the outer class.
Local Inner Class
Definition: •Example:
•Defined within a method or block; public class NumberChecker {
scope limited to that block. public void checkNumber(int number) {
class LocalInner {
•Access: void check() {
•Can access final and non-final if (number % 2 == 0) {
local variables. System.out.println("Even");
•Usage: } else { System.out.println("Odd");
•Instantiated and used within the }
}
block it is defined in. LocalInner localInner = new LocalInner();
localInner.check();
}
}
THANK YOU !!