You are on page 1of 14

Submission Instructions

• All questions should be answered


• The assignment carries 40 marks
• Don’t make a mistake of copying, have original work
• Include screenshots of code snippets from your IDEs
• Make sure you include your name & reg number

QUESTION 1:

A) Explain what Inheritance is


A/ Inheritance in Java is a fundamental concept in object-oriented programming that
allows a class to inherit properties and behaviors from another class
B) What are the advantages of Inheritance
A/ In Java, inheritance provides numerous benefits that enhance the organization,
reusability, and maintainability of code. Some of these advantages are as follows:
Enhanced Code Reusability: Inheritance enables subclasses to inherit both fields and
methods from their superclass. This eliminates the need to duplicate common
functionalities in each subclass, thereby streamlining development and reducing
redundancy.
Promotion of Code Organization: By fostering a hierarchical relationship among
classes, where subclasses represent specialized versions of their superclass, inheritance
facilitates a systematic and coherent code structure. This hierarchical arrangement
simplifies code management and upkeep.
Support for Polymorphism: Inheritance is closely associated with polymorphism, a
fundamental principle in object-oriented programming. Subclasses can be treated as
instances of their superclass, allowing for dynamic method invocation and flexible code
design.
Facilitation of Extensibility: Inheritance permits classes to be extended and modified
without altering the original code. This enables the addition of new functionalities
through the creation of subclasses or the overriding of methods in existing subclasses,
offering a versatile and scalable approach to software development.
C) What are the problems of Inheritance
A/ While inheritance is a powerful feature in Java, there are a few possible problems and
challenges associated with it as well:

1. Tight Association : Changes made to the superclass may unintentionally affect


subclasses due to the close association that inheritance creates between classes.
Because of this close relationship, the codebase may become fragile and difficult to
manage.
2. Rigidity in Hierarchy : It becomes difficult to make changes without affecting
already-written code once an inheritance-based class hierarchy is established. Changes
to superclass implementations may need to be made at several different places due to
the inheritance chain's ability to propagate changes.
3. Diamond Dilemma : This can arise since Java does not support multiple class
inheritance. This occurs when a class inherits from two classes that have the same
superclass, which may lead to ambiguity in method calls and make understanding code
more difficult.
D) Explain with code, why Java doesn't accept multiple inheritance
A/ Because of the diamond problem and in an effort to make the language clean and
easy to maintain, Java does not support multiple inheritance. A class may inherit from
more than one base class through multiple inheritance, which could cause confusion
when resolving method calls. Java only allows single inheritance, which allows a class to
inherit from a single superclass, in order to prevent this problem. To achieve a similar
result to multiple inheritance without the related issues, Java supports interface
implementation, which enables a class to implement several interfaces.
E) Demonstrate with a practical example how you can implement single level inheritance
A/

F) Demonstrate with a practical example how you can implement multi level inheritance
A/

QUESTION 2:

A) Explain what Aggregation in Java is


A/ Aggregation is a Java term that describes a relationship between two classes where
one class references another class, but the referenced class is not dependent on the
contained class. A "has-a" or "contains-a" link between things is embodied in this
association.
B) What are the advantages of Aggregation
A/ Aggregation in Java presents various benefits within object-oriented programming:
1. Improved Code Reusability: Aggregation makes it possible to create classes with
fewer, more reusable parts. By using pre-existing classes, this technique helps
developers minimize redundancy, prevent code duplication, and promote reusability.
2. Enhanced Modularity and Flexibility: Aggregation makes the design paradigm
more adaptable and modular. Classes can be built from smaller components, which
makes codebase growth, maintenance, and comprehension easier. This modular
strategy encourages adaptation and scalability to changing needs.
3. Better Encapsulation: By permitting items to maintain their unique states and
behaviors, aggregation promotes better encapsulation. By keeping each object
independent and encapsulating its data and operations, dependencies are decreased
and code structure is improved.
4. Representation of "Has-a" Relationships: Aggregation mimics real-world situations
by modeling "has-a" relationships between things. A university "has" departments, a car
"has" an engine, and a library "has" books. This representation closely follows domain
models, improving the readability and clarity of the codebase.
C) What are the problems of Aggregation
A/ While aggregation has many benefits, there are several drawbacks and possible
problems with it for Java developers:
- Complexity Increasing: Aggregation can increase complexity, especially when
determining links between multiple things. Understanding and handling these
interactions can become challenging as the codebase grows in size and complexity.
- Dependency Management: Class dependencies may become more severe as a result
of aggregation. Changes made to one class may have cascading consequences on other
dependant classes, making the codebase more difficult to maintain and brittle.
- Ownership and Lifecycle Supervision: Different ownership and lifecycle management
procedures can be required for aggregated items. It might be difficult to ensure proper
initialization, cleanup, and destruction of aggregated objects, particularly in settings with
many threads.
- Tight Object Coupling: Aggregation can promote tight coupling between classes,
allowing changes made to one class to affect another.
D) Demonstrate with a practical example how you can implement Aggregation in Java
A/
QUESTION 3:

