[go: up one dir, main page]

0% found this document useful (0 votes)
8 views16 pages

CIT383

The document provides an overview of Object-Oriented Programming (OOP), explaining its definition, features, and popular languages. It discusses key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with their advantages and differences. Additionally, it covers methods, operator overloading, and abstract data types, emphasizing the importance of modularity and software reusability.

Uploaded by

glamuche2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

CIT383

The document provides an overview of Object-Oriented Programming (OOP), explaining its definition, features, and popular languages. It discusses key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with their advantages and differences. Additionally, it covers methods, operator overloading, and abstract data types, emphasizing the importance of modularity and software reusability.

Uploaded by

glamuche2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CIT383: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING

Explain the term “ Object Oriented Programming Language” List three main features of Object
Oriented Programming. Outline three popular Object Oriented Languages
answer
Object-oriented programming (OOP) is a programming paradigm that uses “objects” – data structures
consisting of data fields and methods and their interactions to design applications and computer
programmes. Programming techniques may include features such as information hiding, data
abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in
mainstream software application development until the early 1990s.

feature of object oriented programming are;


Modularity
Information-hiding
encapsulation
polymorphism
inheritance
data abstraction

Write short notes on three of the following:

(i) Information-hiding
(ii) Code re-use
(iii) Pluggability and debugging ease
(iv) Modularity
answer

Information-hiding: By interacting only with an object’s methods, the details of its internal
implementation remain hidden from the outside world

Code re-use: If an object already exists (perhaps written by another software developer), you can use
that object in your program. This allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.

Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply
remove it from your application and plug in a different object as its replacement. This is analogous to
fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine
Modularity: The source code for an object can be written and maintained independently of the source
code for other objects. Once created, an object can be easily passed around inside the system

Mention the five popular Object Oriented Languages


answer
 ava.
 JavaScript.
 Python.
 C++
 Visual Basic .NET.
 Ruby.
 Scala.
 PHP

What is the difference between a Class and an Object?


answer
A classdefines object properties including a valid range of values, and a default value. Aclass also
describes object behavior. An object is a member or an "instance" of aclass.

Briefly explain what is meant by a class?


answer
In object-oriented programming , a class is a template definition of the method s and variable s in a
particular kind of object . Thus, an object is a specific instance of aclass; it contains real values instead of
variables. ... Aclass can have subclasses that can inherit all or some of the characteristics of the class

State 5 five advantages of using methods in Java.


answer
 While working on one method, you can focus on just that part of the program and construct it, debug
it and perfect it.
 Different people can work on different methods simultaneously.
 If a method is needed in more than one place in a program, or in different program, you can write it
once and use it many times.
 Using methods greatly enhance the programmes readability because it reduces the complexity of the
main method.
 Methods are often called modules. They are like miniature programmes; you can put them together to
form a larger program. The ability is less apparent with the predefined methods because their
programming code is not available to us. However, because predefined methods are already written for
us, this will be learnt first, so that it could be used when needed. To use a predefined method in your
program (s), you need to know only how to use it.
What is the difference between a Class and an Object?
answers
Class object
A class is a collection of a fixed number of This is the first step in problem-solving with object-
components. The components of a class are called oriented design (OOD).they are keys to
the members of the class. The general syntax for understanding object-oriented technology. Real-
defining a class is world objects share two characteristics: They all
have state and behaviour. Software objects are
conceptually similar to real-world objects: they too
consist of state and related behaviour. An object
stores its state in fields (variables in some
It is also reffered to as the mechanism in java that
programming languages) and exposes its
allows one to combine data and operations on that
behaviour through methods (functions in some
that in a single unit. programming languages).

What do you understand by the term class methods? List three (3) examples of methods
answer
class method is a method which is bound to the class and not the object of theclass. They have the
access to the state of the class as it takes a class parameter that points to the class and not the object
instance. It can modify a class state that would apply across all the instances of the class.
examples of methods are;

List two (2) properties of a class


answer
strings, integers, and booleans (true/false values), like any other variable.

Define the term ‘Encapsulation’


ANSWER
Encapsulation is defined as the ability of an object to be a container (or capsule) for related properties
(data variables) and methods (functions).

What is Inheritance in Object–Oriented Programming Language?


answer
Inheritance is the capability of a class to use the properties and methods of another class while adding
its own functionality. An example of of inheritance Is employee records system.in inheritance, The
generic class is known as the super class or base class and the specific classes as subclasses or derived
classes. The concept of inheritance greatly enhances the ability to reuse code as well as making design a
much simpler and cleaner process.
Differentiate between a static field and an instance variable
answer
Static field Instance variable
A static field is used in such an instance and it is A class normally consists of one or more variables
usually called a class variable. A static field or a that represent the attributes of a particular object
class variable has general information that is to be of a class. These variables are called instance
shared by all objects of the class variables

what is the difference between an instance variable and a class variable


answer
The difference between an instance variable and a class variable is that with an instance variable, the
object of a class maintains its own copy of attribute, each object (instance) of the class has a separate
instance of the variable in memory. Class variables are fields for which each object of a class does not
have a separate instance of the field and such variables are declared as static and they are thus called
static variables

List the three (3) logical operators that can be directly overloaded for a class
answer
These are the NOT operator (!), the AND operator (&), the OR operator (|) and the exclusive OR or XOR
operator (^).

What do you understand by Operator Overloading


answer
Operator overloading is simply the process of adding operator functionality to a class. it allows you to
define exactly how the operator behaves when used with your class and other data types. This can be
standard uses such as the ability to add the values of two vectors, more complex mathematics for
multiplying matrices or non-arithmetic functions such as using the + operator to add a new item to a
collection or combine the contents of two arrays. Multiple overloaded versions of operators may also be
created to provide different functionality according to the data types being processed, in a similar
manner to the varying signatures of method overloading.

Write a short note on each of the following:


(i) Overloaded Method and Overridden Method
(ii) Local Variables and Instance Variables
(iii) Set and Get Methods
answer
overloaded method
Overloaded methods are methods with the same name signature but either a different number of
parameters or different types in the parameter list. For example 'spinning' a number may mean increase
it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of
parameter you achieve the effect that you want.

Overridden Method
Overridden methods are methods that are redefined within an inherited or subclass. They have the
same signature and the subclass definition is used

Local Variables
Local variables are variables that are declared in the body of a particular method, such variables can be
used only in that method. When that method terminates, the values of its local variables are lost

Instance Variables
Instance variable must be initialised to their default value before they are used. The syntax for declaring
an instance variable within a method is:
Datatype variablename;

Set and Get Methods


These methods are used to manipulate the instance variables of a class to respectively assign values to
them (set) and retrieve values from them (get). When this is done, the instance variables of a class can
be made private because these methods are used to internally manipulate them within the class

What do you understand by Recursive Operator


answer?
A recursive method is a method that calls itself. A recursive method can be called either directly or
indirectly through another method. Recursive problem solving approaches have a number of elements
in common. When a recursive method is called to solve a problem, the method is only capable of solving
the simplest cases.

Mention the three (3) ways to call a method


answer
 Using a method name by itself to call another methods of the same class e.g. in our programming
example 3.1.1, getDifference(num1,num2).
 Using a variable that contains a reference to an object followed by a dot (.) and the method name to
call a method of the referenced object. This was discussed in unit one of module two.
 Using the class name and a dot (.) to call a static method of a class.

Create a class Student that has a field birthdate which is of the date type.
The class should have methods that can display the first name, last name and the birth date of
students
answer
Explain the term Polymorphism and give example
answer
Polymorphism is the capability of an action or method to do different things based on the object that it
is acting upon. it is the third basic principle of object-oriented programming.there are two types of
polymorphism which are Overloading and overriding

