Java_Assignment2_Full
Java_Assignment2_Full
1. Method Overloading
Theory: Method Overloading allows multiple methods in the same class to have the same name but differe
Code:
class OverloadingExample {
void display(int a) {
void display(String b) {
obj.display(10);
obj.display("Hello");
// Output:
// Integer: 10
// String: Hello
2. Static
Theory: The static keyword in Java is used for memory management. It can be applied to variables, method
Static members belong to the class rather than any specific instance.
Code:
class StaticExample {
StaticExample() {
count++;
new StaticExample();
new StaticExample();
StaticExample.showCount();
// Output:
// Count: 2
3. Nesting of Methods
Theory: Nesting of methods refers to calling one method inside another method within the same class.
Code:
class NestingExample {
void outerMethod() {
System.out.println("Outer method");
innerMethod();
}
void innerMethod() {
System.out.println("Inner method");
obj.outerMethod();
// Output:
// Outer method
// Inner method
4. Inheritance
Theory: Inheritance is a mechanism where one class (child) acquires the properties and behaviors of anoth
Code:
class Parent {
void show() {
System.out.println("Parent class");
void display() {
System.out.println("Child class");
}
public class InheritanceExample {
obj.show();
obj.display();
// Output:
// Parent class
// Child class
5. Overriding
Theory: Method Overriding occurs when a subclass provides a specific implementation of a method already
Code:
class Base {
void show() {
System.out.println("Base class");
@Override
void show() {
System.out.println("Derived class");
obj.show();
// Output:
// Derived class
Theory: The final keyword is used to restrict modification. It can be applied to variables (to make them cons
Code:
class FinalExample {
System.out.println("Final method");
obj.show();
// Output:
// Final method
7. Finalization
Theory: Finalization is a mechanism where an object's cleanup code is executed before it is garbage collec
Code:
class FinalizationExample {
obj = null;
System.gc();
// Output:
8. VARARGS
Theory: VARARGS (Variable-Length Arguments) allows passing multiple arguments of the same type to a
Code:
class VarargsExample {
}
System.out.println();
display(1, 2, 3);
// Output:
// 1 2 3
// 10 20 30 40 50
Theory: An abstract class is a class that cannot be instantiated and can contain abstract methods (without
Code:
void show() {
System.out.println("Concrete implementation");
obj.show();
}
// Output:
// Concrete implementation
Theory: Dynamic Method Dispatch is a mechanism where a method call is resolved at runtime based on th
Code:
class ParentClass {
void display() {
System.out.println("Parent display");
void display() {
System.out.println("Child display");
obj.display();
// Output:
// Child display
11. Visibility Control
Theory: Visibility control determines the accessibility of class members using access modifiers: private, pro
Code:
class VisibilityExample {
void show() {
obj.show();
// Output:
// Private: 10
// Protected: 20
// Public: 30
// Default: 40