Sunday 10 January 2016

What is an abstract class and interface?

Abstract Class:
An abstract class is a special kind of class that cannot be instantiated.It only allows other classes to inherit from it but cannot be instantiated. Abstract class are incomplete class.Derived classes called concrete classes that must define the missing pieces of base class.Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts of those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error.
Why Abstract Class?
The advantage of abstract class is it enforces certain hierarchies for all the subclasses. It is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

public abstract class Customer      
    {
        protected String id;
        protected String lname;
        protected String fname;

        //properties
        public abstract String ID
        {
            get;
            set;
        }

        public abstract String FirstName
        {
            get;
            set;
        }
    
        public abstract String LastName
        {
            get;
            set;
        }
    
        //completed methods
        public String Add()
        {
            return "Customer " + id + " " +
                      lname + " " + fname +
                      " added";
        }
            
        //abstract method
       public abstract String Address();
    
    }
 


Interface:
An interface is an entity that is defined by the word Interface it is not a class. An interface has no implementation; it only has the signature, it just define the methods without the body. Like Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. 

Example 1:
interface IMario
2{
3  Void behavior();
4}
5
6class  Mario implements IMario
7{
8 public void behavior()
9 {
10     System.out.println("This is simple Mario");
11 }
12}
13
14class  SuperMario implements IMario
15{
16  public void behavior()
17  {
18     System.out.println("This is Super Mario with power");
19  }
20}
21
22class  TestMario
23{
24   public static void main(String args[])
25   {
26        System.out.println("----game started----");
27        Imario obj=new Mario();
28        Obj.behavior();
29        System.out.println(-----After Mashroom-----");
30        obj=new SuperMario();
31        Obj.behavior();
32        System.out.println(-----After Power Loss-----");
33        obj=new Mario();
34        Obj.behavior();
35   }
36}
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:
1interface A
2{
3   void displayA()
4}
5
6abstract class B
7{
8   public void displayB()
9   {
10     System.out.println("Display-B");
11   }
12   abstract public void display();
13}
14
15class  extends implements A
16{
17   Public void displayB()
18  {
19      System.out.println("Abstract Display-B");
20  }
21
22  public void displayA()
23 {
24     System.out.println("Display-A");
25  }
26}
27
28class  TestClass
29{
30  public static void main(String args[])
31 {
32   C obj=new C();
33   Obj.display();
34   Obj.displayA();
35   Obj.displayB();
36 }
37}

No comments:

Post a Comment