example
Mention 2 (two) types of Polymorphism and explain them
answer
The two main types of polymorphism are overloaded method and overridden methods. Overloaded
methods are methods with the same name signature but either a different number of parameters or
different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning'
an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter
you achieve the effect that you want. Overridden methods are methods that are redefined within an
inherited or subclass. They have the same signature and the subclass definition is used.

Enumerate three (3) examples of Message passing styles


answer
a. Actor model implementation
b. Amorphous computing
c. Antiobjects
d. Flow-based programming
e. SOAP (protocol)

Find the values of the following:


(i) Max(-4, -8) = r
(ii) Min(-6,-9) = s
(iii) Pow(4, 2) = t
(iv) Sqr(900) = u
(v) ABS(-80) = v

Evaluate the value of x after each of the following statement is executed


i. x=Math.abs(-7.5);
ii. x=Math.floor(-8.7);
iii. x=Math.ceil(6.3);
iv. x=Math.floor(4.1);
v. x=Math.ceil(-2.5);
answer

use this diagram below to get what it means incase the parameters are changed.

with the diagram above,you should be able to evaluate or solve the question

(i) Max(-4, -8)

since we finding the larger value here,the larger value here is -4 because -4 is larger than -8 by the
number system.therefore, Max(-4, -8) = -4

(ii) Min(-6,-9)

since we finding the minimum value here,the mininum value here is -9 because -9 is smaller than -6 by
the number system..therefore, Min(-6, -9) = -9

(iii) Pow(4, 2)

since we looking for the power here,we are looking for the value of 4 raise to the power 2 which is
indicated as 42 .therefore, Pow(4, 2) =16

(iv) Sqr(900)

since we looking for the square root here,we looking for the square root of 900 which is 30.therefore we
have Sqr(900) = 30

(v) ABS(-80)
since we looking for the absolute value of -80 here,the absolute value will be giving as the positive value
of the original value which is 80.therefore we have ABS(-80) = 80

x=Math.abs(-7.5);

since we looking at the absolute value of (-7.5) here,the absolute value will be giving as the positive
value of the original value which is 7.5.therefore we have Math.abs(-7.5) =7.5

x=Math.floor(-8.7)

since we looking at rounding the value of (-8.7) to the largest integer not greater than (-8.7),then we will
be having a solution as -9.therefore we have Math.floor(-8.7) = -9

x=Math.ceil(6.3)

since we looking at rounding the value of (6.3) to the smallest integer not less than (6.3),then we will be
having a solution as 6.therefore we have Math.ceil(6.3) = 6

x=Math.floor(4.1)

since we looking at rounding the value of (4.1) to the largest integer not greater than (4.1),then we will
be having a solution as 4.there we have Math.floor(4.1) = 4

x=Math.ceil(-2.5)

since we looking at rounding the value of (-2.5) to the smallest integer not less than (-2.5) ,then we will
be having a solution as -2.therefore we have Math.ceil(-2.5) = -2

With the help of a diagram describe Modularity and its merits


ANSWER
With respect to object-oriented programming, explain the term “downcasting’
answer
A superclass reference can be used to invoke only the methods declared in the superclass, attempting to
invoke a subclass only methods through a superclass give a compilation error. If a program needs to
perform a subclass-specific operation in a subclass object referenced by a superclass variable, the
program must first cast the superclass reference to a subclass reference through a technique known as
downcasting. Downcasting enables a program to invoke subclass methods that are not in the
superclass. The syntax for downcasting is;
subclassname subclassobject = new (subclassname) superclassobject

Write the following acronyms in full (10 marks)


(i) JVM
(ii) DLSs
(iii) WWW
(iv) OOP
(v) OS
(vi) GB
(vii) FTP
(viii) CRT
(ix) ADT
(x) URL
answer

JVM: Java Virtual Machine


