Create Thread using Anonymous class and Interface

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , with 4 Comments on Create Thread using Anonymous class and Interface

Demonstration of creating Thread by Anonymous class and Interface

Create thread using Anonymous class:

Anonymous Class

Explanation:-  A Class which does not have any name is known as anonymous class.

Q. Write a program to create thread using anonymous class/local class.

class testAno
{
  public static void main(String args[])
  {
     Thread t =new Thread(new runnable(){
     public void run()
     {
          System.out.println("I am in Thread");
     }
    });

    t.start();
}
}

O/P:-    I am in Thread

Create Thread Using Interface   OR Runnable Interface:

class Method
{
  public static void main(String args[])
  {
     TestThread runnable_obj=new TestThread();
     Thread t=new Thread(runnable_obj);
     t.start();
  }
}

class TestThread implements Runnable
{
  public  void run()
  {
     System.out.println("In Thread Main");
  }
}

Interface, Inheritance and abstract class

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , with 13 Comments on Interface, Inheritance and abstract class

What is abstract class, interface and inheritance in java. Source code and example

Interface,Inheritance & Abstract Class:-

Inheritance, Interface and abstract class

Inheritance :

The concept of extending a class by another class is known as inheritance.

A class which is inherited is known as Parent class, Base class or Super class.

A class which extends base class is known as Derived class or child class.


Abstract Class:

If a class contains one or more than one abstract method (abstract method is defined by keyword abstract and contains only declaration of that method.) then the class is declared with keyword abstract and known as abstract class.


Interface :

An Interface in java is known as Contract which must be followed by its implementing class (Child class).

If a class contains all methods as abstract method then, the class can be declared as Interface.

Important notes on interface:

  1. only public and abstract modifiers are allowed to use in interface. (Static are not allowed)
  2. If variable is defined inside the interface, then it must be initialized and it is final by default.

To read more on interface, refer this article.


Example 1:

interface IMario
{
  Void behavior();
}

class  Mario implements IMario
{
 public void behavior()
 {
     System.out.println("This is simple Mario");
 }
}

class  SuperMario implements IMario
{
  public void behavior()
  {
     System.out.println("This is Super Mario with power");
  }
}

class  TestMario
{
   public static void main(String args[])
   {
        System.out.println("----game started----");
        Imario obj=new Mario();
        Obj.behavior();
        System.out.println(-----After Mashroom-----");
        obj=new SuperMario();
        Obj.behavior();
        System.out.println(-----After Power Loss-----");
        obj=new Mario();
        Obj.behavior();
   }
}

o/p:-

—–Game Started——

This is simple Mario.

—–After Mashroom——

This is Super Mario with power.

—–After Power Loss—–

This is simple Mario.

Example 2:


interface A
{
   void displayA()
}

abstract class B
{
   public void displayB()
   {
     System.out.println("Display-B");
   }
   abstract public void display();
}

class  c extends B implements A
{
   Public void displayB()
  {
      System.out.println("Abstract Display-B");
  }

  public void displayA()
 {
     System.out.println("Display-A");
  }
}

class  TestClass
{
  public static void main(String args[])
 {
   C obj=new C();
   Obj.display();
   Obj.displayA();
   Obj.displayB();
 }
}

Interface in JAVA

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Interface in JAVA

Explains the usage and benefits of interface in JAVA.

Interface & Its Advantage:-

1. If all the methods in the class is abstract then the class is nothing but the interface.

2. We cannot create an object of interface.

3. To implement the interface by child class keyword “implements” is used.

4. Interface is defined by using keyword interface.

5. If a class implements interface but does not implements methods, then the class is abstract class.

6. We can assign the object of class which implements the interface of type of                                                     that interface.

interface Duck
{
    Void DuckType();
    Void DuckProperty();
}

class BlackDuck implements Duck
{
 Public void DuckType()
 {
     System.out.println("Black Duck");
 }

 Public void DuckProperty()
 {
   System.out.println("Swim, Quack  and Fly");
 }
 }

class TestDuck
{
 Public static void main(String args[])
 {
     BlackDuck obj=new BlackDuck();
     Obj.DuckType();
     Obj.DuckProperty();
 }
}

Advantages of Interface:-

  • Runtime Polymorphism
  • Contract can be used throughout the application

To know more about inheritance and abstract classes, refer this article.