Showing posts with label Abstract Class. Show all posts
Showing posts with label Abstract Class. Show all posts

How to access Abstract class concrete methods in java?


Basically we can't create instance for Abstract class. In that case if we need to access the concrete methods of abstract class then we need to extend abstract class to other class and by instance of those classes we call Abstract class concrete methods.

In other way we can create anonymous class and by using those instance we can call abstract class methods. Lets see example for both the approaches below,

My Abstract class:


public abstract class MyAbstract {
 
 public abstract void add();
 
 public void test(){
  System.out.println("Inside ABSTRACT class");
 }
}


Using Instance of other class:


public class MyTestClass extends MyAbstract{
 
 @Override
 public void add() {
  System.out.println("Inside add method");
 }
 
 public static void main(String[] args) {
  
  MyTestClass obj  = new MyTestClass();
  obj.test();
  obj.add(); 
 }
}

OUTPUT:


Inside ABSTRACT class
Inside add method



Using anonymous class:



public class MyTestClass {
 
 public static void main(String[] args) {
  
  MyAbstract obj = new MyAbstract() {
   
   @Override
   public void add() {
    System.out.println("Inside add method");
    
   }
  }; 
  obj.test();
  obj.add();   
 }
}


OUTPUT:


Inside ABSTRACT class
Inside add method







Difference between Abstract Class and Interface


This is one of the important interview question ever asked in Java interviews like, What is the the difference between Abstract class and Interface? When we need to use Abstract class and Interface? etc., First lets walk through what is Abstract Class and Interface. 

Abstract Class:


Abstract class is a class which declared as abstract, may or may not contain abstract methods. Abstract method is a method which wont't have any method definition. So in this case abstract class can have abstract methods as well as concrete methods. Once we say these answer few other questions which may asked from interviewer are,


Abstract class can have class variable or not?

Answer: Yes.

Abstract class can have multiple abstract methods and concrete methods? 

Answer: Yes, abstract class can have any no. of abstract and concrete methods.

Abstract class can extend other abstract class and can implement interfaces?

Answer: Yes, It can extend any class and can implement N no. of interfaces. 

Abstract class can also be a Final class?

Answer: No, it can either one only. 

Abstract class can be instantiated?

Answer: No, Abstract class can not be instantiated directly. But we can instantiate by using anonymous class. 

Main thing which we need to remember is, if a class contains any abstract method then compulsory class needs to be declared as abstract.



Interface:
Interfaces will be declared only with method declarations, there will not be method definition. In general all methods are implicitly abstract methods in Interface. Suppose if a Interface declared without any method declaration or variable, then its a Marker Interface. As same as Abstract class few other questions which may be on Interface are,

Interface can have variable or not?
Answer: Yes we can have but it should be Final variable. 

Interface can extend other class ?
Answer: No, Interface can extends only other interfaces.

How many Interfaces can be extended in a single Interface?
Answer: Interface can extends multiple Interfaces like below

public interface MyInterface  extends FirstInterface, SecondInterface {

public int val = 10;

public void myMethod();
}


Difference between Abstract class and Interface?

  • Abstract class can have Abstract method and as well as concrete methods. But Interface always should will have Abstract methods. 
  • Overriding all the Abstract method is not mandatory while we extends Abstract class. But if its a Final class then we need to Override all abstract methods. On other hand when we implements Interface we need to Override all abstract methods in our class. Only in case of Abstract class we don't need to provide implementation (Override) for all methods in Interface. 
  • Abstract class methods can have any access modifiers. But Interface methods should be always public. 
  • As we seen above Abstract class and extends only one class, where as Interface can extends multiple Interfaces. 
  • Abstract class can have Final and non-Final variables. Where as Interface can contain only Final variables. 
  • Abstract class can have Constructors, but Interface cannot have Constructor.
  • As by performance Abstract class will be faster than Interface since it consumes little more time on routing to Override methods. 
  • When we add any new methods in Abstract class then the base classes won't get affected. But in case of Interface if we add any method then all implemented classes will get affected and need to implemented those new methods also.

When to use Abstract class and Interface in Java?

Whenever we talk about When to use Abstract class and Interface in Java will lead to a multiple questions and answers. So in that case programmer need to decide to choice his best in his design stage. Lets see few choices of using Abstract class or Interface according to our needs,

  • Abstract class is more suited for code reuse like inheritance, where as Interfaces are better suited for Type declaration.
  • Using Interface will be suitable if we feel API will be stable for quite long time.
  • Abstract classes are better if we add any methods in our future, since it won't break the code. 
  • Abstract class will be better when we have common method definition and also non public methods and variables. 
  • Interface will be better when we need similar to multiple inheritance. 
  • Best suitable for Template pattern will be Abstract class declaration. 
  • Prefer Interface for providing more design flexibility.