Saturday, 30 September 2017

Abstract Class & Interface in Java

     Abstract Class & Interface in Java 

       Here is the most important topic on Java Abstract class and interface, Abstraction is the very important aspects in Object oriented Programming and we can achieve abstraction with the help of Abstract class and interface, this topic is equally important for interview point of view. So lets take a look ,if you have any questions please comment below .





If a class contain any abstract method then the class is declared as abstract class. It can have abstract and non-abstract methods (method with body).

Abstraction in Java:

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it.
Abstraction in Java:
1. Abstract class (0 to 100%)
2. Interface (100%)

Abstract class:

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
Syntax:
abstract class class_name {
}
Abstract method:

Method that are declared without any body within an abstract class is known as abstract method. Any class that extends an abstract class must implement all the abstract methods declared by the super class.
Syntax:
abstract return_type function_name ( );    // No definition
NOTE: Instead of curly braces({ }) an abstract method will have a semicolon( ; ) at the end.
Example  of Abstract class
abstract class A
{
abstract void callme();
}
class B extends A
{
void callme()
{
  System.out.println("this is callme.");
}
public static void main(String[] args)
{
  B b=new B();
  b.callme();
}
}
Output : this is callme.

Points to Remember:

1. Abstract classes are not Interfaces. They are different,
2. An abstract class must have an abstract method.
3. Abstract classes can have Constructors, Member variables and Normal methods.
4. Abstract classes are never instantiated.
5. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.
When to use Abstract Methods & Abstract Class??

Ans) Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.

An interface in java is a blueprint of a class. It has static constants and abstract methods only.
Java Interface also represents IS-A relationship.
Syntax
interface interface_name {
}
Example:
/*Actual Code*/
interface Moveable
{
int AVERAGE-SPEED=0;   //what you declare
void move();
}
/*Compiler Code*/
interface Moveable
{
public static final int AVERAGE-SPEED=40;   //what compiler sees
public abstract void move();
}
NOTE : Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
Interface differs from a class in following ways:

1. You cannot instantiate an interface.
2. An interface does not contain any constructors.
3. All of the methods in an interface are abstract.
4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
5. An interface is not extended by a class; it is implemented by a class.
6. An interface can extend multiple interfaces.

NOTE : A class extends another class, an interface extends another interface but a class implements an interface.

Why we use Java interface??

1. It is used to achieve complete abstraction.
2. By interface, we can support the functionality of multiple inheritance.
3. It can be used to achieve loose coupling.

Rules for using Interface:

1. Methods inside Interface must not be static, final, native or strictfp.
2. All variables declared inside interface are implicitly public static final variables(constants).
3. All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
4. Interface can extend one or more other interface.
5. Interface cannot implement a class.
6. Interface can be nested inside another interface.

Example :Interface implementation :
interface Moveable
{
int AVG-SPEED = 60;
void move();
}
class Vehicle implements Moveable
{
public void move()
{
  System.out.println("Average speed is"+AVG-SPEED");
}
public static void main (String[] arg)
{
  Vehicle vc = new Vehicle();
  vc.move();
}
}
Output :

Average speed is 60.
Multiple inheritance in Java by interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
Example:

interface Printable{ 
   void print(); 

 
interface Showable{ 
   void show(); 

 
class A implements Printable,Showable{ 
 
public void print(){
   System.out.println("Hello");
public void show(){
   System.out.println("Java");

 
public static void main(String args[]){ 
A a = new A(); 
a.print(); 
a.show(); 

Output:
Hello
Java
Interface inheritance:
A class implements interface but one interface extends another interface.

Example:

interface Printable{ 
   void print(); 
interface Showable extends Printable{ 
   void show(); 
class Testinterface2 implements Showable{ 
 
  public void print(){
    System.out.println("Hello");
  } 
  public void show(){
   System.out.println("Java");
  } 
 
  public static void main(String args[]){ 
    Testinterface2 obj = new Testinterface2(); 
    obj.print(); 
    obj.show(); 
  } 

Output:
Hello
Java
Nested Interface in Java:
Note: An interface can have another interface i.e. known as nested interface.
Example:
intserface printable{ 
void print(); 
interface MessagePrintable{ 
   void msg(); 

}
Difference between abstract class and interface?
1.Abstract class can have abstract and non-abstract methods.
2.Interface can have only abstract methods.
3.Abstract class doesn't support multiple inheritance.
4.Interface supports multiple inheritance.
5.Abstract class can have final, non-final, static and non-static variables. 6.Interface has only static and final variables.
7.Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor.
8.Abstract class can provide the implementation of interface.
9.Interface can't provide the implementation of abstract class.
10.The abstract keyword is used to declare abstract class.
11.The interface keyword is used to
declare interface.
Example:
public abstract class Shape {
    public abstract void draw();
} Example:
public interface Drawable{
    void draw();
}
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

No comments:

Post a Comment

Abstract Class & Interface in Java

      Abstract Class & Interface in Java         Here is the most important topic on Java Abstract class and interface, Abstraction ...