JAVA

Interface in Java

interface in java - Interface in Java

Interface:

An interface is similar to the class but we declare the functions without any definition. We define the new definition of the functions after inheriting into the class. It provides multiple inheriting into the class and develops a body of the existing function. We use interface keyword instead of class and only declare the data members. We use implements keyword to extend the feature of the interfaces into the class.

 

interface in java
Image Source:- https://qph.ec.quoracdn.net

 

Example:-
import java.util.*;
interface A
{
   public void k();
   public void m();
   public void n();
    }
Class B implements A
{
   public void k()
   {
      System.out.print ("Demo of interface ");
         }
   public void m()
   {
      Scanner sc = new Scanner (System.in);
       System.out.print ("Enter value ");
       int a = sc.nextInt();
       if (a>0)
       {
          System.out.print ("positive value");
            }
        else {
           System.out.print ("negative value");
             }
}
public void n()
{
    Scanner sc = new Scanner (System.in);
     System.out.print ("Enter Value ");
     int f = 1, i;
     int k = sc.nextInt();
     for (i = k; i>0; i--)
     {
        f = f*i;
         }
      System.out.print ("Fact is " + f);
       }
public static void main (String s[])
{
    B b = new B();
    b.k();
    b.m();
    b.n();
    }
}

 

Multiple Interface

Java programming language provides a method to extend more than one interface in a class at a time and separate with comma operator.

 

Interface in Java
Image Source:- http://2.bp.blogspot.com

 

Example:-
import java.util.*;
interface A
{
    public void k();
    public void m();
     }
interface C
{
    public void n();
   }
Class B implements A,C
{
    public void k()
     {
         System.out.print("Demo of interface");
            }
     public void m()
     {
         Scanner sc = new Scanner(System.in);
          System.out.print("Enter value");
          int a=sc.nextInt();
          if (a>0)
          {
             System.out.print("positive value");
               }
          else {
            System.out.print("negative value");
             }
         }
        public void n()
         {
            Scanner sc=new Scanner(System.in);
             System.out.print("Enter value");
             int f=1,i;
             int k=sc.nextInt();
             for (i=k; i>0; i--)
             {
                 f=f*i;
                   }
             System.out.print("Fact is" + f);
          }
           public static void main (String s[])
           {
               B b = new B();
               b.k();
               b.m();
               b.n();
                 }
}

Interface Inheriting

The interfaces are used to declare the function with prototype without definition. The functions are extended at another interface an execute after inheriting into the class and develop the body of the function. We use extend keyword to extend the feature of one interface to another interface.

 

classes
Image Source:- http://www.studytonight.com

 

Example:-
import java.util.*;
interface A
{
   public void t();
   public void d();
    }
interface B extends A
{
   public void p();
    }
Class N implements B
{
   public void t()
    {
        Scanner sc = new Scanner(System.in);
         System.out.print("Enter value");
        int x = sc.nextInt();
         if (x%2 == 0)
         {
             System.out.print("even");
              }
          else {
            System.out.print("odd");
              }
}
public void d()
{
    int x;
    for (x=1; x<10; x++)
     {
        System.out.print(x);
          }
}
public void n ()
{
   Char a;
    Scanner sc = new Scanner(System.in);
    a = sc.next().charAt(0);
     System.out.print("ASCII is" + (int)a);
     }
public static void main(String s[])
{
   N n = new N();
   n.t();
    n.d();
    n.p();
}
}

 

Abstract Class

An abstract class is a class that declares with abstract keyword and it allows to declare the functions with a definition or without definition. The function also declares with an abstract keyword that defines without any definition an extension of another class for construct the body of existing function an execute with the help of derived class object.

 

Example:-
import java.util.*;
abstract Class A
{
   abstract public void t();
   abstract public void d();
   public void p();
   {
      System.out.print(" demo of abstract");
        }
    }