A) What are inner classes in Java


A/ Classes defined inside another class are referred to as inner classes in Java,
sometimes known as nested classes. These internal classes have access to all of the
enclosing class's members, including private members. If the local variables and
parameters of surrounding methods are marked as final or practically final, they can also
access them.
B) Demonstrate with a practical example how you can implement inner classes in Java
A/

QUESTION 4:

A) What is encapsulation in Java?


A/ In Java, encapsulation is the process of organizing related code (methods) and data
(variables) into a logical unit called a class. By doing this, an object's internal state is
kept hidden from the outside world and interaction is limited to approved, open-access
channels.
B) Demonstrate with a practical example how you can implement encapsulation in Java
A/

QUESTION 5:
A) Java supports method overloading, explain what it means
A/ Method overloading in Java refers to the ability to create multiple methods in a
single class with the same name but different parameter lists. This functionality allows
for the existence of numerous methods with the same name in the same class, as long
as they have different signatures that include different counts or kinds of parameters.
B) Explain the advantages of method overloading
A/ Method overloading in Java presents several benefits, enhancing the flexibility, clarity,
and reuse potential of code:
In Java, method overloading improves the code's reusability, flexibility, and clarity in a
number of ways.
1. Improved Clarity: By allowing developers to use the same method name for related
activities, method overloading enhances the readability and clarity of code. The goal of
each method can be clearly communicated by developers by using descriptive method
names and packing them with variations.
2. Simplified Naming: Method overloading enables developers to use a single method
name for all variants, saving time and effort when creating several names for related
activities with different argument counts or types. This lowers the codebase's overall
complexity and streamlines naming conventions.
3. Improved Reusability: By enabling developers to give a single method name to
several variants of a task, method overloading promotes code reusability. hism.
C) Demonstrate with a practical example how you can implement method overloading
in Java
A/

QUESTION 6:

A) Java supports method overriding, explain what it means


A/ In Java, method overriding is a feature of object-oriented programming that allows a
subclass to give a particular implementation of a method that is already defined in its
superclass. This implies that a subclass can modify a method's functionality that it
inherited from its superclass to better fit its needs.
B) Explain the advantages of method overriding
A/ Method overriding in Java presents numerous advantages that enhance the
flexibility, extensibility, and maintainability of object-oriented code:
1. Polymorphism: Polymorphism is made possible by method overriding, which allows
an object of a subclass to be considered as an object of its superclass. This promotes
code flexibility and adaptability by enabling dynamic method invocation, in which the
suitable method implementation is selected at runtime based on the actual object type.
2. Code Reusability: By enabling subclasses to provide their own implementations of
methods inherited from superclasses, method overriding promotes code reusability. This
reduces redundancy and improves code maintainability by avoiding code duplication
and encouraging the reuse of preexisting method names and signatures.
3. Increased Flexibility: The ability to modify and customize class behavior is made
possible via method overriding.
C) Demonstrate with a practical example how you can implement method overriding in
Java
A/

QUESTION 7:

A) Explain what polymorphism is in Java


A/ Polymorphism in Java denotes the capacity of objects to manifest multiple forms,
enabling them to showcase varying behaviors contingent upon their actual types or the
context in which they're employed. This foundational principle is pivotal in object-
oriented programming and is facilitated through inheritance, method overriding, and
method overloading.
There are two main categories of polymorphism in Java:
1. Polymorphism in Compile Time: build-time polymorphism, also known as static
polymorphism, occurs when the compiler determines which method or operation to
perform at build time based on the method signature or action. This is achieved by
defining many methods with the same name but different parameters inside of a single
class—a practice known as method overloading. During compilation, the compiler
chooses the appropriate approach based on the arguments passed to it.
2. Run-Time Polymorphism: Also known as dynamic polymorphism, run-time
polymorphism occurs when the method that is to be performed is chosen at runtime
based on the true type of the object that is being linked to. This is accomplished by a
subclass providing a unique implementation of a method that is already defined in its
superclass, a process known as method overriding. When a superclass object is invoked,
the JVM determines which version of the method to run depending on the object's
actual type at runtime.
B) Demonstrate with a practical example how you can implement runtime
polymorphism in Java
A/

QUESTION 8:

A) Explain what you understand by the term "Abstraction in Java"


A/ In Java, abstraction is the idea of expressing key functionalities without incorporating
implementation or background information. It entails stripping out superfluous features
and concentrating on the important elements of complicated systems in order to
simplify them. Java allows for abstraction through the use of abstract classes and
interfaces.
B) Demonstrate with a practical example how you can implement Abstraction in Java
with abstract classes
A/

C) Demonstrate with a practical example how you can implement Abstraction in Java
with interfaces
A/
D) Demonstrate with a practical example how you can implement inheritance within
interfaces
A/

QUESTION 9:

A) Explain what a package is in Java


