SlideShare a Scribd company logo
Individual Recitation
2
Which of the following is NOT a principle
of Object-Oriented Programming?
A) Encapsulation
B) Abstraction
C) Inheritance
D) Multithreading
3
Answer and Explanation
D) Multithreading
Multithreading is a concept related to concurrent
programming, not strictly a principle of OOP.
4
In Java, which keyword is used to
implement inheritance between classes?
A) this
B) extends
C) implements
D) inherit
5
Answer and Explanation
B) extends
The keyword "extends" is used to establish an
inheritance relationship between classes in Java.
6
Which OOP concept describes the ability
of an object to take many forms?
A) Polymorphism
B) Abstraction
C) Inheritance
D) Encapsulation
7
Answer and Explanation
A) Polymorphism
Polymorphism allows objects to be treated as
instances of their parent class, enabling flexibility
in programming.
8
Which access modifier allows a variable
or method to be accessible only within its
own class?
A) public
B) private
C) protected
D) default (no modifier)
9
Answer and Explanation
B) private
Private access modifier restricts access to
members within the same class only.
10
Which access modifier allows a variable
or method to be accessible only within its
own class?
A) public
B) private
C) protected
D) default (no modifier)
11
Answer and Explanation
B) private
Private access modifier restricts access to
members within the same class only.
12
Which OOP concept refers to bundling
data and methods that operate on the data
into a single unit?
A) Encapsulation
B) Inheritance
C) Polymorphism
D) Abstraction
13
Answer and Explanation
A) Encapsulation
Encapsulation hides the internal state of an
object and restricts access to it.
14
Which method is automatically called
when an object is created in Java?
A) init()
B) start()
C) main()
D) constructor()
15
Answer and Explanation
D) constructor()
Constructors initialize objects when they are
created.
16
Which OOP concept allows a class to
have multiple methods with the same
name but different parameters?
A) Overriding
B) Overloading
C) Polymorphism
D) Encapsulation
17
Answer and Explanation
B) Overloading
Overloading enables a class to have multiple
methods with the same name but different
parameter lists.
18
Which keyword is used to refer to the
current instance of a class in Java?
A) that
B) self
C) this
D) here
19
Answer and Explanation
C) this
The "this" keyword refers to the current
instance of the class.
20
Which OOP principle describes the
process of hiding the implementation
details of an object?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
21
Answer and Explanation
D) Abstraction
Abstraction focuses on hiding the internal
implementation details of objects and showing
only the necessary features.
22
Which keyword is used to indicate that a
method does not return any value in
Java?
A) void
B) null
C) none
D) empty
23
Answer and Explanation
A) void
The "void" keyword is used in method
declarations to indicate that the method does not
return any value.
24
Which OOP concept allows a class to
inherit attributes and methods from
another class?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
25
Answer and Explanation
C) Inheritance
Inheritance enables the creation of a new class
that adopts the properties and behaviors of an
existing class.
26
Which OOP principle describes the ability
of objects to share behaviors or
characteristics with other objects?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
27
Answer and Explanation
A) Inheritance
Inheritance allows objects to acquire properties
and behaviors from parent objects, facilitating
code reuse and organization.
28
Which OOP principle describes the ability
of objects to share behaviors or
characteristics with other objects?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
29
Answer and Explanation
A) Inheritance
Inheritance allows objects to acquire properties
and behaviors from parent objects, facilitating
code reuse and organization.
30
What is the process of defining a new
class based on an existing class, thereby
inheriting its attributes and methods?
A) Composition
B) Polymorphism
C) Inheritance
D) Encapsulation
31
Answer and Explanation
C) Inheritance
Inheritance allows for the creation of new
classes that acquire properties and behaviors
from existing classes.
32
Which OOP concept allows a class to
have multiple methods with the same
name and parameter list, but different
functionality?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
33
Answer and Explanation
C) Polymorphism
Polymorphism allows methods to be overridden
in subclasses, providing different
implementations while using the same method
signature.
34
What does the term "method
overloading" refer to in Java?
A) Defining a method with the same name in
different classes
B) Defining multiple methods with the same
name but different parameter lists in the same
class
C) Calling a method from within another method
D) Overriding a method in a subclass
35
Answer and Explanation
B) Defining multiple methods with the same
name but different parameter lists in the same
class
Method overloading allows a class to have
multiple methods with the same name but
different parameter lists.
36
Which OOP concept allows a class to hide
the implementation details of its methods
while still providing access to their
functionality?
A) Inheritance
B) Polymorphism
C) Abstraction
D) Encapsulation
37
Answer and Explanation
C) Abstraction
Abstraction focuses on hiding the
implementation details of methods and showing
only the essential features.
38
Which access modifier provides the
widest accessibility in Java, allowing a
variable or method to be accessed from
anywhere?
A) public
B) private
C) protected
D) default (no modifier)
39
Answer and Explanation
A) public
The "public" access modifier allows a variable
or method to be accessed from any class.
40
What is the purpose of the "this"
keyword in Java?
A) It refers to the current instance of the class.
B) It references the superclass.
C) It indicates that a method does not return any
value.
D) It is used to prevent a class from being
inherited.
41
Answer and Explanation
A) It refers to the current instance of the class.
The "this" keyword is used to refer to the
current instance of the class.
42
In Java, what does the term
"encapsulation" refer to?
A) It refers to the process of hiding the
implementation details of a class.
B) It allows a class to inherit attributes and methods
from another class.
C) It describes the ability of objects to share
behaviors or characteristics with other objects.
D) It involves bundling data and methods that
operate on the data into a single unit.
43
Answer and Explanation
D) It involves bundling data and methods that
operate on the data into a single unit.
Encapsulation hides the internal state of an
object and restricts access to it, while bundling
data and methods together.
44
Which OOP concept allows a class to
provide different implementations of
methods that are called by the same
name?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
45
Answer and Explanation
C) Polymorphism
Polymorphism allows for the implementation of
methods to vary depending on the object that
invokes them.
46
What is the purpose of the "extends"
keyword in Java?
A) It is used to prevent a class from being inherited.
B) It is used to establish an inheritance relationship
between classes.
C) It allows a class to provide multiple
implementations of a method.
D) It is used to declare a method that does not return
any value.
47
Answer and Explanation
B) It is used to establish an inheritance
relationship between classes.
The "extends" keyword is used to create a
subclass that inherits properties and behaviors
from a superclass.
48
Which keyword is used to create an
object of a class in Java?
A) new
B) class
C) object
D) instance
49
Answer and Explanation
A) new
The "new" keyword is used to instantiate an
object of a class in Java.
50
In Java, what is the purpose of the
"default" access modifier?
A) It allows access to the variable or method only
within the same package.
B) It restricts access to the variable or method to only
subclasses.
C) It makes the variable or method accessible from
anywhere in the program.
D) It hides the variable or method from being
accessed outside the class.
51
Answer and Explanation
A) It allows access to the variable or method only
within the same package.
The "default" access modifier allows access
within the same package but not outside of it.
52
Which OOP concept allows a class to
implement multiple interfaces in Java?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Interface
53
Answer and Explanation
D) Interface
Interfaces in Java allow a class to implement
multiple behaviors, enabling polymorphism.
54
What is the term for the process of hiding the
internal state of an object and requiring all
interactions to occur through well-defined
interfaces?
A) Abstraction
B) Polymorphism
C) Inheritance
D) Overriding
55
Answer and Explanation
A) Abstraction
Abstraction involves hiding internal details and
exposing only necessary features through well-
defined interfaces.
56
In Java, what is the purpose of the "implements" keyword?
A) It is used to establish an inheritance relationship
between classes.
B) It allows a class to define multiple implementations
of a method.
C) It is used to indicate that a class is a subclass of
another class.
D) It is used to declare that a class implements an
interface.
57
Answer and Explanation
D) It is used to declare that a class implements
an interface.
The "implements" keyword is used to indicate
that a class implements one or more interfaces.
58
Which OOP concept allows different classes to share
some common functionality or behavior?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
59
Answer and Explanation
A) Inheritance
Inheritance allows classes to share properties
and behaviors from a common superclass.
60
What is the term for the process of defining a new class
based on an existing class, thereby inheriting its attributes
and methods?
A) Composition
B) Polymorphism
C) Inheritance
D) Encapsulation
61
Answer and Explanation
C) Inheritance
Inheritance allows for the creation of new classes
that acquire properties and behaviors from
existing classes.
62
What is the term for the process of defining a new class
based on an existing class, thereby inheriting its attributes
and methods?
A) Composition
B) Polymorphism
C) Inheritance
D) Encapsulation
63
Answer and Explanation
C) Inheritance
Inheritance allows for the creation of new classes
that acquire properties and behaviors from
existing classes.
64
Which OOP concept allows a class to provide
different implementations of methods based on their
parameter types?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
65
Answer and Explanation
C) Polymorphism
Polymorphism in Java allows methods to be
invoked with different arguments, resulting in
different behavior.
66
Which access modifier restricts access to the variable or
method to only subclasses and classes within the same
package?
A) private
B) public
C) protected
D) default (no modifier)
67
Answer and Explanation
C) protected
The "protected" access modifier allows access
to subclasses and classes within the same
package, but not from other packages.
68
What is the purpose of the "abstract" keyword
in Java?
A) It is used to create abstract classes.
B) It is used to prevent a class from being
instantiated.
C) It indicates that a method must be overridden in
subclasses.
D) It allows a class to define multiple
implementations of a method.
69
Answer and Explanation
A) It is used to create abstract classes.
The "abstract" keyword is used to declare
abstract classes and methods in Java.
70
Which OOP concept describes the ability of objects
to share behaviors or characteristics with other
objects through a common interface?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
71
Answer and Explanation
A) Inheritance
Inheritance allows objects to share properties
and behaviors through a common superclass.
72
What is the term for the ability of a method to be defined in a
superclass and then redefined in a subclass?
A) Overriding
B) Overloading
C) Polymorphism
D) Inheritance
73
Answer and Explanation
A) Overriding
Method overriding allows a subclass to provide
a specific implementation of a method that is
already defined in its superclass.
74
What is the term for the ability of a method to be defined in a
superclass and then redefined in a subclass?
A) Overriding
B) Overloading
C) Polymorphism
D) Inheritance
75
Answer and Explanation
A) Overriding
Method overriding allows a subclass to provide
a specific implementation of a method that is
already defined in its superclass.
Java OOP
Introduction to UML
77
UML (Unified Modeling Language)
• Standardized general-purpose modeling
language in the field of software engineering.
• It provides a way to visualize a system's
architecture, helping developers understand the
system, create its design, and document its
architecture.
78
History of UML:
• Late 1980s to early 1990s: Software engineering had several
competing methods for modeling systems, which made it difficult to
communicate ideas between developers. This led to the development
of UML.
• Early 1990s: Grady Booch, James Rumbaugh, and Ivar Jacobson
were the principal developers of UML, although they were working
separately at Rational Software, Hewlett-Packard, and Objectory AB,
respectively.
• 1994: The three pioneers merged their ideas to create a single,
standardized modeling language, and UML 0.8 was developed.
• 1997: The first version of UML was formally released as a standard
by the Object Management Group (OMG).
• 2005: The International Organization for Standardization (ISO)
adopted UML as an official standard, ISO/IEC 19501:2005.
79
Characteristics of UML
1. It is a generalized modeling language.
2. It is different from software programming languages
such as Python, C, C++, etc.
3. It is a pictorial language which can be used to
generate powerful modeling elements.
4. It is related to object-oriented designs and analysis.
5. It has unlimited applications even outside the
software industry. It can be used to visualize the
workflow of a factory.
80
Conceptual model
• A conceptual model is made up of various concepts
which are interrelated. It helps us to understand
1. What are the objects?
2. How interaction takes place to execute a process?
• A conceptual model is required in UML. You
have to understand the entities and
relationships between them before actually
modeling the system.
81
Object Oriented Concepts
• Object: It is a real-world entity. There are
multiple objects available within a single system.
It is a fundamental building block of UML.
• Class: A class is nothing but a container where
objects and their relationships are maintained.
• Abstraction: It is a mechanism of representing
an entity without showing the implementation
details. It is used to visualize the behavior of an
object.
82
Object Oriented Concepts
• Inheritance: It is a mechanism of extending
an existing class to create a new class.
• Polymorphism: It is a mechanism of
representing an object having multiple forms
which are used for different purposes.
• Encapsulation: It is a method of binding the
object and the data together as a single unit.
It ensures tight coupling between the object
and the data.
83
Advantages of UML
• Standardization: UML is a standardized language,
providing a common and widely understood vocabulary
for modeling software systems.
• Visual Representation: UML diagrams offer a visual
representation of system architecture, making it easier
for stakeholders to understand the system and
communicate ideas effectively.
• Structured Analysis: UML allows for a structured
approach to system analysis and design, helping
developers break down complex systems into
manageable components.
84
Advantages of UML
• Model-Driven Development: UML supports
model-driven development, allowing
developers to create models of a system
before writing code, which can improve the
overall quality of the system.
• Documentation: UML diagrams serve as
documentation for the system, capturing its
architecture and design decisions in a
standardized format.
85
Disadvantages of UML
• Complexity: UML can be complex, due to the large
number of diagram types and the detailed
specifications for each diagram.
• Too Much Detail: UML diagrams can become too
detailed, making them difficult to understand and
maintain.
• Lack of Flexibility: UML may not be flexible enough
to capture all aspects of a system, especially for
more complex or unconventional systems.
86
Disadvantages of UML
• Tool Support: UML tools can be expensive, and
not all tools support all UML diagram types or the
latest UML standards.
• Subjectivity: The interpretation of UML diagrams
can be subjective, leading to potential
miscommunication or misunderstandings among
stakeholders.
Java OOP
Introduction to UML – Class Diagram
88
Class Diagram
• Represents the structure of a system by
showing the system's classes, attributes,
methods, and relationships between classes.
• It provides a detailed view of the system's
entities and their relationships, making it easier
to understand the system's architecture, design,
and dependencies.
89
Purposes of a Class Diagram
• Analysis and Design: Class diagrams help
in analyzing and designing the system by
providing a clear representation of the
system's entities and their relationships.
• This helps in understanding how the system
works and how its components interact with
each other.
90
Purposes of a Class Diagram
• Communication: Class diagrams serve as a
communication tool between stakeholders,
such as developers, designers, and clients,
by providing a visual representation of the
system's structure and relationships.
• This makes it easier to discuss and agree on
system requirements and design decisions.
91
Purposes of a Class Diagram
• Implementation and Documentation: Class
diagrams can be used as a basis for
implementing the system by providing a
detailed description of the system's classes,
attributes, methods, and relationships.
• They also serve as documentation for the
system's architecture and design, making it
easier for developers to understand and
maintain the system.
92
When is it ideal to use
• 1. Object-Oriented Systems: Class diagrams are
most commonly used in object-oriented systems
where objects and classes are the main building
blocks of the system.
• 2. Large and Complex Systems: Class diagrams
are particularly useful in large and complex
systems with multiple classes and complex
relationships between them.
93
When is it ideal to use
• 3. Design and Analysis Phases: Class diagrams are
commonly used in the design and analysis phases of
the software development lifecycle to understand the
system's requirements, design the system's
architecture, and communicate the design to
stakeholders.
• 4. Collaborative Development: Class diagrams are
beneficial for collaborative development, as they
provide a shared understanding of the system's
structure and design among team members.
94
Class Diagram Notation
Class Name
- The name of the class appears in the first
partition.
Class Attributes
- Attributes are shown in the second partition.
- The attribute type is shown after the colon.
- Attributes map onto member variables (data
members) in code.
95
Class Diagram Notation
Class Operations (Methods)
- Operations are shown in the third partition.
They are services the class provides.
- The return type of a method is shown after the
colon at the end of the method signature.
- The return type of method parameters is shown
after the colon following the parameter name.
- Operations map onto class methods in code
96
Class Diagram Notation
Visibility Type
• + Public
• - Private
• # Protected
Public: Accessible by
anyone, anywhere.
Private: Accessible only
within the same class.
Protected: Accessible
within the same class and
its subclasses.
97
Public (+)
Attribute or method
is accessible by all
other classes and
objects. Public
attributes and
methods are not
restricted to
specific classes
and can be
accessed from
anywhere.
Class Diagram
Java
+speed:int
public int speed;
+cost:double
public double cost;
+text:String
public String text;
+main(args : String[]):void
public static void main(String[]
args)
+ myMethod(fname:String,
age:int):void
public static void
myMethod(String fname, int
age)
+ plusMethodInt(x:int, y:int):int
public static int
plusMethodInt(int x, int y)
98
Private (-)
Attribute or method
is accessible only
within the class in
which it is defined.
Private attributes
and methods are
not accessible from
other classes and
objects.
Class Diagram
Java
-speed:int
private int speed;
-cost:double
private double cost;
-text:String
private String text;
-main(args : String[]):void
private static void main(String[]
args)
- myMethod(fname:String,
age:int):void
private static void
myMethod(String fname, int
age)
- plusMethodInt(x:int, y:int):int
private static int
plusMethodInt(int x, int y)
99
Protected (#)
Attribute or method is
accessible within the
class in which it is
defined and by
subclasses that inherit
from that class.
Protected attributes and
methods are not
accessible by other
classes outside the
inheritance hierarchy.
Class Diagram
Java
#speed:int
public int speed;
#cost:double
public double cost;
#text:String
public String text;
#main(args : String[]):void
public static void main(String[]
args)
#myMethod(fname:String,
age:int):void
public static void
myMethod(String fname, int
age)
#plusMethodInt(x:int, y:int):int
public static int
plusMethodInt(int x, int y)
100
Example 1
- Class
Diagram
101
Example 1
Java Code
public class Vehicle {
private String make;
private String model;
private int year;
// Constructor
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Getters and Setters
public String getMake() {
return make;
}
102
Example 1
Java Code
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
103
Example 1
Java Code public void setYear(int year) {
this.year = year;
}
// Other methods
public void accelerate() {
System.out.println("Vehicle is accelerating.");
}
public void brake() {
System.out.println("Vehicle is braking.");
}
104
Example 1
Java Code
// Main method for testing
public static void main(String[] args) {
Vehicle car = new Vehicle("Toyota", "Camry", 2022);
System.out.println("Make: " + car.getMake());
System.out.println("Model: " + car.getModel());
System.out.println("Year: " + car.getYear());
car.accelerate();
car.brake();
}
}
105
Example 2
- Class
Diagram
106
Example 2
Java Code
public class BankAccount {
private String accountNumber;
private double balance;
// Constructor
public BankAccount(String accountNumber, double
initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Getter and Setter for accountNumber
public String getAccountNumber() {
return accountNumber;
}
107
Example 2
Java Code
public void setAccountNumber(String
accountNumber) {
this.accountNumber = accountNumber;
}
// Getter for balance
public double getBalance() {
return balance;
}
// Method to deposit money
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
108
Example 2
Java Code
// Method to withdraw money
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " +
amount);
return true;
} else {
System.out.println("Insufficient funds!");
return false;
}
}
109
Example 2
Java Code
// Main method for testing
public static void main(String[] args) {
BankAccount account = new
BankAccount("123456789", 1000.0);
System.out.println("Account Number:
" + account.getAccountNumber());
System.out.println("Initial Balance: "
+ account.getBalance());
account.deposit(500.0);
System.out.println("Current Balance
after deposit: " + account.getBalance());
110
Example 2
Java Code
account.withdraw(200.0);
System.out.println("Current Balance
after withdrawal: " +
account.getBalance());
account.withdraw(2000.0); // Attempt
to withdraw more than balance
}
}
111
Exercise
Given the class
diagram provide
the Java Source
Code. Write
your answers in
1 whole yellow
paper.
112
113
Class Diagram Notation
Class relationship: Inheritance
• Represents an "is-a" relationship.
• An abstract class name is shown in
italics.
• SubClass1 and SubClass2 are
specializations of Super Class.
• A solid line with a hollow arrowhead
that point from the child to the
parent class
114
Sample Java Code
class SuperClass{
// Base class
}
class SubClass1 extends
SuperClass {
// SubClass1 inherits from
SuperClass
}
class SubClass2 extends SuperClass{
// SubClass2 inherits from
SuperClass
}
115
Class Diagram Notation
Simple Association
• “has a” relationship
• A structural link between two
peer classes.
• There is an association
between Class1 and Class2
• A solid line connecting two
classes
116
Sample Java Code
• class Class1 {
• private Class2 class2; // Associated with
Class2 class
• }
• class Class2 {
• // Class2 definition
• }
117
Class Diagram Notation
• Aggregation
• - A special type of association. It represents
a "part of" relationship.
• - Class2 is part of Class1.
• - Many instances (denoted by the *) of
Class2 can be associated with Class1.
• - Objects of Class1 and Class2 have
separate lifetimes.
• - A solid line with an unfilled diamond at the
association end connected to the class of
composite
118
Sample Java Code
• public class Class1 {
• private List<Class2> class2List;
• public Class1(List<Class2>
class2List) {
• this.class2List = class2List;
• }
• }
class Class2 {
// Class2 definition
}
119
Class Diagram Notation
• Composition
• - A special type of aggregation
where parts are destroyed when
the whole is destroyed.
• - Objects of Class2 live and die with
Class1.
• - Class2 cannot stand by itself.
• - A solid line with a filled diamond at
the association connected to the
class of composite
120
Sample Java Code
• public class Class1 {
• private List<Class2> class2List;
• public Class2() {
• this.class2List = new
ArrayList<>();
• }
• }
121
Class Diagram Notation
• Dependency
• - Exists between two classes if
the changes to the definition of
one may cause changes to the
other (but not the other way
around).
• - Class1 depends on Class2
• - A dashed line with an open
arrow
122
Sample Java Code
• class Class1 {
• private Class2 class2; // Depends on
Class2 class
• public Class1(Class2 class2) {
• this.class2 = class2;
• }
• }
class Class2 {
// Class2 definition
}
123
Relationship Summary
• Inheritance: Child class "is-a" parent class.
• Association: Class A "has-a" relationship with
Class B.
• Aggregation: Class A “is part of" Class B, but B
can exist independently.
• Composition: Class A "contains" Class B, and B
cannot exist independently.
• Dependency: Class A "depends-on" Class B, but
they are independent entities.
Class Diagram
– Student
Management
System
Class
Diagram –
Online
Shopping
System
Use Case
Diagram –
Library
Management
System

More Related Content

Similar to Introduction to Java Object Oriented Programming (20)

PPTX
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
DOC
Core java interview questions
Vinay Kumar
 
PDF
Java MCQ Important Questions and Answers
SONU HEETSON
 
PPTX
Java PPT
Dilip Kr. Jangir
 
PDF
Cs8392 oops 5 units notes
Narayanan sockalinganathan
 
PPT
Java OOP s concepts and buzzwords
Raja Sekhar
 
DOCX
Java Interview Questions For Freshers
zynofustechnology
 
DOCX
OOP Lab-manual btech in cse kerala technological university
seenamt1234
 
DOCX
Mcs 024 assignment solution (2020-21)
smumbahelp
 
DOCX
Core java questions
Pradheep Ayyanar
 
PDF
FP 301 OOP FINAL PAPER
Syahriha Ruslan
 
PDF
Java Interview Questions
Kuntal Bhowmick
 
DOC
Faqs in java
prathap kumar
 
PDF
Core java interview faq
Kumaran K
 
PPTX
Std 12 computer chapter 8 classes and objects in java important MCQs
Nuzhat Memon
 
PDF
Java Programming.pdf
RavinderKSingla
 
DOCX
Corejavainterviewquestions.doc
Joyce Thomas
 
PDF
__ Java Technical round questions .pdf soo
Rajkumar751652
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
DOCX
Java interview questions
Shashwat Shriparv
 
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Core java interview questions
Vinay Kumar
 
Java MCQ Important Questions and Answers
SONU HEETSON
 
Cs8392 oops 5 units notes
Narayanan sockalinganathan
 
Java OOP s concepts and buzzwords
Raja Sekhar
 
Java Interview Questions For Freshers
zynofustechnology
 
OOP Lab-manual btech in cse kerala technological university
seenamt1234
 
Mcs 024 assignment solution (2020-21)
smumbahelp
 
Core java questions
Pradheep Ayyanar
 
FP 301 OOP FINAL PAPER
Syahriha Ruslan
 
Java Interview Questions
Kuntal Bhowmick
 
Faqs in java
prathap kumar
 
Core java interview faq
Kumaran K
 
Std 12 computer chapter 8 classes and objects in java important MCQs
Nuzhat Memon
 
Java Programming.pdf
RavinderKSingla
 
Corejavainterviewquestions.doc
Joyce Thomas
 
__ Java Technical round questions .pdf soo
Rajkumar751652
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Java interview questions
Shashwat Shriparv
 

Recently uploaded (20)

PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Ad

Introduction to Java Object Oriented Programming

  • 2. 2 Which of the following is NOT a principle of Object-Oriented Programming? A) Encapsulation B) Abstraction C) Inheritance D) Multithreading
  • 3. 3 Answer and Explanation D) Multithreading Multithreading is a concept related to concurrent programming, not strictly a principle of OOP.
  • 4. 4 In Java, which keyword is used to implement inheritance between classes? A) this B) extends C) implements D) inherit
  • 5. 5 Answer and Explanation B) extends The keyword "extends" is used to establish an inheritance relationship between classes in Java.
  • 6. 6 Which OOP concept describes the ability of an object to take many forms? A) Polymorphism B) Abstraction C) Inheritance D) Encapsulation
  • 7. 7 Answer and Explanation A) Polymorphism Polymorphism allows objects to be treated as instances of their parent class, enabling flexibility in programming.
  • 8. 8 Which access modifier allows a variable or method to be accessible only within its own class? A) public B) private C) protected D) default (no modifier)
  • 9. 9 Answer and Explanation B) private Private access modifier restricts access to members within the same class only.
  • 10. 10 Which access modifier allows a variable or method to be accessible only within its own class? A) public B) private C) protected D) default (no modifier)
  • 11. 11 Answer and Explanation B) private Private access modifier restricts access to members within the same class only.
  • 12. 12 Which OOP concept refers to bundling data and methods that operate on the data into a single unit? A) Encapsulation B) Inheritance C) Polymorphism D) Abstraction
  • 13. 13 Answer and Explanation A) Encapsulation Encapsulation hides the internal state of an object and restricts access to it.
  • 14. 14 Which method is automatically called when an object is created in Java? A) init() B) start() C) main() D) constructor()
  • 15. 15 Answer and Explanation D) constructor() Constructors initialize objects when they are created.
  • 16. 16 Which OOP concept allows a class to have multiple methods with the same name but different parameters? A) Overriding B) Overloading C) Polymorphism D) Encapsulation
  • 17. 17 Answer and Explanation B) Overloading Overloading enables a class to have multiple methods with the same name but different parameter lists.
  • 18. 18 Which keyword is used to refer to the current instance of a class in Java? A) that B) self C) this D) here
  • 19. 19 Answer and Explanation C) this The "this" keyword refers to the current instance of the class.
  • 20. 20 Which OOP principle describes the process of hiding the implementation details of an object? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 21. 21 Answer and Explanation D) Abstraction Abstraction focuses on hiding the internal implementation details of objects and showing only the necessary features.
  • 22. 22 Which keyword is used to indicate that a method does not return any value in Java? A) void B) null C) none D) empty
  • 23. 23 Answer and Explanation A) void The "void" keyword is used in method declarations to indicate that the method does not return any value.
  • 24. 24 Which OOP concept allows a class to inherit attributes and methods from another class? A) Encapsulation B) Polymorphism C) Inheritance D) Abstraction
  • 25. 25 Answer and Explanation C) Inheritance Inheritance enables the creation of a new class that adopts the properties and behaviors of an existing class.
  • 26. 26 Which OOP principle describes the ability of objects to share behaviors or characteristics with other objects? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 27. 27 Answer and Explanation A) Inheritance Inheritance allows objects to acquire properties and behaviors from parent objects, facilitating code reuse and organization.
  • 28. 28 Which OOP principle describes the ability of objects to share behaviors or characteristics with other objects? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 29. 29 Answer and Explanation A) Inheritance Inheritance allows objects to acquire properties and behaviors from parent objects, facilitating code reuse and organization.
  • 30. 30 What is the process of defining a new class based on an existing class, thereby inheriting its attributes and methods? A) Composition B) Polymorphism C) Inheritance D) Encapsulation
  • 31. 31 Answer and Explanation C) Inheritance Inheritance allows for the creation of new classes that acquire properties and behaviors from existing classes.
  • 32. 32 Which OOP concept allows a class to have multiple methods with the same name and parameter list, but different functionality? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 33. 33 Answer and Explanation C) Polymorphism Polymorphism allows methods to be overridden in subclasses, providing different implementations while using the same method signature.
  • 34. 34 What does the term "method overloading" refer to in Java? A) Defining a method with the same name in different classes B) Defining multiple methods with the same name but different parameter lists in the same class C) Calling a method from within another method D) Overriding a method in a subclass
  • 35. 35 Answer and Explanation B) Defining multiple methods with the same name but different parameter lists in the same class Method overloading allows a class to have multiple methods with the same name but different parameter lists.
  • 36. 36 Which OOP concept allows a class to hide the implementation details of its methods while still providing access to their functionality? A) Inheritance B) Polymorphism C) Abstraction D) Encapsulation
  • 37. 37 Answer and Explanation C) Abstraction Abstraction focuses on hiding the implementation details of methods and showing only the essential features.
  • 38. 38 Which access modifier provides the widest accessibility in Java, allowing a variable or method to be accessed from anywhere? A) public B) private C) protected D) default (no modifier)
  • 39. 39 Answer and Explanation A) public The "public" access modifier allows a variable or method to be accessed from any class.
  • 40. 40 What is the purpose of the "this" keyword in Java? A) It refers to the current instance of the class. B) It references the superclass. C) It indicates that a method does not return any value. D) It is used to prevent a class from being inherited.
  • 41. 41 Answer and Explanation A) It refers to the current instance of the class. The "this" keyword is used to refer to the current instance of the class.
  • 42. 42 In Java, what does the term "encapsulation" refer to? A) It refers to the process of hiding the implementation details of a class. B) It allows a class to inherit attributes and methods from another class. C) It describes the ability of objects to share behaviors or characteristics with other objects. D) It involves bundling data and methods that operate on the data into a single unit.
  • 43. 43 Answer and Explanation D) It involves bundling data and methods that operate on the data into a single unit. Encapsulation hides the internal state of an object and restricts access to it, while bundling data and methods together.
  • 44. 44 Which OOP concept allows a class to provide different implementations of methods that are called by the same name? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 45. 45 Answer and Explanation C) Polymorphism Polymorphism allows for the implementation of methods to vary depending on the object that invokes them.
  • 46. 46 What is the purpose of the "extends" keyword in Java? A) It is used to prevent a class from being inherited. B) It is used to establish an inheritance relationship between classes. C) It allows a class to provide multiple implementations of a method. D) It is used to declare a method that does not return any value.
  • 47. 47 Answer and Explanation B) It is used to establish an inheritance relationship between classes. The "extends" keyword is used to create a subclass that inherits properties and behaviors from a superclass.
  • 48. 48 Which keyword is used to create an object of a class in Java? A) new B) class C) object D) instance
  • 49. 49 Answer and Explanation A) new The "new" keyword is used to instantiate an object of a class in Java.
  • 50. 50 In Java, what is the purpose of the "default" access modifier? A) It allows access to the variable or method only within the same package. B) It restricts access to the variable or method to only subclasses. C) It makes the variable or method accessible from anywhere in the program. D) It hides the variable or method from being accessed outside the class.
  • 51. 51 Answer and Explanation A) It allows access to the variable or method only within the same package. The "default" access modifier allows access within the same package but not outside of it.
  • 52. 52 Which OOP concept allows a class to implement multiple interfaces in Java? A) Inheritance B) Encapsulation C) Polymorphism D) Interface
  • 53. 53 Answer and Explanation D) Interface Interfaces in Java allow a class to implement multiple behaviors, enabling polymorphism.
  • 54. 54 What is the term for the process of hiding the internal state of an object and requiring all interactions to occur through well-defined interfaces? A) Abstraction B) Polymorphism C) Inheritance D) Overriding
  • 55. 55 Answer and Explanation A) Abstraction Abstraction involves hiding internal details and exposing only necessary features through well- defined interfaces.
  • 56. 56 In Java, what is the purpose of the "implements" keyword? A) It is used to establish an inheritance relationship between classes. B) It allows a class to define multiple implementations of a method. C) It is used to indicate that a class is a subclass of another class. D) It is used to declare that a class implements an interface.
  • 57. 57 Answer and Explanation D) It is used to declare that a class implements an interface. The "implements" keyword is used to indicate that a class implements one or more interfaces.
  • 58. 58 Which OOP concept allows different classes to share some common functionality or behavior? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 59. 59 Answer and Explanation A) Inheritance Inheritance allows classes to share properties and behaviors from a common superclass.
  • 60. 60 What is the term for the process of defining a new class based on an existing class, thereby inheriting its attributes and methods? A) Composition B) Polymorphism C) Inheritance D) Encapsulation
  • 61. 61 Answer and Explanation C) Inheritance Inheritance allows for the creation of new classes that acquire properties and behaviors from existing classes.
  • 62. 62 What is the term for the process of defining a new class based on an existing class, thereby inheriting its attributes and methods? A) Composition B) Polymorphism C) Inheritance D) Encapsulation
  • 63. 63 Answer and Explanation C) Inheritance Inheritance allows for the creation of new classes that acquire properties and behaviors from existing classes.
  • 64. 64 Which OOP concept allows a class to provide different implementations of methods based on their parameter types? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 65. 65 Answer and Explanation C) Polymorphism Polymorphism in Java allows methods to be invoked with different arguments, resulting in different behavior.
  • 66. 66 Which access modifier restricts access to the variable or method to only subclasses and classes within the same package? A) private B) public C) protected D) default (no modifier)
  • 67. 67 Answer and Explanation C) protected The "protected" access modifier allows access to subclasses and classes within the same package, but not from other packages.
  • 68. 68 What is the purpose of the "abstract" keyword in Java? A) It is used to create abstract classes. B) It is used to prevent a class from being instantiated. C) It indicates that a method must be overridden in subclasses. D) It allows a class to define multiple implementations of a method.
  • 69. 69 Answer and Explanation A) It is used to create abstract classes. The "abstract" keyword is used to declare abstract classes and methods in Java.
  • 70. 70 Which OOP concept describes the ability of objects to share behaviors or characteristics with other objects through a common interface? A) Inheritance B) Encapsulation C) Polymorphism D) Abstraction
  • 71. 71 Answer and Explanation A) Inheritance Inheritance allows objects to share properties and behaviors through a common superclass.
  • 72. 72 What is the term for the ability of a method to be defined in a superclass and then redefined in a subclass? A) Overriding B) Overloading C) Polymorphism D) Inheritance
  • 73. 73 Answer and Explanation A) Overriding Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
  • 74. 74 What is the term for the ability of a method to be defined in a superclass and then redefined in a subclass? A) Overriding B) Overloading C) Polymorphism D) Inheritance
  • 75. 75 Answer and Explanation A) Overriding Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
  • 77. 77 UML (Unified Modeling Language) • Standardized general-purpose modeling language in the field of software engineering. • It provides a way to visualize a system's architecture, helping developers understand the system, create its design, and document its architecture.
  • 78. 78 History of UML: • Late 1980s to early 1990s: Software engineering had several competing methods for modeling systems, which made it difficult to communicate ideas between developers. This led to the development of UML. • Early 1990s: Grady Booch, James Rumbaugh, and Ivar Jacobson were the principal developers of UML, although they were working separately at Rational Software, Hewlett-Packard, and Objectory AB, respectively. • 1994: The three pioneers merged their ideas to create a single, standardized modeling language, and UML 0.8 was developed. • 1997: The first version of UML was formally released as a standard by the Object Management Group (OMG). • 2005: The International Organization for Standardization (ISO) adopted UML as an official standard, ISO/IEC 19501:2005.
  • 79. 79 Characteristics of UML 1. It is a generalized modeling language. 2. It is different from software programming languages such as Python, C, C++, etc. 3. It is a pictorial language which can be used to generate powerful modeling elements. 4. It is related to object-oriented designs and analysis. 5. It has unlimited applications even outside the software industry. It can be used to visualize the workflow of a factory.
  • 80. 80 Conceptual model • A conceptual model is made up of various concepts which are interrelated. It helps us to understand 1. What are the objects? 2. How interaction takes place to execute a process? • A conceptual model is required in UML. You have to understand the entities and relationships between them before actually modeling the system.
  • 81. 81 Object Oriented Concepts • Object: It is a real-world entity. There are multiple objects available within a single system. It is a fundamental building block of UML. • Class: A class is nothing but a container where objects and their relationships are maintained. • Abstraction: It is a mechanism of representing an entity without showing the implementation details. It is used to visualize the behavior of an object.
  • 82. 82 Object Oriented Concepts • Inheritance: It is a mechanism of extending an existing class to create a new class. • Polymorphism: It is a mechanism of representing an object having multiple forms which are used for different purposes. • Encapsulation: It is a method of binding the object and the data together as a single unit. It ensures tight coupling between the object and the data.
  • 83. 83 Advantages of UML • Standardization: UML is a standardized language, providing a common and widely understood vocabulary for modeling software systems. • Visual Representation: UML diagrams offer a visual representation of system architecture, making it easier for stakeholders to understand the system and communicate ideas effectively. • Structured Analysis: UML allows for a structured approach to system analysis and design, helping developers break down complex systems into manageable components.
  • 84. 84 Advantages of UML • Model-Driven Development: UML supports model-driven development, allowing developers to create models of a system before writing code, which can improve the overall quality of the system. • Documentation: UML diagrams serve as documentation for the system, capturing its architecture and design decisions in a standardized format.
  • 85. 85 Disadvantages of UML • Complexity: UML can be complex, due to the large number of diagram types and the detailed specifications for each diagram. • Too Much Detail: UML diagrams can become too detailed, making them difficult to understand and maintain. • Lack of Flexibility: UML may not be flexible enough to capture all aspects of a system, especially for more complex or unconventional systems.
  • 86. 86 Disadvantages of UML • Tool Support: UML tools can be expensive, and not all tools support all UML diagram types or the latest UML standards. • Subjectivity: The interpretation of UML diagrams can be subjective, leading to potential miscommunication or misunderstandings among stakeholders.
  • 87. Java OOP Introduction to UML – Class Diagram
  • 88. 88 Class Diagram • Represents the structure of a system by showing the system's classes, attributes, methods, and relationships between classes. • It provides a detailed view of the system's entities and their relationships, making it easier to understand the system's architecture, design, and dependencies.
  • 89. 89 Purposes of a Class Diagram • Analysis and Design: Class diagrams help in analyzing and designing the system by providing a clear representation of the system's entities and their relationships. • This helps in understanding how the system works and how its components interact with each other.
  • 90. 90 Purposes of a Class Diagram • Communication: Class diagrams serve as a communication tool between stakeholders, such as developers, designers, and clients, by providing a visual representation of the system's structure and relationships. • This makes it easier to discuss and agree on system requirements and design decisions.
  • 91. 91 Purposes of a Class Diagram • Implementation and Documentation: Class diagrams can be used as a basis for implementing the system by providing a detailed description of the system's classes, attributes, methods, and relationships. • They also serve as documentation for the system's architecture and design, making it easier for developers to understand and maintain the system.
  • 92. 92 When is it ideal to use • 1. Object-Oriented Systems: Class diagrams are most commonly used in object-oriented systems where objects and classes are the main building blocks of the system. • 2. Large and Complex Systems: Class diagrams are particularly useful in large and complex systems with multiple classes and complex relationships between them.
  • 93. 93 When is it ideal to use • 3. Design and Analysis Phases: Class diagrams are commonly used in the design and analysis phases of the software development lifecycle to understand the system's requirements, design the system's architecture, and communicate the design to stakeholders. • 4. Collaborative Development: Class diagrams are beneficial for collaborative development, as they provide a shared understanding of the system's structure and design among team members.
  • 94. 94 Class Diagram Notation Class Name - The name of the class appears in the first partition. Class Attributes - Attributes are shown in the second partition. - The attribute type is shown after the colon. - Attributes map onto member variables (data members) in code.
  • 95. 95 Class Diagram Notation Class Operations (Methods) - Operations are shown in the third partition. They are services the class provides. - The return type of a method is shown after the colon at the end of the method signature. - The return type of method parameters is shown after the colon following the parameter name. - Operations map onto class methods in code
  • 96. 96 Class Diagram Notation Visibility Type • + Public • - Private • # Protected Public: Accessible by anyone, anywhere. Private: Accessible only within the same class. Protected: Accessible within the same class and its subclasses.
  • 97. 97 Public (+) Attribute or method is accessible by all other classes and objects. Public attributes and methods are not restricted to specific classes and can be accessed from anywhere. Class Diagram Java +speed:int public int speed; +cost:double public double cost; +text:String public String text; +main(args : String[]):void public static void main(String[] args) + myMethod(fname:String, age:int):void public static void myMethod(String fname, int age) + plusMethodInt(x:int, y:int):int public static int plusMethodInt(int x, int y)
  • 98. 98 Private (-) Attribute or method is accessible only within the class in which it is defined. Private attributes and methods are not accessible from other classes and objects. Class Diagram Java -speed:int private int speed; -cost:double private double cost; -text:String private String text; -main(args : String[]):void private static void main(String[] args) - myMethod(fname:String, age:int):void private static void myMethod(String fname, int age) - plusMethodInt(x:int, y:int):int private static int plusMethodInt(int x, int y)
  • 99. 99 Protected (#) Attribute or method is accessible within the class in which it is defined and by subclasses that inherit from that class. Protected attributes and methods are not accessible by other classes outside the inheritance hierarchy. Class Diagram Java #speed:int public int speed; #cost:double public double cost; #text:String public String text; #main(args : String[]):void public static void main(String[] args) #myMethod(fname:String, age:int):void public static void myMethod(String fname, int age) #plusMethodInt(x:int, y:int):int public static int plusMethodInt(int x, int y)
  • 101. 101 Example 1 Java Code public class Vehicle { private String make; private String model; private int year; // Constructor public Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Getters and Setters public String getMake() { return make; }
  • 102. 102 Example 1 Java Code public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; }
  • 103. 103 Example 1 Java Code public void setYear(int year) { this.year = year; } // Other methods public void accelerate() { System.out.println("Vehicle is accelerating."); } public void brake() { System.out.println("Vehicle is braking."); }
  • 104. 104 Example 1 Java Code // Main method for testing public static void main(String[] args) { Vehicle car = new Vehicle("Toyota", "Camry", 2022); System.out.println("Make: " + car.getMake()); System.out.println("Model: " + car.getModel()); System.out.println("Year: " + car.getYear()); car.accelerate(); car.brake(); } }
  • 106. 106 Example 2 Java Code public class BankAccount { private String accountNumber; private double balance; // Constructor public BankAccount(String accountNumber, double initialBalance) { this.accountNumber = accountNumber; this.balance = initialBalance; } // Getter and Setter for accountNumber public String getAccountNumber() { return accountNumber; }
  • 107. 107 Example 2 Java Code public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; } // Getter for balance public double getBalance() { return balance; } // Method to deposit money public void deposit(double amount) { balance += amount; System.out.println("Deposited: " + amount); }
  • 108. 108 Example 2 Java Code // Method to withdraw money public boolean withdraw(double amount) { if (amount <= balance) { balance -= amount; System.out.println("Withdrawn: " + amount); return true; } else { System.out.println("Insufficient funds!"); return false; } }
  • 109. 109 Example 2 Java Code // Main method for testing public static void main(String[] args) { BankAccount account = new BankAccount("123456789", 1000.0); System.out.println("Account Number: " + account.getAccountNumber()); System.out.println("Initial Balance: " + account.getBalance()); account.deposit(500.0); System.out.println("Current Balance after deposit: " + account.getBalance());
  • 110. 110 Example 2 Java Code account.withdraw(200.0); System.out.println("Current Balance after withdrawal: " + account.getBalance()); account.withdraw(2000.0); // Attempt to withdraw more than balance } }
  • 111. 111 Exercise Given the class diagram provide the Java Source Code. Write your answers in 1 whole yellow paper.
  • 112. 112
  • 113. 113 Class Diagram Notation Class relationship: Inheritance • Represents an "is-a" relationship. • An abstract class name is shown in italics. • SubClass1 and SubClass2 are specializations of Super Class. • A solid line with a hollow arrowhead that point from the child to the parent class
  • 114. 114 Sample Java Code class SuperClass{ // Base class } class SubClass1 extends SuperClass { // SubClass1 inherits from SuperClass } class SubClass2 extends SuperClass{ // SubClass2 inherits from SuperClass }
  • 115. 115 Class Diagram Notation Simple Association • “has a” relationship • A structural link between two peer classes. • There is an association between Class1 and Class2 • A solid line connecting two classes
  • 116. 116 Sample Java Code • class Class1 { • private Class2 class2; // Associated with Class2 class • } • class Class2 { • // Class2 definition • }
  • 117. 117 Class Diagram Notation • Aggregation • - A special type of association. It represents a "part of" relationship. • - Class2 is part of Class1. • - Many instances (denoted by the *) of Class2 can be associated with Class1. • - Objects of Class1 and Class2 have separate lifetimes. • - A solid line with an unfilled diamond at the association end connected to the class of composite
  • 118. 118 Sample Java Code • public class Class1 { • private List<Class2> class2List; • public Class1(List<Class2> class2List) { • this.class2List = class2List; • } • } class Class2 { // Class2 definition }
  • 119. 119 Class Diagram Notation • Composition • - A special type of aggregation where parts are destroyed when the whole is destroyed. • - Objects of Class2 live and die with Class1. • - Class2 cannot stand by itself. • - A solid line with a filled diamond at the association connected to the class of composite
  • 120. 120 Sample Java Code • public class Class1 { • private List<Class2> class2List; • public Class2() { • this.class2List = new ArrayList<>(); • } • }
  • 121. 121 Class Diagram Notation • Dependency • - Exists between two classes if the changes to the definition of one may cause changes to the other (but not the other way around). • - Class1 depends on Class2 • - A dashed line with an open arrow
  • 122. 122 Sample Java Code • class Class1 { • private Class2 class2; // Depends on Class2 class • public Class1(Class2 class2) { • this.class2 = class2; • } • } class Class2 { // Class2 definition }
  • 123. 123 Relationship Summary • Inheritance: Child class "is-a" parent class. • Association: Class A "has-a" relationship with Class B. • Aggregation: Class A “is part of" Class B, but B can exist independently. • Composition: Class A "contains" Class B, and B cannot exist independently. • Dependency: Class A "depends-on" Class B, but they are independent entities.