Exact use of abstract class and Interface in java -
i have gone through many examples abstract classes , interface not getting wats use of doing so, because both doing same job .can give me easy example figure out riddle
first of all,revision of basic diffferences :-
variables declared in java interface default final. abstract class may contain non-final variables.
members of java interface public default. java abstract class can have usual flavors of class members private, protected, etc..
java interface should implemented using keyword “implements”; java abstract class should extended using keyword “extends”.
an interface can extend java interface only, abstract class can extend java class , implement multiple java interfaces.
a java class can implement multiple interfaces can extend 1 abstract class. interface absolutely abstract , cannot instantiated; java abstract class cannot instantiated, can invoked if main() exists.
in comparison java abstract classes, java interfaces slow requires indirection.
consider using abstract classes if of these statements apply situation:
you want share code among several closely related classes. expect classes extend abstract class have many common methods or fields, or require access modifiers other public (such protected , private).
you want declare non-static or non-final fields. enables define methods can access , modify state of object belong.
consider using interfaces if of these statements apply situation:
you expect unrelated classes implement interface. example, interfaces comparable , cloneable implemented many unrelated classes.
you want specify behavior of particular data type, not concerned implements behavior.
you want take advantage of multiple inheritance of type.
(taken oracle's official tutorials).
Comments
Post a Comment