A/ A package in Java is a method that is used to group classes and interfaces into
different namespaces, providing a way to classify and organize similar code elements. A
package serves as a container for a group of related classes, interfaces, and maybe sub-
packages. This provides a hierarchical structure for the arrangement of Java code.
Here are some important things to know about Java packages:
1. Namespace: By providing a unique namespace for the enclosed classes and
interfaces, packages are essential in preventing naming conflicts. Classes in the same
package can have the same name without conflicting with classes in other packages.
2. Organization: Coders can systematically arrange their codebase with the help of
packages. Codebase administration and maintenance are greatly streamlined by
combining related classes and interfaces into a package.
3. Access Control: Packages with access modifiers like "public," "protected," and
"private" provide some level of access control. While p̀rotected`components can only be
accessed by subclasses and within the package, p̀ublic` elements can be accessed from
outside the package. On the other hand, {private} elements are only available within the
class.
4. Import Statements: Java uses the ìmport` statement to make it easier to use classes
and interfaces from other packages. This directive eliminates the need for fully qualified
class names by enabling developers to specifically identify the classes and packages
they want to use in their code.
B) Why do we need packages in Java
A/ Packages play several vital roles in Java development, rendering them indispensable:
1. Structural Organization: Code can be systematically arranged into logical groups by
using packages, which makes it easier to classify related classes and interfaces. This
codebase's hierarchical structure provides structure, which is necessary for overseeing
and navigating large projects with multiple classes.
2. Namespace Administration: By providing distinct namespaces for the classes and
interfaces they contain, packages help to avoid naming conflicts. Classes in one package
can have the same name as another without causing conflicts with classes in other
packages. This kind of namespace separation removes uncertainty and promotes clarity.
3. Access Control: By providing access control mechanisms, packages enable
developers to control which classes, interfaces, and members are visible within a
package. Code integrity is preserved via access modifiers such as p̀ublic}, p̀rotected,̀ and
p̀rivate}, which control which elements are accessible within a package.
4. Encapsulation and Modularization: By drawing clear boundaries inside the
codebase, packages promote modularity and encapsulation. They allow implementation
details to be hidden while providing other program segments with access to necessary
interfaces. By using this technique, interdependencies are reduced and code
sustainability is improved.
C) Demonstrate with a practical example how you can implement packages in Java
A/

QUESTION 10:

A) Explain what constructors in Java are


A/ In Java, constructors are special functions that belong to a class and are responsible
for initializing class objects. They are automatically called when an object is created with
the ǹew} keyword. Constructors, who have the same name as the class, are responsible
for setting instance variables' initial values, carrying out setup procedures, and ensuring
the object's validity when it is instantiated.
B) Demonstrate with a practical example how you can implement constructors in Java
A/

QUESTION 11:

A) Java supports arrays, explain what an array is.


A/ In Java, an array is a data structure that stores a fixed-size collection of elements of
the same data type. Several values of the same type can be easily stored and accessed
under a single variable name with the help of arrays. An array's index, which indicates
each element's location inside the array, can be used to access any element.
B) Explain the advantages of using arrays in Java
A/ Java array usage has the following advantages:
1. Optimal Storage: When compared to separate variable declarations, arrays provide
an efficient memory use strategy by providing a contiguous memory block for the
storage of components of the same data type.
2. Direct Element Access: No matter where they are in the array, elements can be
quickly and effectively retrieved by using their index positions to gain direct access to
them.
3. Seamless Iteration: By enabling simple operations on each array element
sequentially, arrays make element iteration simpler through the use of structures like the
for-loop.
4. Streamlined programming: By enabling the succinct processing of data collections,
arrays simplify programming. Code is cleaner and more compact when only one array
variable is needed rather than keeping track of separate variables for each element.

C) Demonstrate with a practical example how you can implement 1 Dimensional arrays
in Java
A/

D) Demonstrate with a practical example how you can implement 2 Dimensional arrays
in Java
A/

E) Demonstrate with a practical example how you can implement 3 Dimensional arrays
in Java
A/

F) Demonstrate with a practical example how you can implement Jagged arrays in Java
A/

QUESTION 12:

A) Explain what static, local and instance variables are in Java


A/ - Static variable are variable declared in a class with the word "static" at the
beginning but out of the methode
- Instance variable are variable declared in a class but out of the methode
- Local variable are variable declared inside of the methode
B) Demonstrate with a practical example how you can use static, local and instance
variables in Java
A/

QUESTION 13:

A) What are methods in Java


A/ A method is a block of code which only runs when it is called.
B) Demonstrate with a practical example how you can use methods in Java
A/

QUESTION 14:

A) Demonstrate with a practical example how you can use the util package in Java
B) Demonstrate with a practical example how you can use the math package in Java
A/
QUESTION 15:

A) Java supports conditional statements for java application execution redirection,


explain the different conditional statements supported by Java
A/ A Java application's execution flow can be controlled by using one of the many
conditional statements that Java allows. The primary conditional statements that Java
supports are as follows:
1. if Statement: If a given condition is true, a block of code is executed using the if
statement. If the condition evaluates to false, an optional else block may be executed
after it.
2. else-if Statement: The else-if statement allows you to specify multiple conditions to
be tested sequentially. If the first condition is false, subsequent else-if conditions are
evaluated until a true condition is found, and the corresponding block of code is
executed.
3. switch Statement: The switch statement is used to select one of many code blocks to
be executed based on the value of an expression. It provides a more concise way to
write multiple if-else statements when testing for multiple possible values of a single
variable.
B) Demonstrate with a practical example how you can use if statements in Java
A/

C) Demonstrate with a practical example how you can use if-else statements in Java
A/

D) Demonstrate with a practical example how you can use switch statements in Java
A/

E) Demonstrate with a practical example how you can use break statements in Java
A/
F) Demonstrate with a practical example how you can use continue statements in Java
A/

QUESTION 16:

A) Explain what a loop is in Java


A/
A loop is a control structure in Java that lets you run a block of code continuously as
long as a certain condition is met. Loops are necessary for iterating over data sets and
automating repetitive processes.
B) Demonstrate with a practical example how you can use for loops in Java
A/

C) Demonstrate with a practical example how you can use for each loops in Java
A/

D) Demonstrate with a practical example how you can use while loops in Java
A/

E) Demonstrate with a practical example how you can use do while loops in Java
A/

You might also like