Class N extends A
{
   public void t()
    {
        Scanner sc = new Scanner(System.in);
         System.out.print("Enter value");
        int x = sc.nextInt();
         if (x%2 == 0)
         {
             System.out.print("even");
              }
          else {
            System.out.print("odd");
              }
}
public void d()
{
    int x;
    for (x=1; x<10; x++)
     {
        System.out.print(x);
          }
}
public void n ()
{
   Char a;
    Scanner sc = new Scanner(System.in);
    a = sc.next().charAt(0);
     System.out.print("ASCII is" + (int)a);
     }
public static void main(String s[])
{
   N n = new N();
   n.t();
    n.d();
    n.p();
}
}

 

Math Class

Math class is a built-in class that allows managing mathematics related instructions. It provides built-in functions to handle the values.

 

math class
Image Source:- http://slideplayer.com

 

Functions of Math Class

 

abs()
It is a function that returns absolute value of given data. It is a static function that accept numeric value as argument.
ceil()
It is a function that returns nearest highest value. In this, we pass numeric value as argument.
floor()
This function allow to get nearest lowest value of given fraction number.
sqrt()
It returns square root of the given value.
pow()
It is a function that return power of the given value. In this, we pass two parameters as value and power.
sin()
This function returns sin value of the given data.
cos()
This function returns cos value of the given data.
tan()
This function returns tan value of the given data.
min()
It is a function that returns minimum value from given data.
max()
It is a function that returns maximum value from given data.
log()
This function returns log value of given data.

 

Date Class

Data class is a class that available in java.util package. It allows us to manage the date and time-related information.

 

Functions of Date Class

 

getDay()
It is a function that return numeric value as current system date.
getMonth()
This function allows to get current system month.
getYear()
This function allows to get current system year.
getHours()
It is a function that return hours from the current system date and time.
getMinutes()
It is a function that return minutes from the current system date and time.
getSeconds()
It is a function that return seconds from the current system date and time.
setDate()
It is a function that allow to define the date on the base of given argument. In this, we pass string as argument.
setMonth()
It is used to define the month of selected date.
setYear()
It is used to set the year in selected date.
setHours()
This function allow to set the hours in selected date.
setMinute()
This function allow to set the minutes in selected date.
setSecond()
This function allow to set the seconds in selected date.

 

Program to display the date
import java.util.*;
Class DT
{
   public static void main(String s[])
    {
       Thread t = new Thread();
        Date d = new Date();
        int a = d.getHours();
        int b = d.getMinutes();
         int c = d.getSeconds();
         while(true)
          {
              try {
                       if(a<=24)
                        {
                            if(b==60)
                             {
                                a = a+1;
                                b=0;
                                 }
                             if(c==60)
                             {
                                b = b+1;
                                 c=0;
                                  }
                             if(a==24)
                             {
                                 a=0;
                                 b=0;
                                  c=0;
                                   }
                         }
                 t.sleep(1000);
                  c++;
                 System.out.println(a + ":" + b + ":" + c);
               }
             catch(InterruptedException e)
             {
                 }
}
}

Vector Class

Vector class is a class that allows storing the information similar to an array but it provides dynamic memory. It means we can store multiple data at different index position for accessing information. It provides built-in functions to process the data and instruction.

 

Functions of Vector Class

 

  1. add:- It is a function that allows adding the component. In this, we pass element as an argument.
  2. Element At:- It is a function that at the element and print the value from the vector on the base of given position. In this, we pass the numeric value as an argument.
  3. Remove Element At:- This function removes the element from the vector. In this, we pass a numeric value as an argument.
  4. Remove All:- It is the function that removes all elements from the vector.
  5. Index off:- It returns the index position of given element.
  6. Equals:- It compares the data and returns logical value.
  7. Insert Element At:- It is the function that allows inserting the data into the vector at a particular position.

 

 

About the author

Udit Mutreja

Add Comment

Click here to post a comment