Showing posts with label interfaces. Show all posts
Showing posts with label interfaces. Show all posts
Set Interface v/s List Interface

Set Interface v/s List Interface

Set Interface List Interface
It is collection of unique elements. It may contains duplicate elements.
It is a distinct list of elements which is unordered. List is an ordered sequvanceof elements.
No new additional method other than collection. New Methods other than collection are defined in a list.
Can use only Iteratorto traverse Set. Can use both Iterator and ListIterator to traverse list.
Set Interface does not allowNULLvalues. List Interface allows NULL values.
Sub class of Set Interface are :
- HashSet [ unordered ]
- TreeSet from SortedSet [ Ordered ]
Sub class of List Interface are :
- ArrayList
- LinkedList
- Vector
It is Based on values. It is Based on Index.
Example :
Set s=new HashSet();
Example :
List l=new ArrayList();


Example of abstract class and interface in Java : 

// Set Interface example

import java.util.*;

class SetInterface{
 
 public static void main(String... str){
  
  // unordered 
  Set s=new HashSet();
  s.add(1);
  s.add(2);
  s.add(5);
  s.add(8);
  
  System.out.println(s);
  
  // ordered
  Set s1=new TreeSet();
  s1.add(4);
  s1.add(2);
  s1.add(9);
  s1.add(1);
  
  System.out.println(s1);
 }
 
}

Output : 
[1,2,5,8]
[1,2,4,9]


// example of List interface

import java.util.*;

class ListInterface{
 public static void main(String... str){
  
  List l=new ArrayList();
  l.add("XYZ");
  l.add(10);
  l.add(1.5f);
  l.add('c');
  
  //null value accept
  
  l.add(null);
  l.add(true);
  
  System.out.println(l);
 }
}

Output : 
[XYZ,10,1.5f,c,null,true ]

Difference between abstract class and interface

Difference between abstract class and interface


Abstract Class Interface
Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
Abstract class can have constructor. Interface can not have constructor.
Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
Calendar class is example of abstract class. ActionListener is example of interface.
Example:
 public abstract class Shape{
  public abstract void draw();
 }
Example:
 public interface Drawable{
  void draw();
 }

Example of abstract class and interface in Java

 // abstract class
 
 abstract class A{
  abstract void display();
 }

 // interface
 interface C{
  
  void test();
  void disp();
  
 }

 // class B that extends A and implements C
 
 class B extends A implements C{
  public static void main(String... str){
   B b=new B();
   
   // call interface methods
   
   b.disp();
   b.test();
   
   // call abstract class method
   
   b.display();
  }
  
  public void test(){
   System.out.println("test method.");
  }
  
  public void disp(){
   System.out.println("Disp method.");
  }
  public void display(){
   System.out.println("Display method.");
  }
 }