Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1. The difference between overloading and over-riding?

Overloading Over-riding
Method overloading is a compile-time Method overriding is a run-time
polymorphism. polymorphism.
Method overloading helps to increase the Method overriding is used to grant the
readability of the program. specific implementation of the method that is
already provided by its parent class or
superclass.
It occurs within the class. It is performed in two classes with inheritance
relationships.
Method overloading may or may not require Method overriding always needs inheritance.
inheritance.
Poor Performance due to compile time Polymorphism. It gives better performance.
polymorphism. The reason behind this is that the binding of
overridden methods is being done at runtime.

2. What is the abstract method?


-The abstract Method is used for creating blueprints for classes or interfaces. Here methods are
defined but these methods don’t provide the implementation. Abstract Methods can only be
implemented using subclasses or classes that implement the interfaces.

3. What is an interface? Use of interface


-
An interface in the context of software development is a contract that specifies the methods that a
class must implement. It defines a set of rules or guidelines for how different software
components can communicate with each other. In object-oriented programming (OOP), an
interface is a programming structure that allows unrelated classes to communicate with each other
by providing a common set of methods that they must implement. The primary use of an interface
is to enable polymorphism, which allows different objects to be treated interchangeably based on
their shared interface, rather than their specific implementation details. This promotes code
reusability, flexibility, and modularity in software design.

Use of interface
 Use to achieve total abstraction.
 It is also used to achieve loose coupling.
 Interfaces are used to implement abstraction.
 It can achieve multiple inheritance.

4. Example of the public, private, protected, and default?

Public: -
The public modifier allows public field and public Method to be accessed from any other class.

public class PublicExample {


public String publicField = "I am a public field";
public void publicMethod() {
System.out.println("I am a public method");
}
}
Private: - The private modifier restricts private Field and private Method to be accessible only
within the same class.

public class PrivateExample {


private String privateField = "I am a private field";

private void privateMethod() {


System.out.println("I am a private method");
}
}

Protected: - The protected modifier allows protected Field and protected Method to be
accessed within the same package or by subclasses of Protected Example.

public class ProtectedExample {


protected String protectedField = "I am a protected field";

protected void protectedMethod() {


System.out.println("I am a protected method");
}
}

Default: - The absence of an access modifier (default or package-private) restricts defaultField


and defaultMethod to be accessible only within the same package.
// DefaultExample.java
class DefaultExample {
String defaultField = "I am a default (package-private) field”.

void default Method () {


System.out.println("I am a default (package-private) method

Whaawbsence of an access modifier (default or


 an infinite number of interfaces.
 It is also used to achieve loose coupling.
 Interfaces are used to implement abstraction.

You might also like