Java Answer
Java Answer
void defaultMethod() {
System.out.println("This is a method with
default access.");
}
}
Behavior:
●
Accessible only from within the same package.
Example:
final int MAX_ATTEMPTS = 3;
At declaration: You can assign a value to the final variable directly when
declaring it.
When applied to a method, the final keyword prevents that method from
being overridden by subclasses. This can be useful for ensuring that a critical
method's behavior is not altered in subclasses, preserving the intended
functionality.
Example:
class BaseClass {
}
}
When applied to a class, the final keyword prevents that class from being
extended. This is useful for creating classes that should not have subclasses,
ensuring that their implementation remains unchanged.
Example:
// ...
class Shape {
// // Class members
// }
The method in the subclass must have the same name as the method
in the superclass.
The method in the subclass must have the same type signature (same
parameters and return type) as the method in the superclass.
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String[] args) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
In this example:
Important:
Static Methods: Interfaces can also have static methods, which are called
using the interface name and don't require an object instance.
interface IntStack {
}
Implementation (FixedStack.java):
FixedStack(int size) {
tos = -1;
Usage (IFTest.java):
class IFTest {
}
Here's a Java program demonstrating the working of an interface:
interface Resizable {
this.width = width;
this.height = height;
@Override
if (width > 0) {
this.width = width;
} else {
}
}
@Override
if (height > 0) {
this.height = height;
} else {
System.out.println("Original Rectangle:");
rectangle.display();
// Resize the rectangle using the Resizable methods
rectangle.resizeWidth(15);
rectangle.resizeHeight(25);
System.out.println("Resized Rectangle:");
rectangle.display();
Explanation:
●In the Main class, a Rectangle object is created with initial dimensions. The
resizeWidth and resizeHeight methods (from the Resizable interface) are
called on the rectangle object to change its dimensions.
●
A class can implement multiple interfaces, allowing it to inherit behavior
from different sources.
1.
2.
3.
In this example, the BoxWeight class extends the Box class. This means a
BoxWeight object possesses all the features of a Box object (width, height,
depth, and the volume() method) and additionally incorporates a weight
attribute. This showcases the "is-a" relationship: a BoxWeight is a type of
Box.
The sources explain that inheritance using extend plays a vital role in
creating multilevel hierarchies. In such scenarios, a class can inherit from a
superclass, which itself inherits from another class. The program illustrates
this by creating a Shipment class that extends BoxWeight, forming a three-
level hierarchy (Box -> BoxWeight -> Shipment).
The sources illustrate this concept using an IntStack interface, which defines
the behavior of a stack data structure. The IntStack interface, presented
below, declares two methods: push for adding an element and pop for
retrieving and removing an element from the stack.
The sources provide an example using the IntStack interface where two
default methods, popNElements and skipAndPopNElements, share a
common code segment:
d. Object Class: Sitting at the pinnacle of Java's class hierarchy is the Object
class. It's the ultimate ancestor, meaning every class you define in Java
automatically inherits from it, even if you don't explicitly use the extend
keyword. This inheritance grants all Java objects access to the methods
defined in the Object class. These methods include:
equals(Object obj): Used to compare the current object with another object
for equality.
The sources emphasize that a reference variable of type Object can hold a
reference to an object of any other class. This universality stems from the
fact that Object is the superclass of all classes.
In the Main class, even though the animal reference is of type Animal, it
points to a Dog object. When makeSound is called on animal, the Dog class's
makeSound method is invoked, resulting in the output "Woof!". This
selection, happening at runtime, exemplifies dynamic method dispatch
A package in Java is a mechanism for organizing related classes and interfaces into
a single unit. It acts like a folder or directory in a file system, grouping similar files
together. The package declaration appears at the beginning of a source file,
indicating which package the classes and interfaces within that file belong to.
Example:
package MyPack;
class Balance {
// ...
}
This code snippet declares a package named MyPack, and the Balance class is
now part of this package.
1. Defining a Package:
Example:
package mypack;
This code defines a package called mypack. Any classes declared in this file will
be part of the mypack package.
2. Storing in Directories:
The package structure is mapped directly to the file system. For example, the
Calculator.class file created from the above code must be stored in a
directory named mypack.
3. Importing Packages:
To use classes from another package, you use the import statement.
Example:
Here, the CalculatorDemo class imports the Calculator class from the
mypack package. This allows it to create and use instances of the Calculator
class.
Packages, in conjunction with access modifiers, control which classes can access
members of other classes.
5. Package Hierarchy:
class Shape {
System.out.println("Drawing Shape");
System.out.println("Erasing Shape");
@Override
System.out.println("Drawing Circle");
}
@Override
System.out.println("Erasing Circle");
@Override
System.out.println("Drawing Triangle");
@Override
System.out.println("Erasing Triangle");
@Override
System.out.println("Drawing Square");
}
@Override
System.out.println("Erasing Square");
shape1.draw();
shape1.erase();
shape2.draw();
shape2.erase();
shape3.draw();
shape3.erase();
}
**Explanation:**
* **Base Class (Shape):** The `Shape` class acts as a blueprint for all
shapes. It defines the basic `draw()` and `erase()` methods.
**Output:**
Drawing Circle
Erasing Circle
Drawing Triangle
Erasing Triangle
Drawing Square
Erasing Square
**Key Points:**