Join Regular Classroom : Visit ClassroomTech

Programming in JAVA – codewindow.in

Related Topics

JAVA Programming

Explain the use of abstract classes and interfaces in Java and the difference between them?

Abstract classes and interfaces are both used in Java to define common behavior that can be shared by multiple classes. However, there are some differences between them.

Abstract classes:

An abstract class is a class that cannot be instantiated, meaning you cannot create objects of that class. It is used to define common behavior and properties that can be shared by multiple subclasses. Abstract classes can have both abstract and non-abstract methods.

An abstract method is a method that does not have an implementation. It is declared in the abstract class with the abstract keyword and ends with a semicolon instead of a method body. Subclasses of the abstract class must provide an implementation of the abstract methods.

Here is an example of an abstract class:

public abstract class Animal {
    public abstract void makeSound();
    
    public void sleep() {
        System.out.println("The animal is sleeping.");
    }
}

public class Dog extends Animal {
    public void makeSound() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        
        myDog.makeSound(); // Output: The dog barks.
        myDog.sleep(); // Output: The animal is sleeping.
    }
}

In this example, we have an abstract class Animal with an abstract method makeSound() and a non-abstract method sleep(). We also have a subclass Dog that extends the Animal class and provides its own implementation of the makeSound() method.

In the Main class, we create an object of the Dog class called myDog, but we declare it as an Animal type. We can then call both the makeSound() and sleep() methods on the myDog object.

Interfaces:

An interface is a collection of abstract methods and constants. It is used to define common behavior that can be shared by multiple unrelated classes. An interface does not contain any method implementations, only method signatures.

A class can implement one or more interfaces, which means that it must provide an implementation of all the methods defined in the interface.

Here is an example of an interface:

public interface Animal {
    public void makeSound();
}

public class Dog implements Animal {
    public void makeSound() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        
        myDog.makeSound(); // Output: The dog barks.
    }
}

In this example, we have an interface Animal with an abstract method makeSound(). We also have a class Dog that implements the Animal interface and provides its own implementation of the makeSound() method.

In the Main class, we create an object of the Dog class called myDog, but we declare it as an Animal type (since Dog implements the Animal interface). We can then call the makeSound() method on the myDog object.

Difference between abstract classes and interfaces:

  1. Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.

  2. A class can only extend one abstract class, but it can implement multiple interfaces.

  3. An abstract class can have instance variables, while an interface can only have constants.

  4. An abstract class can provide default implementations for some of its methods, while an interface cannot provide any method implementations.

What is the use of the final keyword in Java and when is it applied?

The final keyword in Java is used to indicate that a variable, method, or class cannot be changed after it has been initialized or defined.

Here are some of the uses of the final keyword in Java:

  1. Final variables: When a variable is declared as final, its value cannot be changed once it has been initialized. It can be initialized during the declaration or in a constructor. Once the value is set, it cannot be modified.

final int MAX_VALUE = 100;
  1. Final methods: When a method is declared as final, it cannot be overridden by subclasses. This means that the implementation of the method in the superclass cannot be changed by subclasses.

public class Parent {
    public final void sayHello() {
        System.out.println("Hello from parent.");
    }
}

public class Child extends Parent {
    // This will result in a compiler error since sayHello() is final
    public void sayHello() {
        System.out.println("Hello from child.");
    }
}
  1. Final classes: When a class is declared as final, it cannot be extended by any subclass. This means that the behavior and properties of the final class cannot be changed by any subclass.

public final class FinalClass {
    // ...
}

// This will result in a compiler error since FinalClass is final
public class ChildClass extends FinalClass {
    // ...
}
  1. Final arguments: When a method argument is declared as final, it cannot be modified within the method.

public void printMessage(final String message) {
    // This will result in a compiler error since message is final
    message = "New message";
    
    System.out.println(message);
}

The final keyword is commonly used to indicate constants, such as public static final double PI = 3.14;, or to prevent unintended changes to variables, methods, or classes. It can also be used for optimization purposes, since the compiler can make some optimizations when it knows that certain variables or methods will not change.

Questions on Chapter 7

Questions on Chapter 7

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories