SSCA2022: CORE JAVA
Module-3 Object Oriented Programming Fundamentals
Class Fundamentals
Class - classes are the building blocks of object-oriented programming. They
define the blueprint or template for creating objects.
A class in Java is a user-defined data type that groups data members (fields)
and methods (functions) together.
Structure of class
Key Concepts
•A class contains:
•Fields: store the state/data
•Methods: define behavior
•A class doesn't consume memory until an object is created.
•Classes promote encapsulation, modularity, and reusability.
Object
Object - An object is a real-world entity that is created from a class.
It represents an instance of a class and has:
•State → stored in fields (variables)
•Behavior → defined by methods
Stack,Heap and Static Memory
Object Reference
An object reference is a variable that holds the memory
address (reference) of the object in the heap.
🕒 Object Lifetime in Java
Creation When you use new, memory is allocated in
the heap.
Usage You call methods or access fields using the
reference.
Dereferencing When the reference is set to null or
goes out of scope.
Garbage Collection JVM automatically deletes unused
objects using Garbage Collector.
Garbage Collection
Garbage Collector- Java has automatic memory
management.
Objects with no references are eligible for garbage collection.
Student s1 = new Student(); // Object created
s1 = null; // Dereferenced
[Link](); // Suggest GC (not guaranteed)
Constructor and initialization code block
A constructor is a special method that is used to initialize objects.
It has the same name as the class and does not have a return type, not even void.
Example
Initialization Code Block (Instance Initializer Block)
An initialization block is a block of code enclosed in {} used to
initialize instance variables.
•It runs before the constructor when the object is created.
•Useful when multiple constructors need common initialization
code.
{
// Initialization code
}
Example with Constructor & Initialization Block:
Access Control
Access Control in Java is managed using access modifiers
that determine the visibility (scope) of classes, methods,
constructors, and variables.
📘 Descriptions with Examples
Access Control
Nested Class
A nested class is a class defined within another class. It
is used to logically group classes that are only used in one
place and to increase encapsulation.
Types:
[Link] Nested Class
[Link]-static Inner Class
[Link]-local Inner Class
[Link] Inner Class
Inner Class
An inner class is a non-static class declared inside another class.
It can access all members (even private) of the outer class.
Abstract Class
An abstract class in Java is a class that cannot be
instantiated and may contain abstract methods (without
body) as well as concrete methods (with body).
abstract class Animal {
abstract void sound(); // Abstract method
void eat() { // Concrete method
[Link]("This animal eats food.");
}
}
Example
class Dog extends Animal {
void sound() {
[Link]("Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Output: Barks
[Link](); // Output: This animal eats food.
}
}
Interface
An interface in Java is a reference type, similar to a class, that can
only contain abstract methods (by default), static, default, and
private methods, and constants (public static final). Used to
achieve 100% abstraction and multiple inheritance.
interface Animal {
void sound(); // abstract method by default
}
Example
Abstract Class vs Interface
Anonymous Classes
An anonymous class is a local inner class without a name,
used to instantiate a class or interface on the fly, typically for
one-time use.
interface Greeting {
void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = new Greeting() {
public void sayHello() {
[Link]("Hello, Ajit!");
}
};
[Link](); // Output: Hello, Ajit!
}
}
Method Definition in Java
A method in Java is a block of code that performs a specific
task when called. It is defined once and can be executed
(called) multiple times to reuse code and improve modularity.
Examples
Method with Return Type & Parameters
Method with Return Type & No Parameters
Method with No Return Type & Parameters
Example
Method with No Return Type & No Parameters
🧪 Complete Class with All 4 Types java
Method Overloading in Java
Method Overloading is a feature in Java that allows a class to have
more than one method with the same name, but different
parameters (either in number, type, or order).
It increases the readability of the program and allows us to perform a
similar operation in different ways.
Rules for Method Overloading:
You can overload a method by changing:
The number of parameters
The type of parameters
The order of parameters (if of different types)
You cannot overload by changing the return type alone.
Example of method overloading
public class Calculator {
// Method 1: add two integers
public int add(int a, int b) {
return a + b;
}
// Method 2: add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method 3: add two doubles
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]([Link](5, 3)); // Output: 8
[Link]([Link](1, 2, 3)); // Output: 6
[Link]([Link](4.5, 3.2)); // Output: 7.7
}}
Dealing with Static Members
Static members in Java (fields or methods) belong to the
class rather than an instance (object). They are shared
among all objects of the class.
Static Member Description
static variable Shared value across all instances
Can be called without creating an
static method
object
static block Used to initialize static variables
static class Only nested classes can be static
Example: Static Variable and Static Method java
Example: Static Block java
Use of “this” reference
this refers to the current object inside a class.
•Differentiate between instance and local variables
•Call another constructor in the same class (this(...))
•Pass current object as a parameter
•Return current object from a method
this = current class object reference.
Use of Modifiers with Classes & Methods
Modifiers in Java define access level, behavior, or properties of classes,
methods, and variables.
Use of Modifiers with Classes & Methods
Generic Class Types in Java
Generics allow you to write type-safe and reusable code by
defining a class or method that can operate on objects of any type.
class ClassName<T> {
T value;
void set(T val) {
value = val;
}
T get() {
return value;
}
}
Example: Generic Box Class
class Box<T> {
T item;
void setItem(T item) {
[Link] = item;
}
T getItem() {
return item;
}
}
public class Main {
public static void main(String[] args) {
Box<String> stringBox = new Box<>();
[Link]("Hello");
[Link]([Link]()); // Output: Hello
Box<Integer> intBox = new Box<>();
[Link](123);
[Link]([Link]()); // Output: 123
}
}
Benefits of Generics: