JAVA Unit II
JAVA Unit II
interface Pet {
void play();
}
interface Pet {
void play();
}
void display() {
System.out.println("Name: " + this.name); // Refers to the instance
variable
}
}
2. Invoking current class methods:
o this can be used to call other methods in the current class.
Example:
class Calculator {
void add(int a, int b) {
this.printResult(a + b); // Calling another method in the same class
}
Car(String model) {
this.model = model;
}
void showCar() {
this.display(this); // Passing the current object
}
}
4. Calling the constructor of the current class:
o this() can be used to invoke another constructor of the same class.
Example:
class Person {
String name;
int age;
Person(String name) {
this.name = name;
}
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
void display() {
System.out.println("Dog's color: " + color); // Refers to Dog's color
System.out.println("Animal's color: " + super.color); // Refers to
Animal's color
}
}
Definition:
1. Method Overloading:
o Methods with the same name but different parameter lists in the same
class.
o The compiler resolves which method to call based on the method
signature at compile time.
2. Method Overriding:
o A subclass provides a specific implementation of a method already
defined in its superclass.
o This enables runtime polymorphism, where the actual method invoked is
determined at runtime.
3. Abstract Classes:
o A class that cannot be instantiated directly and may contain abstract
methods that must be implemented by subclasses.
o Used as a blueprint for other classes to extend and provide concrete
implementations.
These concepts are fundamental for writing modular, reusable, and maintainable
object-oriented programs in Java.
Dynamic Method Dispatch in Java
Dynamic Method Dispatch (also known as runtime polymorphism) is a
mechanism in Java where a call to an overridden method is resolved at runtime rather
than compile-time. This is possible due to method overriding, where a subclass
provides a specific implementation of a method that is already defined in its
superclass.
In dynamic method dispatch, the object type (not the reference type) determines
which method gets called, making it a key feature of polymorphism.
How Dynamic Method Dispatch Works:
1. A method is overridden in the subclass.
2. The reference variable of the superclass points to an object of the subclass.
3. The method that gets called is based on the object type (the actual object, not
the reference type), and it is determined at runtime.
Example of Dynamic Method Dispatch:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
2. Implementation of Interfaces
To implement an interface, a class must:
Use the implements keyword.
Provide concrete implementations for all the abstract methods declared in the
interface.
Example of Implementing an Interface:
// Define an interface
interface Animal {
void sound(); // Abstract method
void eat(); // Abstract method
}
class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Creating an object of the Dog class
animal.sound(); // Outputs: Dog barks
animal.eat(); // Outputs: Dog eats meat
}
}
Explanation:
The Animal interface defines two methods: sound() and eat().
The Dog class implements the Animal interface and provides concrete
implementations for both methods.
In the Main class, we create a Dog object and use it via the Animal reference.
3. Extending Interfaces
An interface in Java can extend another interface, allowing a subclass to inherit
multiple method declarations from more than one interface. This is a way of
achieving multiple inheritance of method signatures, unlike classes, which can only
inherit from one class.
Syntax for Extending an Interface:
interface InterfaceA {
void methodA();
}
interface InterfaceB {
void methodB();
}
@Override
public void eat() {
System.out.println("Bulldog eats bones");
}
@Override
public void sleep() {
System.out.println("Bulldog sleeps in the kennel");
}
}
Note:
1. try-catch Block:
o Used to catch and handle exceptions. The code that may throw an
exception is placed inside the try block, and the exception is caught and
handled inside the catch block.
2. throw:
o Used to explicitly throw an exception.
3. throws:
o Used in method declarations to specify that the method may throw
exceptions, passing the responsibility of handling the exception to the
calling method.
4. finally:
o Code in the finally block always executes, regardless of whether an
exception is thrown or not. It is typically used to clean up resources.
5. Built-in Exceptions:
o Java provides many built-in exceptions (e.g., ArithmeticException,
IOException, ArrayIndexOutOfBoundsException), which can be caught
and handled.
6. Creating Custom Exceptions:
o You can create your own exceptions by extending the Exception class or
its subclasses. Custom exceptions are useful for handling specific error
conditions in your application.
Exception handling is an essential part of robust and reliable Java applications,
allowing developers to manage unexpected conditions gracefully and improve user
experience.