0% found this document useful (0 votes)
18 views

Java Assignment

Java Assigment
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java Assignment

Java Assigment
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit-01: JAVA

1. Discuss all the three usages of final keyword?


1. Final Variables:
- When a variable is declared as `final`, its value cannot be changed once it's
initialized.
- Final variables must be initialized either at the time of declaration or within the
constructor of the class.
- It's often used to define constants in Java programs, ensuring that their values
remain constant throughout the program execution.
- Example:
public class Example {
final int MAX_VALUE = 100;
final double PI;

public Example() {
PI = 3.14;
}

// Attempting to reassign a final variable will result in a compilation error


// MAX_VALUE = 200; // Compilation error
}

2. Final Methods:
- When a method is declared as `final`, it cannot be overridden by subclasses.
- This is useful when you want to prevent subclasses from changing the behavior
of a particular method.
- Final methods are implicitly `final` in Java classes marked as `final`.
- Example:
public class Parent {
public final void display() {
System.out.println("This is a final method.");
}
}

public class Child extends Parent {


// Attempting to override the final method will result in a
compilation error
// public void display() {
// System.out.println("This is an overridden final method.");
// }
}
3. Final Classes:
- When a class is declared as `final`, it cannot be subclassed. In other words, you
cannot extend a `final` class.
- This is useful when you want to prevent inheritance or subclassing for security,
optimization, or design reasons.
- Example:
final public class FinalClass {
// Some code here
}
// Attempting to extend a final class will result in a compilation error
// public class Subclass extends FinalClass {} // Compilation error
These usages of the `final` keyword provide various mechanisms for ensuring
immutability, preventing method overriding, and prohibiting subclassing, contributing
to the robustness and security of Java programs.

2. What is the use of this keyword?

3. What is a constructor? Does java provide default constructor? Does java provide a
default copy constructor? Explain with answer with suitable example.
4. Explain any 5 string class methods in detail.
5. What is private constructor?
6. Explain in detail the features of java.
7. Explain why the main function is declared as “public static void main(String args[])”?
8. Explain the concept of abstract classes in java.
9. What is the difference between Integer and int?
10. Explain jdk,jvm and jre .

You might also like