Java Basics 2

What is this keyword and its use?

The "this" keyword in Java refers to the current object that the code is being executed on. It can be used to access the members (fields and methods) of the current object and to call other constructors of the same class.

There are a few common uses of the "this" keyword:

  1. Accessing instance variables: When a local variable has the same name as an instance variable, the "this" keyword can be used to access the instance variable. For example:
class MyClass {
    int myVar;
    public void myMethod(int myVar) {
        this.myVar = myVar;
    }
}

2. Calling other constructors: The "this" keyword can be used to call another constructor of the same class. This is known as constructor chaining. For example:

class MyClass {
    int myVar;
    public MyClass() {
        this(0);
    }
    public MyClass(int myVar) {
        this.myVar = myVar;
    }
}


3. To distinguish between the local variable and instance variable: When the local variable and instance variable have the same name, the "this" keyword is used to refer instance variable.

4. To pass the current object as an argument: The "this" keyword can also be used to pass the current object as an argument to a method or constructor. For example:

class MyClass {
    public void myMethod() {
        otherMethod(this);
    }
    public void otherMethod(MyClass obj) {
        // do something with obj
    }
}

It is also worth noting that the "this" keyword is implicitly used when calling a method or accessing a field of the current object without specifying an object reference.

Can we implement any method in interface? If yes, how?

In Java 8 and later versions, interfaces can include default and static methods in addition to the abstract methods they have always been able to include.

A default method is a method that has an implementation in the interface and it can be overridden by the class that implements the interface. It is identified by the keyword "default" in its declaration.

A static method is a method that is associated with the interface rather than with any instance of the interface. It can be called directly on the interface without the need to create an instance of the interface. It is identified by the keyword "static" in its declaration.

For example:

interface MyInterface {
    void myAbstractMethod();
    default void myDefaultMethod() {
        // implementation
    }
    static void myStaticMethod() {
        // implementation
    }
}

It is important to note that default and static methods are not part of the contract that a class implementing an interface needs to fulfill, and they can be overridden by an implementing class if needed, static methods cannot be overridden.

By using default and static methods in interfaces, developers can now add new functionality to existing interfaces without breaking existing code that implements those interfaces. This allows for more flexibility in code evolution and maintenance.

Difference between Abstraction and interface? Minimum 5.

  1. Implementation: An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract class can provide a partial implementation of a class, while an interface cannot provide any implementation.
  2. Inheritance: An abstract class can inherit from another class using the "extends" keyword and implement multiple interfaces using the "implements" keyword. An interface can inherit from multiple interfaces using the "extends" keyword.
  3. Access Modifiers: An abstract class can have different access modifiers (public, protected, private) for its members (fields and methods), while all members of an interface are implicitly public.
  4. Constructors: An abstract class can have constructors, while an interface cannot have any constructors.
  5. Variables: An abstract class can have instance variables, while an interface can only have final and static variables.

In summary, an interface defines a contract for a class to implement, providing a blueprint for methods that a class must implement, but doesn't provide any implementation. An abstract class, on the other hand, can provide both the blueprint for the methods and some implementation, but the implementation can be overriden. Interfaces are mainly used to achieve multiple inheritance and abstract class is used for abstraction.