java assignment 2 ans another pdf
java assignment 2 ans another pdf
Discuss the various forms of inheritance and provide a sample program to illustrate the
concept of single inheritance
Forms of Inheritance
1. Single Inheritance
A class inherits from a single parent class.
Example: Class B inherits from Class A.
2. Multilevel Inheritance
A class inherits from a class, which in turn inherits from another class.
Example: Class C inherits from Class B, which inherits from Class A.
3. Hierarchical Inheritance
Multiple classes inherit from a single parent class.
Example: Classes B, C, and D inherit from Class A.
4. Multiple Inheritance (Not directly supported in Java due to the "Diamond Problem")
A class inherits from multiple parent classes. This is achieved in Java using interfaces.
5. Hybrid Inheritance
A combination of two or more types of inheritance. Java handles this through interfaces.
---
class Animal {
String name = "Animal";
void display() {
System.out.println("I am an " + name);
}
}
void showBreed() {
System.out.println("I am a " + breed);
}
}
3. Explain the application of the super keyword in Java with a relevant example.
The super keyword in Java is a reference variable used to access members of the parent
class. It is particularly useful in inheritance to avoid ambiguity when the child class overrides
methods or has variables with the same name as the parent class.
Example:
class Animal {
String name = "Animal";
void sound() {
System.out.println("Animals make sound");
}
}
void sound() {
super.sound();
System.out.println("Dogs bark");
}
void displayNames() {
System.out.println("Parent name: " + super.name);
System.out.println("Child name: " + name);
}
}
Abstract Keyword
1. Abstract Class:
Cannot be instantiated.
May have both abstract methods (methods without implementation) and concrete methods
(methods with implementation).
2. Abstract Method:
Final Keyword
The final keyword is used to declare constants, methods, or classes that cannot be modified:
1. Final Variable:
3. Final Class:
---
Example Program
5. Explain method overriding and dynamic method dispatching in java with example .
Method overriding occurs when a subclass provides a specific implementation for a method
already defined in its parent class.
The method in the child class must have the same name, return type, and parameters as the
method in the parent class.
It allows runtime polymorphism.
---
Dynamic method dispatch (or runtime polymorphism) is the process where the method to be
executed is determined at runtime based on the object type, not the reference type.
This is achieved using method overriding and enables flexibility by allowing behavior to be
defined based on the actual object being referred to.
---
Example Program
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}
Java does not support multiple inheritance directly for classes (i.e., a class cannot inherit
from more than one class) due to the diamond problem. However, you can achieve multiple
inheritance in Java through interfaces. A class can implement multiple interfaces, allowing it
to inherit behavior from multiple sources.
How to Achieve Multiple Inheritance in Java
Using Interfaces: A class can implement multiple interfaces, each providing a set of methods
that the class must implement. This achieves the effect of multiple inheritance.
---
interface Animal {
void sound();
}
interface Mammal {
void habitat();
}
8. Define package. Explain the steps involved in creating a user-defined package with an
example.
Package in Java
A package in Java is a way to group related classes and interfaces together. It helps in
organizing the code and avoids name conflicts. Packages also make it easier to manage and
maintain large software projects. Java provides two types of packages:
2. Create Classes in the Package: Define the classes or interfaces that will be part of the
package.
3. Compile the Classes: Compile the classes, and the .class files will be created in a folder
named after the package.
4. Access the Package in Another Class: Use the import statement to access the classes
from the user-defined package.
File: HelloWorld.java
Now, we create another class outside the package to access the HelloWorld class.
File: TestPackage.java
import mypackage.HelloWorld;
public class TestPackage {
public static void main(String[] args) {
HelloWorld obj = new HelloWorld(); obj.displayMessage();
}
}
javac HelloWorld.java
javac TestPackage.java
java TestPackage
9. Examine the various levels of access protections available for packages and their
implications
with suitable example.
Java provides several levels of access protection that determine the visibility and
accessibility of classes, methods, and variables across different packages. The four primary
access levels are:
1. public:
The member is accessible from anywhere, both inside and outside the package.
2. protected:
The member is accessible within the same package and by subclasses (including
subclasses in different packages).
---
public:
protected:
The member can be accessed by classes within the same package and subclasses in any
package.
default:
private:
Example:
// File: AccessExample.java
// File: TestAccess.java
In Java, you can use multiple catch blocks to handle different types of exceptions. This
allows you to catch and handle multiple exceptions that might arise from a single try block.
You can have multiple catch blocks following a single try block, each catching a different type
of exception.
Java will execute the first matching catch block and skip the rest.
Example Program
15. Describe how a divide-by-zero error can be managed in Java, providing an example
In Java, a divide-by-zero error can occur when you attempt to divide a number by zero. This
typically results in an ArithmeticException. To handle this error, you can use a try-catch block
to catch and manage the exception gracefully.
For floating-point numbers (float or double), dividing by zero does not throw an exception;
instead, it results in Infinity or NaN (Not a Number).
try {
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
}
}
Java provides several built-in exception classes to handle different types of errors and
abnormal situations that may occur during the execution of a program. These exception
classes are part of the java.lang package. Some commonly used built-in exception classes
include:
1. ArithmeticException: Thrown when an arithmetic error occurs, such as dividing by zero.
Example:
public class SimpleExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
try {
String str = null;
System.out.println(str.length());
}
catch (NullPointerException e) {
System.out.println("Error: Null pointer exception.");
}
}
}