JavaFX Application Structure
JavaFX Application Structure
Fast answer
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage primaryStage) {
// Create a label
Label label = new Label("Welcome to JavaFX");
// Create scene
Scene scene = new Scene(root, 400, 300);
// Main method
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/63
public static void main(String[] args) {
launch(args);
}
}
Explanation of Components
1. Import Statements
Java
import [Link];
import [Link];
import [Link];
Java
public class HelloJavaFX extends Application
Java
public void start(Stage primaryStage)
4. Stage
Java
[Link]("JavaFX Application");
Functions:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/63
Set title
Set scene
Show window
5. Scene
Java
Scene scene = new Scene(root, 400, 300);
Width = 400
Height = 300
6. Layout Pane
Java
StackPane root = new StackPane();
Common layouts:
StackPane
VBox
HBox
GridPane
BorderPane
7. Controls
Java
Label label = new Label("Welcome to JavaFX");
Label
Button
TextField
CheckBox
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/63
RadioButton
Java
[Link]();
Structure Diagram
Application
|
|--> Stage (Window)
|
|--> Scene
|
|--> Layout Pane
|
|--> Controls
Advantages of JavaFX
Rich GUI controls
CSS styling support
Animation and multimedia support
Cross-platform compatibility
Modern UI design
Summary
A JavaFX application mainly contains:
Component Purpose
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/63
Component Purpose
Layout Panes
Button
Label
TextField
CheckBox
Instead of placing components manually, layout panes automatically manage their positions.
Example
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/63
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link]("VBox Example");
[Link](scene);
[Link]();
}
Output Arrangement
Button 1
Button 2
Button 3
Example
Java
HBox root = new HBox(10);
Output Arrangement
Button1 Button2 Button3
3. BorderPane
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/63
BorderPane divides the window into 5 regions:
Top
Bottom
Left
Right
Center
Example
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link](new Button("Top"));
[Link](new Button("Bottom"));
[Link](new Button("Left"));
[Link](new Button("Right"));
[Link](new Button("Center"));
[Link](scene);
[Link]("BorderPane Example");
[Link]();
}
Structure
Top
Left Center Right
Bottom
4. GridPane
GridPane arranges controls in rows and columns like a table.
Example
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/63
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link](10);
[Link](10);
[Link](scene);
[Link]("GridPane Example");
[Link]();
}
Structure
Name: [TextField]
Password: [TextField]
5. StackPane
StackPane places components on top of each other.
Example
Java
StackPane root = new StackPane();
[Link]().addAll(
new Button("Button"),
new Label("Label")
);
6. FlowPane
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/63
FlowPane arranges components like text flow.
Example
Java
FlowPane root = new FlowPane();
[Link]().addAll(
new Button("One"),
new Button("Two"),
new Button("Three")
);
7. AnchorPane
AnchorPane anchors controls to window edges.
Example
Java
Button btn = new Button("Submit");
[Link](btn, 20.0);
[Link](btn, 30.0);
[Link]().add(btn);
Responsive interface
Better screen management
VBox Vertical
HBox Horizontal
BorderPane Five regions
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/63
Layout Arrangement
Conclusion
Layout panes are very important in JavaFX because they help organize GUI components properly.
Different layout panes are used for different types of user interfaces.
UI Components
UI Components in JavaFX
UI Components are the graphical elements used to interact with the user in a JavaFX application.
Examples:
Button
Label
TextField
CheckBox
RadioButton
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/63
Component Purpose
1. Label
A Label is used to display text.
Example
Java
Label label = new Label("Welcome to JavaFX");
Complete Program
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link](scene);
[Link]("Label Example");
[Link]();
}
2. Button
A Button performs an action when clicked.
Example
Java
Button btn = new Button("Click Me");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/63
Button with Event
Java
[Link](e -> {
[Link]("Button Clicked");
});
3. TextField
A TextField accepts single-line input.
Example
Java
TextField tf = new TextField();
Get Text
Java
String name = [Link]();
4. PasswordField
A PasswordField hides typed characters.
Example
Java
PasswordField pf = new PasswordField();
5. TextArea
A TextArea accepts multi-line text.
Example
Java
TextArea ta = new TextArea();
Used for:
Comments
Notes
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/63
Messages
6. CheckBox
A CheckBox allows multiple selections.
Example
Java
CheckBox c1 = new CheckBox("Java");
CheckBox c2 = new CheckBox("Python");
7. RadioButton
A RadioButton allows only one selection from a group.
Example
Java
RadioButton r1 = new RadioButton("Male");
RadioButton r2 = new RadioButton("Female");
[Link](tg);
[Link](tg);
8. ComboBox
A ComboBox provides a drop-down list.
Example
Java
ComboBox<String> cb = new ComboBox<>();
9. ListView
A ListView displays a list of items.
Example
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/63
Java
ListView<String> list = new ListView<>();
10. TableView
A TableView displays data in table form.
Example
Java
TableView table = new TableView();
Used for:
Student records
Employee data
Database display
11. MenuBar
A MenuBar creates menus in applications.
Example
Java
MenuBar mb = new MenuBar();
[Link]().add(open);
[Link]().add(file);
12. ImageView
ImageView displays images.
Example
Java
Image img = new Image("file:[Link]");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/63
Example Program Using Multiple Components
Java
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link](e -> {
[Link]("Hello " + [Link]());
});
[Link]("UI Components");
[Link](scene);
[Link]();
}
Modern appearance
Cross-platform support
Conclusion
UI components are the main building blocks of JavaFX GUI applications. They help users interact with the
application through buttons, text fields, menus, lists, and other controls.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/63
Color and Font Classes in JavaFX
JavaFX provides Color and Font classes to improve the appearance of GUI applications.
Text
Background
Shapes
Buttons
Labels
Import Statement
Java
import [Link];
Java
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/63
@Override
public void start(Stage stage) {
[Link](scene);
[Link]("Color Example");
[Link]();
}
Syntax
Java
[Link](red, green, blue)
Example
Java
[Link]([Link](255, 0, 0));
Java
[Link]("-fx-background-color: lightblue;");
Font family
Font size
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/63
Font style
Import Statement
Java
import [Link];
import [Link];
Creating Font
Syntax
Java
[Link]("Arial", 20)
Java
Label label = new Label("JavaFX Font");
[Link]([Link]("Arial", 25));
Font Styles
JavaFX supports:
Bold
Italic
Regular
Java
[Link]([Link]("Verdana", [Link], 20));
Java
import [Link];
import [Link];
import [Link];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/63
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
// Set font
[Link]([Link]("Arial", [Link], 30));
[Link]("-fx-background-color: lightyellow;");
[Link](scene);
[Link]();
}
Output Features
The program displays:
Font size 30
Light yellow background
Important Methods
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/63
Color Methods
Method Purpose
Font Methods
Method Purpose
[Link]() Creates font
setFont() Applies font
Conclusion
The Color and Font classes are important in JavaFX for designing attractive GUI applications. They help
improve the appearance, readability, and user experience of the application.
Event
Handling
Button click
Mouse click
Keyboard press
Mouse movement
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/63
Read user input
Control GUI behavior
Example:
Java
[Link](e -> {
// Code to execute
});
e → Event object
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
// Event Handling
[Link](e -> {
[Link]("Button Clicked");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/63
});
[Link](scene);
[Link]("Event Handling");
[Link]();
}
2. ActionEvent is generated
3. Event handler executes
4. Message prints on console
Java
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void start(Stage stage) {
[Link](e -> {
[Link]("Button Clicked");
});
[Link]().addAll(label, btn);
[Link](scene);
[Link]("Event Demo");
[Link]();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/63
Mouse Events
Mouse events occur when user interacts using mouse.
Examples:
Mouse click
Mouse move
Mouse enter
Java
[Link](e -> {
[Link]("Mouse Clicked");
});
Keyboard Events
Keyboard events occur when user presses keys.
Examples:
Key pressed
Key released
Java
[Link](e -> {
[Link]("Key Pressed");
});
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/63
Example
Java
import [Link];
import [Link];
@Override
public void handle(ActionEvent e) {
[Link]("Button Clicked");
}
}
Use:
Java
[Link](new MyHandler());
Important Points
Events are generated by user actions.
Event handlers contain code to respond to events.
setOnAction() is commonly used for buttons.
Lambda expressions make code shorter and easier.
Conclusion
Event handling is one of the most important features of JavaFX. It allows GUI applications to respond to
user actions like button clicks, mouse events, and keyboard events, making the application interactive
and user-friendly.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/63
I/O Management and Generics:, File class, Writing, reading and Random
Access files, Purpose of Generics, Defining Generic classes and methods,
Generics with Collections like ArrayList, HashMap, etc
Java
[Link] package
2. File Class
File class is used to represent a file or folder path.
It does not read or write data directly.
It is used to create, delete, check, and get information about files.
Import Statement
Java
import [Link];
Example
Java
import [Link];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/63
[Link]("Path: " + [Link]());
[Link]("Exists: " + [Link]());
[Link]("Can read: " + [Link]());
[Link]("Can write: " + [Link]());
}
}
Example
Java
import [Link];
import [Link];
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Name: Dilip\n");
[Link]("Subject: OOP in Java\n");
[Link]();
Explanation
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 26/63
FileWriter opens file for writing.
write() writes data into file.
close() closes the file.
try-catch handles file errors.
Example
Java
import [Link];
import [Link];
try {
FileReader fr = new FileReader("[Link]");
int ch;
[Link]();
}
catch (IOException e) {
[Link]("File reading error.");
}
}
}
Explanation
FileReader opens file for reading.
read() reads one character at a time.
-1 means end of file.
close() closes the file.
Import Statement
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 27/63
Java
import [Link];
Modes
Mode Meaning
Example
Java
import [Link];
try {
RandomAccessFile raf = new RandomAccessFile("[Link]", "rw");
[Link]("Hello Java");
[Link](0);
[Link](data);
[Link]();
}
catch (Exception e) {
[Link]("Error occurred.");
}
}
}
Important Methods
Method Use
seek(position) Moves file pointer
read() Reads data
write() Writes data
6. Generics in Java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 28/63
Generics allow us to write code that can work with any data type.
It provides type safety.
Without Generics
Java
ArrayList list = new ArrayList();
[Link]("Java");
[Link](10);
With Generics
Java
ArrayList<String> list = new ArrayList<String>();
[Link]("Java");
[Link]("Python");
7. Purpose of Generics
Generics are used to:
8. Generic Class
A generic class can work with different data types.
Syntax
Java
class ClassName<T> {
T variable;
}
Java
class Box<T> {
T value;
T getValue() {
return value;
}
}
9. Generic Method
A generic method can accept any type of data.
Syntax
Java
<T> void methodName(T value) {
// code
}
Example
Java
public class GenericMethodDemo {
display(10);
display("Java");
display(25.5);
}
}
Example
Java
import [Link];
[Link]("Dilip");
[Link]("Rahul");
[Link]("Amit");
Benefit
Only String values can be stored in this list.
Example
Java
import [Link];
[Link](1, "Dilip");
[Link](2, "Rahul");
[Link](3, "Amit");
[Link]([Link](1));
}
}
Explanation
Integer is key type.
String is value type.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 31/63
put() adds data.
get() gets data.
Conclusion
I/O management is used to read and write data in files. Java provides classes like File, FileReader,
FileWriter,and RandomAccessFile. Generics are used to make Java programs type-safe, reusable, and
easy to maintain. Generics are commonly used with collections like ArrayList and HashMap.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 32/63
1. byte
byte stores small integer values.
Example
Java
byte b = 100;
[Link](b);
Range:
-128 to 127
2. short
short stores larger integer values than byte.
Example
Java
short s = 2000;
[Link](s);
3. int
int is most commonly used integer type.
Example
Java
int num = 50000;
[Link](num);
4. long
long stores very large integer values.
Example
Java
long mobile = 9876543210L;
[Link](mobile);
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 33/63
L is written at end.
5. float
float stores decimal numbers.
Example
Java
float marks = 85.5f;
[Link](marks);
f is required.
6. double
double stores large decimal values.
Example
Java
double pi = 3.14159;
[Link](pi);
7. char
char stores single character.
Example
Java
char grade = 'A';
[Link](grade);
8. boolean
boolean stores only:
true
false
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 34/63
Example
Java
boolean result = true;
[Link](result);
9. String
String stores group of characters.
It is a non-primitive data type.
Example
Java
String name = "Dilip";
[Link](name);
10. Arrays
Array stores multiple values of same type.
Example
Java
int arr[] = {10, 20, 30, 40};
[Link](arr[0]);
Output:
10
2. Variables in Java
Variable is a named memory location used to store data.
Syntax
Java
datatype variableName = value;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 35/63
Example
Java
int age = 20;
Types of Variables
Variable Type Description
Local Variable Declared inside method
Instance Variable Declared inside class
Static Variable Shared by all objects
Example
Java
class Student {
void display() {
[Link](roll);
[Link](college);
[Link](marks);
}
}
3. Keywords in Java
Keywords are reserved words with special meaning.
Keywords cannot be used as variable names.
Common Keywords
Keyword Use
class Declares class
int Integer type
if Conditional statement
for Loop
public Access modifier
static Common memory
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 36/63
Keyword Use
return Returns value
new Creates object
Example
Java
public class Demo {
}
Here:
public
class
are keywords.
4. Literals in Java
Literal is a fixed value written directly in program.
Types of Literals
Literal Type Example
Integer Literal 10
Floating Literal 12.5
Example
Java
int x = 10;
char ch = 'A';
String s = "Java";
Here:
10
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 37/63
'A'
"Java"
are literals.
5. Operators in Java
Operators perform operations on data.
Types of Operators
Operator Type Example
Arithmetic +-*/%
Relational > < == !=
Logical && || !
Assignment = += -=
Unary ++ --
Bitwise &|^
Ternary ?:
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example
Java
int a = 10;
int b = 3;
[Link](a + b);
[Link](a % b);
Relational Operators
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 38/63
Used for comparison.
Operator Meaning
> Greater than
< Less than
== Equal
!= Not equal
Logical Operators
Operator Meaning
&& AND
|| OR
! NOT
Unary Operators
Operator Meaning
++ Increment
-- Decrement
6. Operator Precedence
Operator precedence decides which operator executes first.
Example
Java
int x = 5 + 3 * 2;
5 + 6 = 11
Output:
11
Lowest =
7. Associativity
Associativity decides evaluation direction when operators have same precedence.
Associativity Direction
Left to Right +-*/
Right to Left =
Example
Java
int x = 10 - 5 + 2;
Left to right:
10 - 5 = 5
5 + 2 = 7
Output:
8. Type Conversion
Type conversion changes one data type into another.
Two types:
1. Implicit Conversion
2. Explicit Conversion
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 40/63
Example
Java
int x = 10;
double y = x;
[Link](y);
Output:
10.0
Example
Java
double d = 25.75;
int x = (int)d;
[Link](x);
Output:
25
Conclusion
Java provides different data types to store various kinds of data. Variables are used to store values in
memory. Operators perform calculations and comparisons. Operator precedence and associativity
control expression evaluation. Type conversion allows conversion between different data types.
Conditional and looping statements: simple if, if-else statement, else-if ladder,
switch-case statement, for loop, while loop, do-while loop, Enhanced for Loop
(for-each loop), break, continue, labelled break, labelled continue
Make decisions
Repeat statements
Control loops
1. Conditional Statements
Conditional statements are used to execute code based on conditions.
Types:
1. simple if
2. if-else
3. else-if ladder
4. switch-case
1. Simple if Statement
if statement executes code only when condition is true.
Syntax
Java
if(condition) {
// code
}
Example
Java
public class IfDemo {
public static void main(String[] args) {
Output
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 42/63
Eligible for voting
2. if-else Statement
if-else executes one block if condition is true and another block if false.
Syntax
Java
if(condition) {
// true block
}
else {
// false block
}
Example
Java
public class IfElseDemo {
public static void main(String[] args) {
int num = 5;
if(num % 2 == 0) {
[Link]("Even Number");
}
else {
[Link]("Odd Number");
}
}
}
Output
Odd Number
3. else-if Ladder
Used when multiple conditions are checked.
Syntax
Java
if(condition1) {
}
else if(condition2) {
}
else {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 43/63
}
Example
Java
public class ElseIfDemo {
public static void main(String[] args) {
Output
Grade B
4. switch-case Statement
switch is used to select one option from many choices.
Syntax
Java
switch(expression) {
case value:
// code
break;
default:
// code
}
Example
Java
public class SwitchDemo {
public static void main(String[] args) {
int day = 2;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 44/63
switch(day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid Day");
}
}
}
Output
Tuesday
2. Looping Statements
Loops are used to repeat code multiple times.
Types:
1. for loop
2. while loop
3. do-while loop
4. Enhanced for loop
1. for Loop
for loop is used when number of repetitions is known.
Syntax
Java
for(initialization; condition; increment/decrement) {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 45/63
// code
}
Example
Java
public class ForDemo {
public static void main(String[] args) {
Output
1
2
3
4
5
2. while Loop
while loop checks condition first.
Syntax
Java
while(condition) {
// code
}
Example
Java
public class WhileDemo {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
[Link](i);
i++;
}
}
}
3. do-while Loop
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 46/63
do-while executes at least one time.
Syntax
Java
do {
// code
} while(condition);
Example
Java
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
[Link](i);
i++;
} while(i <= 5);
}
}
Syntax
Java
for(datatype variable : array) {
// code
}
Example
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 47/63
Java
public class ForEachDemo {
public static void main(String[] args) {
for(int x : arr) {
[Link](x);
}
}
}
Output
10
20
30
40
3. break Statement
break immediately terminates loop or switch.
Example
Java
for(int i = 1; i <= 10; i++) {
if(i == 5) {
break;
}
[Link](i);
}
Output
1
2
3
4
4. continue Statement
continue skips current iteration and moves to next iteration.
Example
Java
for(int i = 1; i <= 5; i++) {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 48/63
if(i == 3) {
continue;
}
[Link](i);
}
Output
1
2
4
5
5. Labelled break
Used to terminate outer loop from inner loop.
Example
Java
public class LabelBreakDemo {
public static void main(String[] args) {
outer:
if(i == 2 && j == 2) {
break outer;
}
6. Labelled continue
Used to skip iteration of outer loop.
Example
Java
public class LabelContinueDemo {
public static void main(String[] args) {
outer:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 49/63
if(j == 2) {
continue outer;
}
Comparison of Loops
Loop Condition Check Minimum Execution
for Beginning 0
while Beginning 0
do-while End 1
Conclusion
Conditional statements are used for decision making, while looping statements are used for repeating
code execution. Java provides different control statements like if, switch, for, while, do-while, break, and
continue to control program flow effectively.
Objects
Classes
Data security
Reusability
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 50/63
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
1. Class
A class is a blueprint or template used to create objects.
It contains:
Syntax
Java
class ClassName {
// data members
// methods
}
Example
Java
class Student {
int rollNo;
String name;
void display() {
[Link](rollNo + " " + name);
}
}
2. Object
Object is an instance of class.
It occupies memory and can access class members.
Syntax
Java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 51/63
ClassName objectName = new ClassName();
Example
Java
Student s1 = new Student();
[Link] = 101;
[Link] = "Dilip";
[Link]();
3. Data Encapsulation
Encapsulation means wrapping:
Data members
Member methods
Example
Java
class Employee {
Advantages
Data security
Controlled access
Better maintenance
4. Data Abstraction
Abstraction means hiding implementation details and showing only necessary information.
Example:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 52/63
ATM machine
Mobile phone
Achieved By
Abstract class
Interface
5. Access Specifiers
Access specifiers control visibility of members.
Example
Java
class Demo {
Example
Java
class Student {
int roll;
static String college = "GTU";
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 53/63
7. Static Member Function
Static method belongs to class.
It can access only static members directly.
Example
Java
class Test {
Java
[Link]();
8. Constructor
Constructor is special method used to initialize objects.
Rules:
Types of Constructors
1. Default constructor
2. Parameterized constructor
Default Constructor
Java
class Demo {
Demo() {
[Link]("Default Constructor");
}
}
Parameterized Constructor
Java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 54/63
class Student {
int id;
Student(int x) {
id = x;
}
}
9. References in Java
Object variable stores reference of object.
Example
Java
Student s1 = new Student();
Here:
s1 is reference variable.
Example
Java
class Student {
int id;
Student(int id) {
[Link] = id;
}
void display() {
[Link](id);
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 55/63
Example
Java
class Test {
int x = 10;
Test create() {
return this;
}
}
Example
Java
class Test {
int x = 10;
void display(Test t) {
[Link](t.x);
}
}
Example
Java
class Student {
int id;
Student(int id) {
[Link] = id;
}
void display() {
[Link](id);
}
for(Student s : arr) {
[Link]();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 56/63
}
}
Example
Java
class Demo {
static {
[Link]("Main Method");
}
}
Output
Static Block Executed
Main Method
Example
Java
class Demo {
[Link]("Instance Block");
}
Demo() {
[Link]("Constructor");
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 57/63
Output
Instance Block
Constructor
Example
Java
class Outer {
class Inner {
void display() {
[Link]("Inner Class");
}
}
[Link]();
}
}
Advantages of OOP
Code reusability
Data security
Easy maintenance
Better program structure
Real-world modeling
Conclusion
OOP is a powerful programming approach used in Java. Concepts like classes, objects, encapsulation,
abstraction, constructors, static members, and inner classes help in developing secure, reusable, and
organized programs.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 58/63
Method Overloading, Method Overriding and Dynamic Binding in
Java
These are important concepts of Object Oriented Programming (OOP) in Java.
They help achieve:
Polymorphism
Flexibility
Code reusability
1. Method Overloading
Method overloading means:
Number of parameters
OR
Type of parameters
OR
Order of parameters
Java
class Addition {
[Link]([Link](10, 20));
[Link]([Link](5.5, 2.5));
}
}
Output
30
60
8.0
2. Method Overriding
Method overriding means:
Java
class Animal {
void sound() {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 60/63
[Link]("Animal makes sound");
}
}
@Override
void sound() {
[Link]("Dog barks");
}
[Link]();
}
}
Output
Dog barks
Another Example
Java
class Vehicle {
void run() {
[Link]("Vehicle is running");
}
}
void run() {
[Link]("Bike is running");
}
[Link]();
}
}
Output
Bike is running
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 61/63
3. Dynamic Binding
Dynamic binding means method call is resolved at runtime.
It is also called:
Late binding
Runtime binding
Java
class Parent {
void display() {
[Link]("Parent class method");
}
}
void display() {
[Link]("Child class method");
}
[Link]();
}
}
Output
Child class method
Explanation
Java
Parent p = new Child();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 62/63
Method Overloading Method Overriding
Same class usually Requires inheritance
Different parameters Same parameters
Compile-time polymorphism Runtime polymorphism
Faster execution Slightly slower
No inheritance needed Inheritance required
Important Points
Overloading increases method flexibility.
Overriding changes parent class behavior.
Dynamic binding occurs in overridden methods.
@Override annotation improves readability.
Conclusion
Method overloading allows multiple methods with same name but different parameters. Method
overriding allows child class to provide its own implementation of parent class method. Dynamic binding
helps Java decide method execution at runtime, which supports runtime polymorphism.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 63/63