Skip to main content

interface and interface collections in java

Interfaces:

 Interface in Java refers to the abstract data types. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.

                                             

As you have already learnt about Abstract methods and abstract classes and how they are implemented. The main difference between an abstract class and an interface is that in an abstract class there exists at least one abstract method. On the other hand, an interface consists of all the methods as abstract. Here, by abstract we mean that the methods are unimplemented.

We all know that multiple inheritance is not supported in java. This is because of the conflicts that might arise when both the parent classes are having the same variable declarations. This makes it hard for the child class to infer which class the particular variable or the method belongs.

Even though it is said that Java directly doesn’t support multiple inheritance, it might support the functionality in an indirect manner. This is accomplished using the concept of interfaces. 

Example for abstract class:                                     

Abstract class A{

             public void run()

               {

                   System.out.println("run method");

                }

Public void stop();

Public void start();

            }

 

 Example for Interface:

interface A{

                     public void run();    

/*by default interface implements public methods and remember this  a method so we are using semicoln after method.*/                  

public void show();  

                   }

Every interface must be implemented by a sub-class. This is done using the “implements” keyword in java.

class B implements A

                  {

                    public void show()

                  { 

                    System.out.println("show method");

                   }

                    public void run()

                  {

                   System.out.println("show method");

                   }

                   }

If you would like to call the above methods, just create an object for the child class (Class B) and then call the methods using the object name.

B classBObject=new B();

classBObject.show();  

 

Since we now know what an interface is, let us understand the concept of interfaces using Java Collections.

Java Collections: Interface

Iterator interface: Iterator is an interface that iterates over the elements. It is used to traverse a list and modify its elements. Iterator interface has three methods which are mentioned below: 

public boolean hasNext() – This method returns true if iterator has more elements.

public object next() – It returns the element and moves the cursor pointer to the next element.

public void remove() – This method removes the last elements returned by the iterator. 

These methods are used in the subclass which implements the Iterator interface.

There are three components in Java that extend the collection interface. They are:

·       List

·       Queue

·       Set

 

In the next post, let us explore about the three interfaces in detail.

If you have any queries please mention them in the comments below!

Thanks for reading!





Comments

  1. really very use resource thanks man keep posting....

    ReplyDelete

Post a Comment

Thanks for your comments!

Popular posts from this blog

ANGULAR-2

Angular JS is an open source framework built on JavaScript. It was built by the
developers at Google. This framework was used to overcome obstacles encountered while
working with Single Page applications. Also, testing was considered as a key aspect while
building the framework. It was ensured that the framework could be easily tested. The
initial release of the framework was in October 2010.

Features of Angular 2:Components: The earlier version of Angular had a focus of Controllers but now has changed the focus to having components over controllers. Components help to
build the applications into many modules. This helps in better maintaining the
application over a period of time.

TypeScript: The newer version of Angular is based on TypeScript. This is a
superset of JavaScript and is maintained by Microsoft.

Services: Services are a set of code that can be shared by different components
of an application. So for example, if you had a data component that picked data
from a database, you …

Efficiency of an algorithm

Efficiency of an algorithm
Programmers find a tough time writing efficient code. But what exactly do we mean by efficiency? What difference does it make if we write normal code instead of an efficient code? Let’s find out with a simple example of traditional sorting problem.


The first, known as insertion sort, takes time roughly equal to c1*n2 to sort n items, where c1 is a constant that does not depend on n. That is, it takes time roughly proportional to n2. The second, merge sort, takes time roughly equal to c2*n lg n, where lg n stands for log2 n and c2 is another constant that also does not depend on n. Insertion sort usually has a smaller constant factor than merge sort, so that c1 < c2. We shall see that the constant factors can be far less significant in the running time than the dependence on the input size n.


    Where merge sort has a factor of lg n in its running time, insertion sort has a factor of n, which is much larger. Although insertion sort is usually faster than mer…

What is context in android ?

The topic of Context in Android seems to be confusing too many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it.
What exactly is Context?


Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment". The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…allows access to application-specific resources and classes, as well as up-calls for application-level operations such as…