DLSs: Dynamic Languages Symposiums
WWW:world wide web
OOP:Object oriented programming
OS:operating system
GB:gigabyte
FTP: File Transfer Protocol
CRT: composition refactoring triangle
ADT: Abstract Data Types
URL:UNIFORM RESOURCE LOCATOR

Mention two advantages of software reusability


answer
 Programmers build more dependable and efficient systems because existing classes and components
often have gone through extensive testing, debugging and performance tuning.
 It saves programmers the time and effort they would have used in re-inventing the wheels.

Discuss Abstract Data Types and state two types


answer
the concept of ADT in computer programming developed as a way of abstracting the common data
structure and the associated operations. An abstract data type (ADT) is characterised by the following
properties which are;
1. It exports a type.
2. It exports a set of operations. This set is called interface.
3. Operations of the interface are the one and only access mechanism to the type's data structure.
4. Axioms and preconditions define the application domain of the type

the two parts of abstract data types are Data and operations

What are the characteristics of abstract data type (ADT)? Name three (3) of them.
answer
1. It exports a type.
2. It exports a set of operations. This set is called interface.
3. Operations of the interface are the one and only access mechanism to the type's data structure.

Write a simple Java Program to print message “I Love Java Programming”


answer

Write short notes on conversion operators


answer
Conversion operators are those that involve converting from one data type to another through
assignment. There are implicit and explicit conversions. Implicit conversions are those that involve direct
assignment of one type to another. Explicit conversions are conversions that require one type to be
casted as another type in order to perform the conversion. Conversions that may cause exceptions or
result in loss of data as the type is converted should be handled as explicit conversions.

Define a Unary operator overloading and show the syntax


answer
Unary operators are those that require a single operand. These include the simple increment (++) and
decrement (--) operators. To declare a unary operator, the following syntax is used:
 public static result-type operator unary-operator (op-type operand)
State and briefly explain three (3) benefits of bundling code into individual software objects
answer
 Modularity: The source code for an object can be written and maintained independently of the source
code for other objects. Once created, an object can be easily passed around inside the system.
 Information-hiding: By interacting only with an object’s methods, the details of its internal
implementation remain hidden from the outside world.
 Code re-use: If an object already exists (perhaps written by another software developer), you can use
that object in your program. This allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.
 Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply
remove it from your application and plug in a different object as its replacement. This is analogous to
fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Define the following terms:


i) Composition?
ii) Abstraction
answer
composition
Composition is a relationship between two classes that is based on the aggregation relationship.
Composition takes the relationship one step further by ensuring that the containing object is responsible
for the lifetime of the object it holds

abstraction
Abstraction is the process or result of generalisation by reducing the information content of a concept or
an observable phenomenon, typically to retain only information which is relevant for a particular
purpose. For example, abstracting a leather soccer ball to a ball retains only the information on the
general ball attributes and behaviour.

Tabularise the differences between a Constructor and a Method?


answer
constructor method
Constructors are special methods that are used to A method is a procedure associated with a
initialise a new object of a class message and an object. ... Data is represented as
properties of the object and behaviors are
represented as methods of the object.

Create a non-executable class that has a method that computes factorial.


answer
Describe what is meant by conversion operator overloading
answer
Conversion operators are those that involve converting from one data type to another through
assignment. There are implicit and explicit conversions. Implicit conversions are those that involve
direct assignment of one type to another. Explicit conversions are conversions that require one type to
be casted as another type in order to perform the conversion

define operator overloading


answer
Operator overloading is the process of adding operator functionality to a class which allows one to
define exactly how the operator behaves when used with your class and other data types

Why does Java not support operator overloading?


answer
Java does not support this but methods can be used to make Java behave like that. Java was designed
not to support operator overloading because it makes Java codes complex but since Java was designed
to make programming easier, the feature was embedded into Java

Mention a programming language that supports method overloading.


