0% found this document useful (0 votes)
2 views

Assignment on OOP

The document defines key object-oriented programming concepts such as class, object, parameters, arguments, abstraction, polymorphism, inheritance, and encapsulation. It explains the principles of OOP using Java examples, highlighting abstraction, encapsulation, inheritance, and polymorphism. Additionally, it contrasts do...while and while loops, describes three types of comments in Java, and provides the general syntax for creating an object in Java.

Uploaded by

piousirungu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment on OOP

The document defines key object-oriented programming concepts such as class, object, parameters, arguments, abstraction, polymorphism, inheritance, and encapsulation. It explains the principles of OOP using Java examples, highlighting abstraction, encapsulation, inheritance, and polymorphism. Additionally, it contrasts do...while and while loops, describes three types of comments in Java, and provides the general syntax for creating an object in Java.

Uploaded by

piousirungu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Define the following terms as used object oriented technology.

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.

public class LoopExample {

public static void main(String[] args) {

// Using a while loop

int countWhile = 0;

System.out.println("While Loop:");

while (countWhile < 5) {

System.out.println("Count: " + countWhile);

countWhile++;

}
// Using a do...while loop

int countDoWhile = 0;

System.out.println("\nDo...While Loop:");

do {

System.out.println("Count: " + countDoWhile);

countDoWhile++;

} while (countDoWhile < 5);

4. Explain three types of comments used in java programming.

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.

5. Write the general syntax of creating an object in java

ClassName objectName = new ClassName(parameters);

You might also like