Monday, January 7, 2019

Abstract Class and Interface in Java

Abstract Class and Interface in Java

Abstract Class

- Abstract classes are java class which will allow both concrete methods and abstract methods.

- In Java application,for abstract class we able to declare reference variable ,but we are unable to
create objects.

- Implement all the abstract methods of abstract class in sub class.if we violate this rule,then
compiler will rise an error.

- In main class (in main()),declare reference variable either for abstract class or sub class.
But we must create object for sub class.

NOTE:

If we declare reference variable for abstract class,then we are able to to access only
abstract class method.

If we declare reference variable for sub class,then we are able to access both abstract
class members and sub class members.

Example1:

package com.test;

abstract class A{
      
       void m1() {
             System.out.println("M1-A");
       }
      
       abstract void m2();
       abstract void m3();
}

class B extends A{

       @Override
       void m2() {
             System.out.println("M2-B");
            
       }

       @Override
       void m3() {
             System.out.println("M3-B");
            
       }
      
       void m4() {
             System.out.println("M4-B");
       }
      
}

public class Test {

       public static void main(String[] args) {
             System.out.println("Part A");
             //A a=new A();//Error-    - Cannot instantiate the type  A
             A a=new B();
             a.m1();
             a.m2();
             a.m3();
            
             //a.m4();//Error- The method m4() is undefined for the type A
            
             System.out.println("Part B");
             System.out.println("Part B");
             B b=new B();
             b.m1();
             b.m2();
             b.m3();
             b.m4();

       }

}

 
Output:

Part A
 
M1-A
M2-B
M3-B

Part B
 
M1-A
M2-B
M3-B
M4-B

Interface


-Interface is a java container.It allows only abstract method,It will not allows concrete methods.

- Interface is a java element,It can be utilized to declare or expose only service name in the form
of abstract method.It will not provide service implementation.

- In Java application,for interface, we able to create only reference variable ,but we are unable to
create objects.

- In case of interface,by default all the variable are public static final.

- In case of interface,by default all the methods are public and abstract.

NOTE:
While implementing abstract methods,we must declare methods with public.Otherwise
compiler will rise an error.

-In the main class,we can declare reference variable either for interface or for implementation class
but we must create object for implementation class.

NOTE:
 
If we declare reference variable for interface then we are able to access only interface members,
we are unable to access implementation class own members.

If we declare reference variable for implementation class then we are able to access
both interface members and implementation class own members.


package com.test;

interface AccountService {
       void create(String accNo, String accName, String accType);

}

class AccountServiceImp implements AccountService {

       @Override
       public void create(String accNo, String accName, String accType) {
             System.out.println("Accounts is created successfully with below detail");
              System.out.println(".................................................");
             System.out.println("Account Numbers:" + accNo);
             System.out.println("Account Numbers:" + accName);
             System.out.println("Account Numbers:" + accType);

       }

}

public class MainProject {

       public static void main(String[] args) {

             AccountService account = new AccountServiceImp();
             account.create("1266366", "Nagraj", "Saving");

       }

}


Output:
Accounts is created successfully with below detail
.................................................
Account Numbers:1266366
Account Numbers:Nagraj
Account Numbers:Saving




0 comments:

Post a Comment