A class
is a top level block that is used for grouping variables and methods for
developing logic. Types of classes –
- · Interface
- · Abstract class
- · Concrete class
- · Final class
- · Enum
1. Interface – It is a
fully unimplemented class used for declaring a set of operations of an object.
It contains only public static final variables and public abstract methods. It
is created by using the keyword interface. It is always created as a root class
in object creation process. We must design an object for interface if its
operations have different implements. So, interface tells what should be
implemented but not how.
Example – Vehicle,
animal objects creation must be started with interface. These two object
operations have different implementations based on different types of vehicles
like Bus, Car, Bike etc.
Interface Vehicle
{
public
void engine();
public
void brakes();
}
In
Interface we cannot define a method, we can only declare methods in it. Those
class which implements using keyword implements
would define the methods declared inside interface. In interface by default
the methods are public abstract.
Example –
interface
Abc{
public void
show();
}
class
Xyz implements Abc{
@Override
public void
show() {
System.out.println("Hello show");
}
public void
display()
{
System.out.println("Hello display");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new
Xyz();
a.show();
a.display();
}
}
Output –
Hello show
Hello display
In the
above example we have seen that class Xyz implements Abc and defines the method
called show() and also it has its own method display(). Now suppose if we want
to restrict that only to have access to the methods of Abc interface not the
other methods inside Xyz.
Note – Interface can only
declare methods, but it cannot define methods. So, that’s why we do not have
constructors for it and also we cannot create object of Interface. But we can
create reference of an interface and through that we can restrict the class not
to use other methods.
Example –
interface
Abc{
public void
show();
}
class
Xyz implements Abc{
@Override
public void
show() {
System.out.println("Hello show");
}
public void
display()
{
System.out.println("Hello display");
}
}
class
Tesr1 {
public static void
main(String[] args) {
// a is a reference of Abc and taking memory from Xyz
Abc a = new
Xyz();
a.show();
// a.display(); // It gives an undefined error
}
}
Output –
Hello show
Example –
interface
Abc{
public void
show();
}
class
Tesr1 {
public static void
main(String[] args) {
Abc a
= new Abc()
{
@Override
public void
show() {
System.out.println("Hello show");
}
};
}
}
Output –
Hello show
Note – In
the above example we are creating anonymous class and overriding the method
show() in it. We have created object earlier before creating any class and then
used an unnamed class known as anonymous class to define the interface methods.
Don’t think that we have created an object of interface rather its created an
object from anonymous class.
There are three kinds of interface in java –
- · Marker interface
- · SAM (Single Abstract method) or Functional interface
- · Normal interface
SAM – From java 8 onwards
there is a concept of functional interface which states that if there is only
one abstract method to be define then why to create extra class for it. So in
java 8 they provided the concept of lambda expression which already exists in
Python. So using lambda expression we can define that expression in just one
line of code.
Example –
interface
Abc{
public void
show();
}
class
Tesr1 {
public static void
main(String[] args) {
Abc a = () ->
System.out.println("Hello
show");
a.show();
}
}
Output –
Hello show
MARKER INTERFACE – An interface having no method declaration is known as marker
interface. It is mostly used for security purpose. Suppose for security reason
if we want to assign some permission to a particular class to access its methods
then we can give the permission using marker interface.
Example –
interface
Abc{}
class
Xyz implements Abc{
public void
show()
{
System.out.println("I am in show");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new
Xyz();
if(a instanceof
Abc)
{
System.out.println("Permision granted");
a.show();
}
else
{
System.out.println("Permission denied");
}
}
}
Output –
Permision granted
I am in show
Example –
interface
Abc{}
class Xyz{
public void
show()
{
System.out.println("I am in show");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new Xyz();
if(a instanceof
Abc)
{
System.out.println("Permision granted");
a.show();
}
else
{
System.out.println("Permission denied");
}
}
}
Output –
Permission denied
Interface in java 8 and new features
From java 8 onwards we can define
methods inside interface using default keyword. As we know that java does not
support multiple inheritance with class but it supports with interface. As
before java 8 we were only supposed to declare methods inside interface, but
from java 8 onwards we can even define methods inside interface. Suppose there
is a class and it implements two interface having some method common, then at
that time it is our responsibility to override that method in the class. Let us
take an example –
Example –
interface
Abc{
void
run();
default void
walk()
{
System.out.println("I am walking inside Abc");
}
}
interface
Def{
void
jump();
default void
walk()
{
System.out.println("I am walking inside Def");
}
}
class
Xyz implements Abc{
@Override
public void
run() {
System.out.println("I am running inside Xyz");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new Xyz();
a.run();
a.walk();
}
}
Output –
I am running inside Xyz
I am walking inside Abc
Example –
interface
Abc{
void
run();
default void
walk()
{
System.out.println("I am walking inside Abc");
}
}
interface
Def{
void
jump();
default void
walk()
{
System.out.println("I am walking inside Def");
}
}
class
Xyz implements Abc,Def{
@Override
public void
run() {
System.out.println("I am running inside Xyz");
}
@Override
public void
jump() {
System.out.println("I am jumping inside Xyz");
}
@Override
public void
walk() {
System.out.println("I am walking inside Xyz");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new
Xyz();
a.run();
a.walk();
a.jump();
}
}
Output –
I am running inside Xyz
I am walking inside Xyz
I am jumping inside Xyz
Note – If
we have same method in some interface and super class defined then always the
super class method will be executed, as it has more priority as compared to
interface method.
Example –
interface
Abc{
default void
walk()
{
System.out.println("I am walking inside Abc");
}
}
interface
Def{
default void
walk()
{
System.out.println("I am walking inside Def");
}
}
class
Ghi{
public void
walk()
{
System.out.println("I am walking inside Ghi");
}
}
class
Xyz extends Ghi implements Abc{
}
class
Tesr1 {
public static void
main(String[] args) {
Xyz a = new
Xyz();
a.walk();
}
}
Output –
I am walking inside Ghi
Note – Always we extends first and then implements, if we do
vice-versa then it gives an error.
Note – If we define any methods inside an interface that overrides
the method of object class then it gives us an error.
Before java 8 we were not allowed to use static methods inside
interface. But from java 8 onwards it gives an allowance to use static methods.
Example –
interface
Abc{
static void
walk()
{
System.out.println("I am walking inside Abc");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Abc.walk();
}
}
Output –
I am walking inside Abc
No comments:
Post a Comment