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 Indirectly we can create object using sub class 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. so Abstract 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.
Interface :
- Interface nothing but some set of rules.
- Interfaces having only Abstract methods.it is purely Achieve 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.
No comments