answer
C#/ C++
differentiate between method overloading and operator overloading
answer
Method overloading Operator overloading
Method overloading is the process through which Operator overloading is the process of adding
methods of the name are declared in the same operator functionality to a class which allows one
class to define exactly how the operator behaves when
used with your class and other data types

what are the motivations for modularising a program into methods?


answer
 The divide and conquer approach- This makes program development more manageable by
constructing programmes for small, simple pieces.
 Software reusability- This allows existing methods to be used as building blocks to create programmes.
Programmers write programmes from standardised methods rather than building customised codes.

What is an interface? Explain the concept of interfaces in OOP context.


answer
Interfaces are used for assigning common functionality to possible unrelated classes. This allows objects
of unrelated classes to be processed polymorphically. Interfaces define and standardise the ways in
which things such as people and systems can interact with one another.
Interfaces specify what and not how e.g. the controls of a radio serve as an interface between radio
users and a radio’s internal components. The interface specifies what operations a radio must permit
users to perform but does not specify how the operations are performed.

Differentiate between an abstract class and a concrete class


answer
Abstract class Concrete class
Not all classes can be used for object concrete classes are classes that can be used for
instantiations, most times, such classes acts as a object instantiation. Concrete classes provide
superclass and their main work is to make other implementations of every method they declare
classes inherit their methods and to share a whereas abstract classes contain at least one
common design. Such classes are called abstract method that does not have implementation.
classes.

How does polymorphism enable promote software extensibility?


answer
Polymorphism promotes extensibility by allowing new sub-classes and methods to be added to a class
hierarchy without having to modify application programs that already use the hierarchy's interface
Differentiate between Predefined classes and user defined classes
answer
Predefined class User defined classes
They can be reused rather than creating new User defined classes are the classes newly created
classes.they are the building blocks for by a programmer to assist in achieving his desired
constructing new classes. task

Explain the concept behind garbage collection.


answer
The garbage collector is a part of the runtime system that runs in a low- priority thread reclaiming
memory that is no longer needed by objects used by the program. An object becomes eligible for
garbage collection in JAVA when there are no reference variables that reference the object. The garbage
collector makes use of a method finalise to perform its housekeeping. JAVA supports another
mechanism for returning memory to the operating system when it is no longer needed by an object. This
mechanism is called garbage collection

What are destructors?


answer
Destructors are used to tidy up the computer’s memory. They are used to delete unused objects from
the memory. The local variables created within the body of a method are automatically deleted once the
control is outside the method

What is constructor overloading?


answer
Constructor overloading is a concept of having more than one constructor with different parameters
list, in such a way so that each constructor performs a different task. For e.g. Vector class has 4 types of
constructors. .

Differentiate between a default constructor and a no argument constructor?


answer
default constructor A no argument constructor
A default constructor is a constructor that either No-argument Constructor. ... If the parent does
has no parameters, or if it has parameters, all the not have a no-argument constructor, then using
parameters have default values. ... It is implicitly this shorthand causes a syntax error. In our
declared or explicitly defaulted . A has no virtual program the class definition for Video (the
functions and no virtual base classes. superclass) lacks a no-argument constructor. The
proposed constructor (above) calls for such
a constructor so it would cause a syntax error.
What is a default constructor?
answer
A default constructor is a constructor that either has no parameters, or if it has parameters, all the
parameters have default values. ... It is implicitly declared or explicitly defaulted . A has no virtual
functions and no virtual base classes.

what is an argument constructor?


answer
An argument constructor is a constructor that is invoked with arguments. Like a method, it specifies in
its parameter list the data it requires to perform its task

A constructor is like a method but it differs from a method with attributes.list them
answer
- a constructor has no return type.
It cannot even return void.
- a constructor must have the same name as the class.
- a constructor that has no parameter is called a no argument constructor and the one that has
parameter is called an argument constructor

what is a no argument constructor?


answer
A no argument constructor is a constructor that is invoked with arguments. Such a constructor simply
initialises the object as specified in the constructor’s body.

You might also like