Assignment on OOP
Assignment on OOP
i. Class
A class is a blueprint or template for creating objects. It defines a set of attributes (data) and
methods (functions or behaviors) that the objects created from the class will have.
ii. Object
An object is an instance of a class. It represents a specific entity that has its own state and
behavior defined by the class. Each object can have unique values for its attributes.
iii. Parameters
Parameters are variables that are used in a method or function to accept input. They define what
type of information the method can accept and are specified in the method’s definition.
iv. Arguments
Arguments are the actual values or data that are passed to a method or function when it is called.
They correspond to the parameters defined in the method.
v. Abstraction
Abstraction is the concept of hiding complex implementation details and exposing only the
essential features of an object. It simplifies interactions with objects by focusing on relevant
attributes and methods.
vi. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon, even
though they share the same name. This can be achieved through method overriding (in
subclasses) or method overloading (same method name with different parameters).
vii. Inheritance
Inheritance is a mechanism by which a new class (subclass or derived class) can inherit attributes
and methods from an existing class (superclass or base class). It promotes code reusability and
establishes a hierarchical relationship between classes.
viii. Encapsulation
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the
data into a single unit or class. It restricts direct access to some of an object’s components, which
is intended to protect the object's integrity and enforce a controlled interface for interacting with
the object.
2. With a Java program example, explain the main principles of object oriented programming languages
I. Abstraction
This hides the implementation details and provides a simple interface for subclasses to implement their
own behavior.
II. Encapsulation
It provides public methods to access and modify this variable, controlling how it can be interacted with.
III. Inheritance
This means they can use the properties and behaviors defined while providing their own specific
implementations of the sound() method.
IV. Polymorphism
where the method behavior changes based on the actual object type.
3. With the help of an example, explain the difference between do…while and while loop as used
repetition structures.
The main difference between a do...while loop and a while loop in Java is when the condition is checked.
In a while loop, the condition is checked before the loop body is executed, while in a do...while loop, the
condition is checked after the loop body is executed. This means that a do...while loop will always
execute its body at least once, even if the condition is false.
Example.
int countWhile = 0;
System.out.println("While Loop:");
countWhile++;
}
// Using a do...while loop
int countDoWhile = 0;
System.out.println("\nDo...While Loop:");
do {
countDoWhile++;
a) Single-Line Comments
Single-line comments start with two forward slashes (//). Everything following // on that line is treated
as a comment and ignored by the compiler.
b) Multi-Line Comments
Multi-line comments start with /* and end with */. This type of comment can span multiple lines,
allowing you to write longer explanations or documentation.
c) Javadoc Comments
Javadoc comments are a special type of multi-line comment that starts with /** and ends with */. These
comments are specifically used to generate documentation for Java classes, methods, and fields. Javadoc
comments can include tags such as @param, @return, and @see to provide structured documentation.