Answers To Java IIA Question Bank
Answers To Java IIA Question Bank
Multilevel inheritance involves creating a chain of classes where one class is derived from another.
Example code:
class A {
class B extends A {
class C extends B {
obj.displayA();
obj.displayB();
obj.displayC();
In Java, interfaces are used to achieve multiple inheritance since a class can implement multiple
interfaces. Example:
interface A {
void methodA();
interface B {
void methodB();
class C implements A, B {
obj.methodA();
obj.methodB();
3. Compare and contrast method overloading and method overriding with suitable example.
Method overloading occurs within the same class, methods have the same name but different
parameters. Method overriding occurs in a subclass, where a method from the parent class is
redefined. Examples:
// Overloading
class OverloadingExample {
// Overriding
class Parent {
Inheritance is the mechanism where one class acquires the properties and behaviors of another
class. Types:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Abstract classes in Java have at least one abstract method and cannot be instantiated. Abstract
s.draw();
6. Illustrate the usage of super keyword in Java with a suitable example. Also explain the
The `super` keyword is used to refer to the immediate parent class. Dynamic dispatch determines
class Parent {
@Override
obj.display();
}
7. Develop a Java program to demonstrate polymorphism using a Shape class.
class Shape {
Shape s;
s = new Circle();
s.draw();
s = new Square();
s.draw();
8. Define package. Explain the steps to create a user-defined package with an example.
Example:
package mypack;
Importing allows the use of classes from other packages using the `import` keyword. Example:
import java.util.*;
int n = sc.nextInt();
try {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Inner catch: " + e);
} catch (Exception e) {
Exceptions are errors disrupting normal program flow. Java handles them using `try`, `catch`,
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
12. Build a Java program to create a package 'balance' containing Account class.
Package example:
package balance;
System.out.println("Balance: $1000");
}
// Import package
import balance.Account;
acc.displayBalance();
13. Examine the various levels of access protections available for packages.
Access levels: