Explain the relation between a Class and an Interface?
Advertisements
प्रश्न
Explain the relation between a Class and an Interface?
स्पष्ट कीजिए
Advertisements
उत्तर
In Java, a Class is a blueprint for creating objects, whereas an Interface is a reference type that defines a set of methods and constants. An interface specifies what operations must be performed, while a class provides their actual implementation.
- Implementation: A class implements an interface using the
implementskeyword and provides implementations for the abstract methods declared in the interface. - Abstraction: An interface provides abstraction by specifying the required operations without defining how they are performed.
- Multiple Inheritance: A class can implement more than one interface, allowing it to follow multiple sets of method contracts.
- Method Declaration: An interface can declare abstract methods, whereas a class implementing the interface provides their definitions.
- Code Reusability: Interfaces promote code reusability, flexibility and better organisation by allowing different classes to follow a common structure.
- Relationship: An interface defines a contract that a class agrees to follow by implementing its required methods.
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: Objects - EXERCISE [पृष्ठ १०७]
