Interface
What is an interface ?
Interface is similar to class which is collection of public static final variables
(constants) and abstract methods.
Why use interface in java ?
In java interface are used for achieving multiple inheritance.
Can an Interface extend another Interface?
Yes an Interface can inherit another Interface.
How interface is similar to class ?
Whenever we compile any Interface program it generate .class file. That means the
bytecode of an interface appears in a .class file.
How interface is different from class ?
· we cannot instantiate an interface.
· An interface does not contain any constructors.
· All methods in an interface are abstract.
· An interface cannot contain instance fields. Interface only contains public static final
variables.
· An interface is can not extended by a class; it is implemented by a class.
· An interface can extend multiple interfaces. That means interface support multiple
Inheritance.
Why interface have no constructor ?
Because, constructor are used for eliminate the default values by user defined values,
but in case of interface all the data members are public static final that means all are
constant so no need to eliminate these values.
Other reason because constructor is like a method and it is concrete method and
interface does not have concrete method it have only abstract methods that's why
interface have no constructor.
What is Marker or tagged interface ?
An interface that have no member is known as marker or tagged interface. For
example: Serializable, Cloneable, Remote etc. They are used to provide some essential
information to the JVM so that JVM may perform some useful operation.
Why Method Overloading is not possible by changing the return type of method?
In java, method overloading is not possible by changing the return type of the method
because there may occur ambiguity.
Why Interface have no Constructor ?
Because, constructor are used for eliminate the default values by user defined values,
but in case of interface all the data members are public static final that means all are
constant so no need to eliminate these values.
Other reason because constructor is like a method and it is concrete method and
interface does not have concrete method it have only abstract methods that's why
interface have no constructor.
Why not use abstract and final modifier together ?
In java, abstract and final both modifiers are not allowed for a class at a time the
reason is abstract and final are opposite keywords. If a class is an abstract, then that
class must be extended (inherited). If a class is final then that class can not be
extended. So both keyword can not be use at a time
Why use Abstract class ?
Abstract class are used for fulfill common requirement. If we use concrete classes for
fulfill common requirements than such application will get the following limitations.
· Application will take more amount of memory space (main memory).
· Application execution time is more.
· Application performance is decreased.
To overcome above limitation you can use abstract class.
Difference between Abstraction and Encapsulation ?
Encapsulation is not provides fully security because we can access private member of
the class using reflation API, but in case of Abstraction we can't access static, abstract
data member of class.
Give Real life example of Abstraction
Abstraction shows only important things to the user and hides the internal details for
example when we ride a bike, we only know about how to ride bike but can not know
about how it work ? and also we do not know internal functionality of bike.
Difference between Encapsulation and Abstraction
Encapsulation is not provides fully security because we can access private member of
the class using reflation API, but in case of Abstraction we can't access static, abstract
data member of class.
How to achieve Encapsulation in java
In java you can achieve Encapsulation by using class concept.
Difference between Abstract and Interface ?