JavaScript
QuickSort Algorithm in JavaScript
What is Quick Sort? Quick Sort algorithm follows Divide and Conquer approach. It divides elements...
An Interface in Java programming is defined as an abstract type used to specify the behavior of a class. A Java interface contains static constants and abstract methods. A class can implement multiple interfaces. In Java, interfaces are declared using the interface keyword. All methods in the interface are implicitly public and abstract.
Syntax for Declaring Interface
interface {
//methods
}
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 Dog implements Pet
interface RidableAnimal extends Animal, Vehicle
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.
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.
Now let's take another example of Dog.
Suppose you have a requirement where class "dog" inheriting class "animal" and "Pet" (see image below). But you cannot extend two classes in Java. So what would you do? The solution is Interface.
The rulebook for interface says,
Class Dog can extend to class "Animal" and implement interface as "Pet".
Step 1) Copy following code into an editor.
interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}
Step 2) Save , Compile & Run the code. Observe the Output.
| | |
|---|---|
| In class, you can instantiate variable and create an object. | In an interface, you can't instantiate variable and create an object. |
| Class can contain concrete(with implementation) methods | The interface cannot contain concrete(with implementation) methods |
The access specifiers used with classes are private, protected and public. | In Interface only one specifier is used- Public. |
Summary:
What is Quick Sort? Quick Sort algorithm follows Divide and Conquer approach. It divides elements...
This Java Development Kit(JDK) allows you to code and run Java programs. It's possible that you...
Download PDF 1) What is JMS? JMS means Java Messaging Service. It is the new standard for inter client...
Javascript Training Summary JavaScript is an open source & most popular client side scripting...
What is Java? Java is a multi-platform, object-oriented, and network-centric, programming...
What is JDK? JDK is a software development environment used for making applets and Java...