Professional Documents
Culture Documents
JAVA Mid
JAVA Mid
class Ques1{
public static void main(String[] args) {
// Single Inheritance
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Multilevel Inheritance
class BabyDog extends Dog {
void weep() {
System.out.println("Baby dog is weeping");
}
}
// Hierarchical Inheritance
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
// Multilevel Inheritance
BabyDog babyDog = new BabyDog();
babyDog.eat();
babyDog.bark();
babyDog.weep();
// Hierarchical Inheritance
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
package com.example.geometry;
Now, let's create a main class outside the packages to demonstrate how
to import and access classes from packages:
import com.example.math.Calculator;
import com.example.geometry.Rectangle;
b. Super
c. super is a reference variable that is used to refer to the immediate parent
class object.
d. It can be used to call methods of the superclass, access superclass
constructor, or access superclass variables.
class Superclass {
public void display() {
System.out.println("Inside superclass");
}
}
c. final:
final is a keyword that can be applied to variables, methods, and classes.
When applied to a variable, it indicates that its value cannot be changed once
initialized.
When applied to a method, it indicates that the method cannot be overridden
by subclasses.
When applied to a class, it indicates that the class cannot be subclassed.
public class Circle {
private final double PI = 3.14;
private final int radius;
public Circle(int radius) {
this.radius = radius;
}
public final double calculateArea() {
return PI * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("Area of circle: " +
circle.calculateArea()); // Output: Area of circle: 78.5
}
}
d. static:
interface Greeting {
void greet();
}
Explanation:
Private: Members with private access level are accessible only within the same
class. They are not accessible outside of the class, including subclasses and
other classes in the same package.
Default (Package-private): Members with default access level are accessible
within the same package. They are not accessible outside of the package,
including subclasses and classes in other packages.
Protected: Members with protected access level are accessible within the
same package and by subclasses, even if they are in a different package.
However, they are not accessible by other classes outside of the package that
are not subclasses.
Public: Members with public access level are accessible from any other class.
They are visible to all classes, regardless of the package or inheritance
relationship.
// ClassA.java
package com.example.packageA;
// ClassB.java
package com.example.packageB;
import com.example.packageA.ClassA;
// Main.java
import com.example.packageA.ClassA;
import com.example.packageB.ClassB;
Exceptions Hierarchy:
1.Synchronized Methods:
You can use the synchronized keyword to make an entire method synchronized.
This means that only one thread can execute the synchronized method of an object
at any given time.
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized void decrement() {
count--;
}
public synchronized int getCount() {
return count;
}
2.Synchronized Blocks:
You can use the synchronized keyword to create a synchronized block of code
within a method. This allows you to synchronize specific sections of code
rather than entire methods.
class SharedResource {
private int sharedData = 0;
public void performTask() {
synchronized(this) {
// Synchronized block
sharedData++;
}
}
}
3.Static Synchronization:
You can also use the synchronized keyword with static methods or blocks to
synchronize access to static variables or methods across all instances of a
class.
class SharedResource {
private static int sharedData = 0;
public static synchronized void increment() {
sharedData++;
}
public static synchronized int getSharedData() {
return sharedData;
}
}
3.How do you place a try block with in another try block for two
different exceptions? Explain with program
public class NestedTryExample {
public static void main(String[] args) {
try {
// Outer try block
int[] numbers = {1, 2, 3};
System.out.println("Outer try block starts");
try {
// Inner try block
System.out.println("Inner try block starts");
System.out.println("Accessing element at index 3: " +
numbers[3]);
System.out.println("Inner try block ends");
} catch (ArrayIndexOutOfBoundsException e) {
// Catch block for ArrayIndexOutOfBoundsException
System.err.println("Inner catch block: Array index out of
bounds");
}
class SharedResource {
private int data;
private boolean newDataAvailable = false;
producer.start();
consumer.start();
}
}