Declarations , Modifiers , Interfaces, OOPs , Constructors Flashcards
Class And Interface
A class can implement any number of classes.
A class can extend only one class at a time.
An interface can extend any number of interfaces.
Methods in interface
Every method present in interface is by default public. Hence at the time of overriding/implementing that method, compulsory we should declare that method as public. We will get compile time errors otherwise.
Abstract class methods
For abstract class methods, if we don’t want to provide implementation, then compulsory we should declare that method as abstract, otherwise we will get compile time error.
We cannot declare abstract method with final, private, and static modifiers.
If a class implements an interface
If a class implements an interface, then compulsory we should provide implementation for every abstract method, otherwise we have to declare class as abstract. Every interface method is public and hence at the time of implementation we should declare as public only.
Main objective of default methods in interfaces
We can extend functionality of interface by adding new methods without effecting implementation classes.
Overriding method resolution
In overriding method resolution is always based on runtime object. Hence child class method will be executed.
Interfaces and abstract classes
Every variable present inside interface is always public, static and final and we cannot declare instance variables.
But abstract class can contain instance variables.
Both interface and abstract class can contain abstract methods.
Both interface and abstract class can contain static methods.
Constructors concept applicable only for classes but not for interfaces.
When will a default constructor be generated?
If we are not writing any constructor then only default constructor will be executed.
Declaring methods with default keyword
We can declare methods with default keyword only in interface but not in class. Hence we will get compile time error.
Variable resolution
Variable resolution is always based on reference type but not based on runtime object. We used Parent reference and hence we got the parent value.