Abstract Class:
  • Abstract class means hiding the implementation  and showing the function definition to the user is known as Abstract class
  • Abstract classes having Abstract methods and normal methods (non abstract methods) will be there.
  • Abstract classes having methods will be anything means public ,private,protected.
    In Abstract classes  variables will be anything( public, private, protected)
    For Abstract classes we not able to create object directly.But Indirectily we can create object using sub calss object.
  • A Java abstract class should be extended using keyword “extends”.
  • A Java abstract class can have instance methods that implements a default behavior.
    If you know requirement and partially implementation you can go for Abstract classes.
    abstract  class  can extend from a class or from an abstract class.
  • Abstract class can extend only one class or one abstract class at a time. soAbstract classes can't support multiple inheritance.
  • In comparison with java Interfaces, java Abstract classes are fast.
    If you add new method to abstract class, you can provide default implementation of it. So you don’t need to change your current code.
  • Abstract classes can have constructors .
    We can run an abstract class if it has main() method.

Example Program:


abstract class A{

abstract void display();
public void show(){
S.o.p("Indhu");
}

Public class B extends A{
void display();
}
Abstract class C Extends B{
//Escaping Here
}
public static void main(String args()){
A a= new B();
a.display();
a.show();

}

Interface :

  • Interface nothing but some set of rules.
  • Interfaces having only Abstract methods.it is purely Achive Abstraction.
  • In Interfaces by default the methods will be public abstract methods.
  • In Interfaces by default the variables will be static final .
  • For Interfaces we can't create object directly or Indirectly but we can give sub class object reference to interface .
  • Java interface should be implemented using keyword “implements”. 
  • methods of a Java interface are implicitly abstract and cannot have implementations.
  • If u don't know Any requirement and any implementation you can go for Interfaces.
  • Interface can extend only from an interface
  • Interface can extend any number of interfaces at a time. so interfaces can support multiple inheritance(syntactical not implementation of multiple inheritance).
  • In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
  • if  you add new method to interface, you have to change the classes which are implementing that interface
  • Interface having no constructor.
    we can’t run an interface because they can’t have main method implementation.
Example Program:

public interface Payment
{
    void makePayment();//by default it is a abstract method
}
public class PayPal implements Payment
{
    public void makePayment()
    {
        //some logic for paypal payment
        //like paypal uses username and password for payment
    }
}
public class CreditCard implements Payment
{
    public void makePayment()
    {
        //some logic for CreditCard payment
        //like CreditCard uses card number, date of expiry etc...
    }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu