!! Alv Mynotes
!! Alv Mynotes
Java
Feature of Java
*Explain the main features of JAVA. [5 marks]
Object Oriented:
Java follows the principles of Object-Oriented Programming, focusing on objects rather than
procedures. supports key OOP concepts such as inheritance, encapsulation, polymorphism, and
abstraction.
Simple: Java is designed to be easy to learn.
Multithreaded:
Java supports multithreading, allowing the execution of multiple tasks simultaneously.
Secure:
Java provides built-in security features; these prevent unauthorized access and safeguard data
during program execution.
High Performance
Java achieves high performance using its Just-In-Time (JIT) compiler,
Platform independent: Unlike many other programming languages including C and C++
, when Java is compiled, it is not compiled into platform specific machine, rather it is compiled
into byte code which can run on any machine with a Java Virtual Machine.
Platform Independent
*Some programming languages may have codes that can’t run on other operating systems unlike JAVA’s
byte code. Explain how this platform independence makes java a better programming language. [4
marks]
Instead, it can be executed on any machine that has a JVM, making Java highly portable and
eliminating the need for recompilation for different platforms.
Unlike other languages that rely on platform-specific compilers, Java ensures that the same code
can run consistently across different operating systems like Windows, macOS, or Linux.
Reduced Development Costs:
Developers save time and effort by not having to write separate versions of the code for different
platforms.
2)
Java Basic Syntax and Programs
*Write a Java code to display your name, address and age. [4 marks]
3. Static Variables: (ITS LIKE INSTANCE but declared with static keyword)
Definition: These are variables declared with the static keyword inside a class but outside any
method, ,
3)
Java Class
o Create Class
*Using a simple example, explain how a class is created in JAVA. (5 marks)
Steps to Create a Class:
1. Define the Class: Use the class keyword followed by the class name. Class names usually start
with an uppercase letter.
2. Add Attributes (Fields): These are variables declared inside the class to represent the object's
state.
3. Define Methods: These are functions declared inside the class to represent the object's
behavior.
4. Create an Object: Use the new keyword to create an instance of the class.
Java Object
*State the THREE characteristics of an object. (3 marks)
1. State:
Represents the data or attributes of the object (e.g., color, name, or value).
Example: A Car object attributes like color = "red" or speed = 100.
2. Behavior:
Represents the actions or operations that the object can perform, defined by its methods.
Example: A Car object might have behaviors like drive(), stop(), or accelerate().
3. Identity:
A unique identifier that distinguishes one object from another, typically represented by the
object's memory address.
Example: Two Car objects with the same attributes (color and speed) are still distinct because
they occupy different memory locations.
*An object is defined as real-world objects have attributes and behaviors. A bank account in
programming is regard as an object. State its attributes and behavior. [2 marks]
Attributes
1. Account Holder Name: The name of the person who owns the account.
2. Account Number: A unique identifier for the account
3. Balance: The amount of money currently in the account
Methods
1. Deposit (): Allows the user to add money to the account.
2. Withdraw (): Allows the user to withdraw money from the account.
3. CheckBalance (): Displays the current balance in the account.
4. Transfer (): Transfers money from one account to another.
*List THREE ways in which an object can be initialized. (3marks)
1. Using Direct Initialization (Field Initialization)
The normal way just
Assigns values to fields directly at the time of declaration.
2. Using Instance Initialization Block
An instance initialization block is a block of code that is executed whenever an object is created
3. Using a Constructor:
A constructor is a special method used to initialize objects when they are created.
Initializes object attributes when the object is created.
Question
*Write a Java program that implements a class named Odd with a data member named x and method
named read that accepts an integer from the keyboard. The program determines whether the integer
is odd or even and displays an appropriate message. Use the if statement. [6 marks]INHERITANCE
4)
Method
A Java method is a collection of statements that are grouped together to perform an operation.
reason for the use of methods
Giving an example, show why methods are useful in java programming [4 marks]
1. Code Reusability: A method can be called multiple times, reducing code repetition.
2. Modularity: Code is broken into smaller units, making it easier to manage.
3. Readability and Maintenance: Methods improve the structure and readability of code.
4. Avoiding Code Duplication: Similar logic can be implemented once and reused.
1. Explain the difference between parameterized methods and non-parameterized methods.
marks]
Parameterized Methods - Methods that take arguments (parameters) as inputs when called.
Eg int add(int a, int b)
The void keyword allows us to create methods which do not return a value.
This method is a void method which does not return any value.
Java does not have the return keyword
A Java method is a collection of statements that are grouped together to perform an operation. Write
down, and explain the syntax for method definition. (5 marks)
*Using a syntax, explain how a method is created in JAVA. [6 marks]
Syntax
modifier returnType methodName(parameters) {
// Method body (statements)
}
Modifier:
Specifies the access level of the method. Common modifiers include:
o public: Accessible from anywhere.
o private: Accessible only within the defining class.
o protected: Accessible within the same package and subclasses.
o No modifier (default): Accessible within the same package.
Return Type:
Specifies the type of value the method will return.
o Examples: int, void (if no value is returned),
Method Name:
The name of the method
Parameters:
Optional. Specifies the input values (data) the method takes.
Method Calling
*Write a code with a method called myMethod () that will print your name when called. (4 mark)
Method Overloading
*State the difference between method overloading and method overriding? [4 marks]
Method overloading - occurs when a class has multiple methods (Functions) with the same name but
different parameter
Method Overriding – occurs when sub class (derived class) defines same function as defined in super
class
*Write a program that compares the values of numbers to explain the concept of method overloading.
[5 marks]
*Using method overloading, write a code to compare the following set of numbers (5.6 and 7.2) and
(11 and 14) to find the minimum and each of the sets. [6 marks]
5)
Java Inheritance
*Explain at least THREE access modifiers in JAVA. [6 marks]
Public:
Definition: When a class, method, or variable is declared as public, it can be accessed from
anywhere in the program.
Private:
Definition: When a class, method, or variable is declared as private, it can only be accessed
within the same class where it is declared. It is the most restrictive access modifier.
Protected:
Definition: The protected access modifier allows the class, method, or variable to be accessed
within its own subclasses
*Write the simple syntax for JAVA inheritance. (4 marks)
Eg
IS-A Relationship
Explain the usage of IS-A relationship as used in JAVA inheritance. [5 Marks]
The IS-A relationship defines that if class B is a subclass of class A, then objects of class B are also
objects of class A. This means that every instance of class B is also considered an instance of
class A.
For example, if Dog is a subclass of Animal, we can say that:
o A Dog IS an Animal.
o A Dog IS a type of Animal.
This relationship allows the subclass (child class) to inherit the behavior (methods) and attributes
(fields) of the parent class
Example down below( outputs animal can eat due to inheriting)
*Write the syntax for inheritance in JAVA using the keyword, extends [4 marks]
1. extends keyword:
Indicates that the Subclass is inheriting from the Superclass.
2. Superclass:
The parent class whose properties and methods are inherited.
3. Subclass:
The child class that inherits properties and methods from the superclass.
o Super Keyword
As a programmer, explain the two circumstances under which you will use the super keyword in
inheritance. [4 marks]
1. Accessing Parent Class Methods or Fields:
If a method in the child class overrides a method in the parent class, the super keyword can be
used to call the parent class's method instead of the overridden version in the child class
2. Accessing a Parent Class Constructor:
The super keyword can be used to call the constructor of the superclass. If the parent class has a
constructor that needs to be called when creating an object of the child class, super is used in
the child class's constructor.
Invoking Superclass Constructor
*How is a super class constructor invoked? (6 marks)
a superclass constructor is invoked in a subclass using the super keyword.
….
*Using a code, explain how the super keyword is used in JAVA inheritance. (6 marks)
Superclass class Animal
Superclass constructor
Superclass method
Subclass class Dog extends Animal
Subclass constructor Calling superclass constructor
Subclass-specific method calling superclass method
6)
Polymorphism
Types of Polymorphism
*State two types of polymorphism. (2 marks)
-Run-time polymorphism (Dynamic polymorphism).
-Compile time Polymorphism (Static polymorphism).
Runtime Polymorphism
*Using an example, explain runtime polymorphism. [6 Marks]
Runtime polymorphism, is a key concept in object-oriented programming where the specific method to
be executed is determined at runtime rather than at compile time. It is achieved through method
overriding
1. Superclass (Animal):
It has a method sound() which prints "Animal makes a sound".
2. Subclass (Dog and Cat):
Both Dog and Cat classes override the sound() method to provide their specific implementations:
o Dog prints "Dog barks".
o Cat prints "Cat meows".
3. Main Method:
We create references of type Animal but assign them objects of Animal, Dog, and Cat.
At runtime, the JVM determines which version of the sound() method to call based on the actual
object type, not the reference type:
o myAnimal.sound() calls the method from the Animal class.
o myDog.sound() calls the method from the Dog class.
o myCat.sound() calls the method from the Cat class.
7)
Abstraction
*Write a JAVA code to show how an abstract class is created. [5 marks]
*Write a program to demonstrate the use of abstract class that has an abstract method.
8) Encapsulation
*Define the term encapsulation as used in Object Oriented programming. [2 marks]
Encapsulation in Object-Oriented Programming is the process of bundling the data (attributes) and the
methods (functions) that operate on that data into a single unit, typically a class.
*State FOUR reasons why we need encapsulation in JAVA. [4 Marks]
*State the advantages of encapsulation. [4 marks]
1. Data Protection and Security
Encapsulation helps in protecting sensitive data by hiding its implementation details and allowing
controlled access through public methods.
2. Reusability and Modularity
Encapsulation makes a class self-contained, making it easier to reuse it in different programs or modules.
3. Improved Code Maintainability:
Since encapsulation isolates the internal details of a class, it is easier to maintain, and modify the code.
4. Improves Debugging and Testing
Encapsulation simplifies testing and debugging by isolating functionality within a class
Data Hiding
Encapsulation is also known as “data Hiding”.
*State FOUR reasons why encapsulation is regarded as data hiding. [4 marks]
1. Restricted Access to Data:
By declaring class attributes as private, encapsulation ensures that they cannot be accessed directly from
outside the class. This prevents unintended modifications to the data.
2. Controlled Access Through Methods:
Encapsulation provides public getter and setter methods to access and modify private attributes.
3. Prevention of Unauthorized Access:
Since the internal details of the class are hidden, external classes or code cannot directly manipulate or
view sensitive data, ensuring security.
4. Hides Implementation Details:
Encapsulation hides the internal logic and data structure of a class from the outside world. External
code interacts only with the public interface (methods), making the internal workings invisible.
*Write a JAVA code to demonstrate the concept of encapsulation. [6 marks]
All this use the code below
*Encapsulation is the process of hiding the implementation details from the user. Using a JAVA code,
explain how the hiding is achieved. [6 Marks]
*Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a
capsule which is mixed of several medicines.
i. Write a code to demonstrate the concept of encapsulation that has only one field with
setter and getter methods. [5marks]
M
m
8)
Files and I/O
1. Using an example, explain how file directories are listed in JAVA. [6 marks]
2. A directory is a File which can contain a list of other files and directories. Explain the two useful File
utility methods, which can be used to create directories [4 Marks]
Streams
Byte Stream Classes
1. Streams are clean way to deal with input/output without having every part of your code understand
the physical. State and explain two streams defined in JAVA. [4 Marks]
2. State at least FOUR important character stream classes. [4 Marks]
3. Using a code, explain how characters are read in Java files. (4 marks)