OOP USING JAVA REVISION NOTES
Module I – OOP and Introduction to Java
Introduction to OOPs
• OOP is a programming approach based on objects
• Objects represent real-world entities
• Combines data and methods in one unit
• Improves security, reusability, maintainability
Object Oriented Programming: Classes and Objects
• Class
• Blueprint or template
• Defines variables and methods
• No memory allocation
• Object
• Instance of a class
• Occupies memory
• Represents real-world entity
Encapsulation
• Wrapping data and methods together
• Achieves data hiding
• Improves security
• Implemented using classes and access specifiers
Abstraction
• Hides internal implementation
• Shows only essential features
• Reduces complexity
• Achieved using abstract classes and interfaces
Polymorphism
• One method, many forms
• Improves flexibility
• Types:
• Compile-time (method overloading)
• Run-time (method overriding)
Inheritance
• One class acquires properties of another class
• Promotes code reusability
• Implemented using extends keyword
Introduction of Java
• High-level programming language
• Object-oriented
• Platform independent
• Secure and robust
History of Java
• Developed by James Gosling
• Developed at Sun Microsystems
• Initially named Oak
How Java is Different from C++
• Java is platform independent
• C++ is platform dependent
• Java uses JVM
• C++ does not use JVM
• Java has automatic garbage collection
• C++ uses manual memory management
JDK Tools
• JDK stands for Java Development Kit
• Used to develop Java applications
• Contains compiler, JVM, and libraries
Class File
• Java source file compiled into .class file
• Contains bytecode
• Executed by JVM
Java Bytecode
• Intermediate code
• Platform independent
• Enables Java to run on any system
JVM
• JVM stands for Java Virtual Machine
• Executes bytecode
• Converts bytecode into machine code
• Provides platform independence
Identifiers
• Names given to variables, methods, and classes
• Cannot start with digit
• Cannot be Java keywords
• Case-sensitive
Data Types
• Define type of data stored in variable
• Primitive data types:
• int
• float
• double
• char
• boolean
Operators
• Used to perform operations
• Types:
• Arithmetic
• Relational
• Logical
• Assignment
• Unary
Control Statements
• Control flow of program
• Decision statements:
• if
• if-else
• switch
• Looping statements:
• for
• while
• do-while
Arrays
• Store multiple values of same data type
• Fixed size
• Index starts from 0
• Supports 1D and 2D arrays
Inheritance in Java
• Supported types:
• Single
• Multilevel
• Hierarchical
• Multiple inheritance not supported using classes
Multilevel Hierarchy
• One class derived from another derived class
• Forms inheritance chain
Method Overriding
• Same method name and parameters
• Different implementation
• Occurs at runtime
• Supports runtime polymorphism
Abstract Classes
• Cannot be instantiated
• May contain abstract and non-abstract methods
• Used for abstraction
Final Classes
• Declared using final keyword
• Cannot be inherited
• Improves security
OOP concepts range from concrete to abstract.
Concrete Abstract
Object Class Encapsulation Inheritance Polymorphism Abstraction
Instance of a class, Blueprint for objects, Wraps data and Acquires properties, One method, many Hides
occupies memory no memory methods, improves promotes reusability forms, improves implementation,
allocation security flexibility shows essential
features
Module II – Package and Exception in Java
Defining, Implementing and Applying Packages
A package is a collection of related classes and interfaces. It is used to organize large
programs, avoid name conflicts, and support access control.
Packages help in:
• Code reusability
• Better maintenance
• Logical grouping of classes
Importing Packages
Packages are accessed using the import [Link] a package is not imported, the fully
qualified name must be used.
Examples:
• Importing a specific class
• Importing all classes using wildcard *
Types of Packages
Built-in Packages
Provided by Java [Link]:
• [Link]
• [Link]
• [Link]
• [Link]
User Defined Packages
Created by programmers to group their own classes and interfaces.
User Defined Package
A user defined package is created using the package [Link] must be written at the top
of the source file.
Benefits:
• Improves modularity
• Avoids class name duplication
• Makes large projects manageable
Exception Handling in Java
An exception is an abnormal condition that occurs during program execution and disrupts
the normal flow of the program.
Examples:
• Division by zero
• Array index out of bounds
• File not found
try Block
The try block contains code that may cause an [Link] must be followed by at least one
catch block or a finally block.
catch Block
The catch block handles the exception thrown in the try [Link] exceptions can be
handled using different catch blocks.
finally Block
The finally block is always executed whether an exception occurs or [Link] is used for
cleanup operations such as closing files or releasing resources.
throw Keyword
The throw keyword is used to explicitly generate an [Link] can throw predefined or
user-defined exceptions.
throws Keyword
The throws keyword is used in method [Link] passes the responsibility of exception
handling to the calling method.
Uncaught Exceptions
If an exception is not handled using try-catch, it becomes an uncaught [Link] JVM
terminates the program and displays an error message.
Multiple Catch Blocks
Multiple catch blocks can be used with a single try block to handle different exceptions.
Rule:
• More specific exceptions must be caught before general exceptions.
Java’s Built-in Exceptions
Common built-in exceptions include:
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException
• NumberFormatException
• IOException
• ClassNotFoundException
Packages and Exceptions Impact Java Programs
Better Maintenance Logical Grouping
Easier to update code Classes are grouped
logically
Code Reusability Exception Handling
Reuse classes across Gracefully handle abnormal
projects conditions
Packages
Organize related classes
Module III – Constructor, Wrapper, String and
StringBuffer Class in Java
Constructors
• Special method used to initialize objects
• Constructor name same as class name
• No return type
• Automatically called when object is created
Various Types of Constructor
• Default Constructor
• Provided by Java if no constructor is defined
• Initializes object with default values
• Parameterized Constructor
• Accepts parameters
• Initializes object with specific values
• Copy Constructor
• Initializes object using another object
• Not directly supported, implemented manually
Role of Constructors in Inheritance
• Constructors are not inherited
• Parent class constructor executes first
• super() is used to call parent constructor
• Default super() is called automatically
Introduction to Wrapper Classes
• Wrapper classes convert primitive data types into objects
• Required for collection framework
• Present in [Link] package
Primitive to Wrapper Mapping
• int → Integer
• char → Character
• float → Float
• double → Double
• boolean → Boolean
String in Java
• String is a sequence of characters
• Stored as an object
• Part of [Link] package
Immutability of String
• String objects cannot be changed once created
• Any modification creates a new object
• Improves security and memory efficiency
Creating Strings
• Using string literal
• Using new keyword
• Stored in string constant pool or heap memory
String Operations
• length()
• charAt()
• equals()
• compareTo()
• substring()
• toUpperCase()
• toLowerCase()
StringBuffer Class
• Mutable sequence of characters
• Allows modification without creating new object
• Thread-safe
String vs StringBuffer
• String is immutable
• StringBuffer is mutable
• String is slower for modifications
• StringBuffer is faster for modifications
Creating and Initializing StringBuffer
• Created using StringBuffer class
• Supports append, insert, delete operations
Key Exam Points
• Constructor initializes object
• super() calls parent constructor
• Wrapper classes are objects
• String is immutable
• StringBuffer is mutable and thread-safe
Java Module III Challenges
String vs StringBuffer
Immutable vs mutable,
performance differences
StringBuffer Mutability
Allows modification
without new objects
String Operations
Various methods for
string manipulation
String Immutability
Cannot change strings
after creation
Wrapper Classes
Primitive to object
conversion
Inheritance Role
Constructors not
inherited, `super()` needed
Constructor Types
Default, parameterized,
and copy constructors
Constructors
Special methods for
object initialization
Module IV – Interface and Threads in Java
Interface
Defining Interfaces
• Interface is a blueprint of a class
• Contains abstract methods and constants
• Defined using interface keyword
• Methods are public by default
Abstract Methods in Interfaces
• Methods without body
• Must be implemented by implementing class
• Provide full abstraction
Implementing Interfaces
• A class implements interface using implements keyword
• Class must implement all abstract methods of interface
• Supports multiple inheritance in Java
Extending Interfaces
• One interface can extend another interface
• Uses extends keyword
• Supports multiple inheritance between interfaces
Interface References
• Interface reference can refer to object of implementing class
• Used to achieve runtime polymorphism
• Only interface methods can be accessed
Default Methods in Interfaces
• Introduced in Java 8
• Have method body
• Used to add new methods without breaking existing code
Static Methods in Interfaces
• Belong to interface itself
• Called using interface name
• Cannot be overridden
Constants in Interfaces
• All variables are public, static, and final by default
• Must be initialized at declaration
• Used to define constant values
Thread
Thread Life Cycle
• New
• Runnable
• Running
• Waiting / Blocked
• Terminated
Creating and Implementing Thread
• By extending Thread class
• By implementing Runnable interface
Multithreaded Programming
• Multiple threads execute simultaneously
• Improves CPU utilization
• Increases program performance
Thread Priorities
• Priority determines execution order
• Range: 1 (MIN) to 10 (MAX)
• Default priority is 5
Synchronization of Thread
• Controls access to shared resources
• Prevents data inconsistency
• Achieved using synchronized keyword
Resuming and Stopping Threads
• Used to control thread execution
• Methods: suspend(), resume(), stop()
• These methods are deprecated
Master interfaces and threads for robust Java
applications.
Advanced Java programming with
interfaces and threads
Confident in building concurrent, abstract
Java applications
Basic understanding of interfaces and
threads
Limited knowledge of Java concurrency
Module V – Applet and Graphics Programming
Applet Class
• Applet is a Java program that runs in a browser
• Part of [Link] package
• Does not use main() method
• Uses JVM inside browser
Life Cycle of Applet
• init() – called once, initialization
• start() – starts execution
• paint() – displays output
• stop() – stops execution
• destroy() – applet removed from memory
Creating an Executable Applet
• Applet class extends Applet
• Compiled using javac
• Executed using browser or appletviewer
Adding Applet to HTML File
• Applet embedded using <applet> tag
• Specifies class name, width, height
• HTML file loads applet in browser
Graphics Class
• Used for drawing shapes
• Part of [Link] package
• Object obtained in paint() method
Drawing Shapes Using Graphics
• drawLine()
• drawRect()
• drawOval()
• drawArc()
• drawPolygon()
• fillRect()
• fillOval()
Using Control Loops in Applet
• Loops used to repeat drawing
• Common loops: for, while
• Used for patterns and animations
Introduction to AWT Package
• AWT stands for Abstract Window Toolkit
• Provides GUI components
• Includes buttons, labels, text fields
Layout Managers
• Control component placement
• Types:
• FlowLayout
• BorderLayout
• GridLayout
• CardLayout
How to create and manage applets and graphics in Java?
Graphics Class
AWT Package
Learn how to use the
Applet Class
Graphics class to Explore AWT for GUI
Understand the draw various shapes. Control Loops components and
basics of applets, layout managers.
their lifecycle, and Use loops to repeat
how to create them. drawing operations
for patterns and
animations.
MCQ-FOCUSED QUICK REVISION (ALL MODULES
COMBINED)
OOP & Java Basics
• OOP focuses on objects
• Four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
• Class = blueprint
• Object = instance
• Java is platform independent
• Java uses JVM
• .java → source file
• .class → bytecode file
• JVM executes bytecode
Inheritance & Polymorphism
• extends keyword used for inheritance
• Java does not support multiple inheritance using classes
• Method overriding supports runtime polymorphism
• Final class cannot be inherited
• Abstract class object cannot be created
Packages
• Package = group of related classes
• package keyword creates package
• import keyword accesses package
• [Link] imported by default
• User-defined packages improve modularity
Exception Handling
• Exception = runtime error
• try block contains risky code
• catch handles exception
• finally always executes
• throw explicitly throws exception
• throws passes responsibility
• Uncaught exception terminates program
String & Wrapper
• String is immutable
• StringBuffer is mutable
• Wrapper classes convert primitive to object
• Wrapper classes in [Link]
Interface & Thread
• Interface supports multiple inheritance
• Interface methods are public by default
• Default methods introduced in Java 8
• Thread created by extending Thread or implementing Runnable
• Thread priority range: 1 to 10
• Synchronization prevents data inconsistency
Applet & Graphics
• Applet does not use main()
• Applet lifecycle methods are important MCQs
• Graphics class used for drawing
• AWT used for GUI
• Layout managers control component arrangement
ALL THE BEST! You Got It.