TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday, 7 February 2020

implementing interface in java | interface in java with example program pdf

Interface in Java 

An interface is just like Java Class, but it only has static constants and abstract method. Java uses Interface to implement multiple inheritance. A Java class can implement multiple Java Interfaces. All methods in an interface are implicitly public and abstract.
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

Syntax for Declaring Interface

interface {
//method
}
To use an interface in your class, append the keyword "implements" after your class name followed by the interface name.

Example for Implementing Interface

class Student implements  Teacher
interface  RidableAnimal extends Animal, Vehicle

An interface is similar to a class in the following ways −

·        An interface can contain any number of methods.
·        An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
·        The byte code of an interface appears in a .class file.
·        Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

An interface is different from a class in several ways, including −

·        You cannot instantiate an interface.
·        An interface does not contain any constructors.
·        All of the methods in an interface are abstract.
·        An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
·        An interface is not extended by a class; it is implemented by a class.
·        An interface can extend multiple interfaces.

Declaring Interfaces

The interface keyword is used to declare an interface. Here is a simple example to declare an interface −

Example

Following is an example of an interface –
/* File name:NameOfInterface.java */
Import java.lang.*;
//Any number of import statements
public interface NameOfInterface {
//Any number of final, static fields
//Any number of abstract method declarations/
}
Example
/* File name:Animal.java */
interface Animal {
public void eat();
public void travel();
}

Interfaces have the following properties −

·        An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
·        Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
·        Methods in an interface are implicitly public.

Why is an Interface required?

To understand the concept of Java Interface better, let see an example. The class "Media Player" has two subclasses: CD player and DVD player. Each having its unique implementation method to play music.

implementing interface in java programming
interface in java


Another class "Combo drive" is inheriting both CD and DVD (see image below). Which play method should it inherit? This may cause serious design issues. And hence, Java does not allow multiple inheritance.
implementing interface in java | interface in java with example program pdf
implementation in java

No comments:

Post a Comment