top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java Interfaces?

+1 vote
313 views

Java Interface                                                                                                                                                                    

java does not support multiple inheritance. However, there are several cases when it becomes mandatory for an object to inherit properties from multiple classes to avoid redundancy and complexity in code. For this purpose, java provides a workaround in the form of interfaces.

Also, java provides the concept of nested classes to make certain types of programs easy to manage, more secure, and less complex.                                                                                        

Interfaces

An interface in java is a contract that specifies the standards to be followed by the types that implement it. The classes and a class are similar in the following ways:

An interface and a class are similar in the following ways:

  • An interface can contain multiple methods.
  • An interface is saved with a .java extension and the name  of the interface just as a java class.
  • Interfaces are stored in packages and the bytecode file is stored in a directory structure that matches the package name.

However, an interface and a class differ in several ways as follows:

  • An interface cannot be instantiated.
  • An interface cannot have constructors.
  • All methods of an interface are implicitly abstract.
  • The fields declared in an interface must be both static and final. Interface cannot have instance fields.
  • An interface is not extended but implemented by a class.
  • An interface can extend multiple interfaces.

                                                                                                   

Purpose of Interfaces

Objects in java interact with the outside world with the help of methods exposed by them. Thus, it can be said that, methods serve as the object’s interface with the outside world.

This is similar to the buttons in front of a television set. These buttons acts as an interface between the user and the electrical circuit and wiring on the other side of the plastic casing. When the “power” button is pressed, the television set is turned on and off.

In java, an interface is a collection of related methods without any body. These methods form the contract that the implementing class must agree with. When a class implements an interface, it becomes more formal about the behavior it promises to provide This contract is enforced at build time by the compiler. If a class implements an interface, all the methods declared by that interface must appear in the implementing class for the class to compile successfully.

Thus, in java, an interface is a reference type that is similar to a class. However, it can contain only method signatures, constants, and nested types. There is no method definition but only declaration. Also, unlike a class, interfaces cannot be instantiated and must be implemented by classes or extended by other interfaces in order to use them.

There are a several situations in software engineering when it becomes necessary for different groups of developers to agree to a ‘contract’ that specifies how their software interacts. However, each group should have the liberty to write their code in their desired manner without having the knowledge of how the other groups are writing their code. Java interfaces can be used for defining such contracts.

Interfaces do not belong to any class hierarchy, even though they work in conjunction with classes. Java does not permit multiple inheritance for which interfaces provide an alternative. In Java, a class can inherit from only one class but it can implement multiple interfaces. Therefore, objects of a class can have multiple types such as the type of their own class as well as the types of the interfaces that the class implements. The syntax for declaring an interface is as follows:

Syntax:

<visibility> interface <interface-name> extends <other-interfaces, …>

{

// declare constants

// declare abstract methods

}

Where,

<visibility> : Indicates the access rights to the interface. Visibility of an interface is always public.

<interface-name>: Indicates the name of the interface.

<other-interfaces>: List of interfaces that the current interface inherits from.

For example,

public interface Sample extends Interface1

{

     static final int someInteger;

     public void someMethod();

}

                                                                                         

In java, interface names are written in CamelCase, that is, first letter of each word is capitalized, Also, the name of the interface describes an operation that a class can perform. For example,

Interface Enumerable

Interface Comparable

Some programmers prefix the letter ‘l’ with the interface name to distinguish interfaces from classes. For example,

Interface IEnumerable

Interface IComparable

Notice that the method declaration does not have any braces and is terminated with a semicolon. Also, the body of the interface contains only abstract methods and no concrete method. However, since all methods in an interface are implicitly abstract the abstract keyword is not explicitly specified with the method signature.

When a class implements an interface it must implement all its methods. If the class does not implement all its methods, it must be marked abstract. Also, if the class implementing the interface is abstract, one of its subclasses must implement the unimplemented methods. Again, if any of the abstract class subclasses does not implement all the interface methods, the subclass must be marked abstract as well.

The data members of an interface are implicitly static, final, and public.

posted Aug 28, 2017 by Ayush Srivastav

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

The user interface is a vital part of any computer system. It determines how easily an end user can interact with the program. One of the achievements of a system depends on how well a user interface is designed and whether it creates a seamless experience for end users.

Definition of UI and UI Design

The UI of an application, also know as an ‘interface’, is the means by with a user and a computer system interact. It can comprise both software and hardware components.

In particular, UI includes:

  • The textual, graphical, and auditory information that the program presents to the user.
  • The control sequences that a user employs to control the program. For example, mouse movements, keystrokes with the computer keyboard, and selections through the touchscreen.

A simple example of the UI in the real world is an Automatic Teller Machine (ATM). It consists of a keypad, a display window, a selection of choice options, and a help screen that displays instructions for completing an ATM transaction.

User Interface Design

User Interface Design is the process of designing user interface for websites, appliances, computers, and software applications. It focuses on anticipating an end user’s requirement, that is, what the users might need to do and then, ensuring that the UI has all elements to facilitate those actions.

Parts of User Interface Design

The fundamental parts or elements of most UIs are as follows:

  • Input controls
  • Navigational Components
  • Informational Components
  • Containers

Principles and Attributes of User Interface Design

UI design principles focus on improving the quality of user interface design. Some of these include:

The structure principle

The principle is concerned with overall UI architecture. The design of the interface should be visually, theoretically, and linguistically clear. It should provide clear and user-specific paths and relevant information.

           Image Courtesy: https://www.amazon.com

Tips/Techniques to support the structure principle

The techniques that help to support this principle are as follows:

  • You should group the logically connected items to communicate an separate unrelated items to achieve visual organization.
  • Ensure good cross-linkages and quick jumps to important sections of the websites.
  • Design page-specific navigation and access to information.
  • Keep the screen less cluttered and easier to understand.
  • Present the flow of actions, information, responses, and visual preparations in a sensible order that is easy to remember and place in context.
READ MORE

What is Interface?
An interface is a TypeScript artifact, it is not part of ECMAScript. ... Along with functions, an interface can also be used with a Class as well to define custom types. An interface is an abstract type, it does not contain any code as a class does. It only defines the 'signature' or shape of an API.

Interfaces in TypeScript are similar to interfaces in many object-oriented programming languages. However, in TypeScript, they play the role of defining the "shape" of an object. Objects don't have to explicitly implement interfaces as you would in C# or Java. Instead, interfaces define the expected properties so that the type checker can verify an object with the expected properties is being used.

Interfaces have zero runtime JS impact. There is a lot of power in TypeScript interfaces to declare the structure of variables.

TypeScript classes can implement an interface just like a class in Java or C# might implement an interface. 
 

declare var myPoint: { x: number; y: number; };

interface Point {
    x: number; y: number;
}
declare var myPoint: Point;

Video for Interface

https://www.youtube.com/watch?v=lusXAJ3tC1k​

READ MORE
...