java - Which is preferred: abstract class vs interface? -


i came across few design constructs in large application:

  1. independent use of abstract classes
  2. independent use of interfaces
  3. abstract class implementing interface
  4. interface extending , interface

being new java, appreciate if pinpoint leverage/advantage of 1 on other in specific situations. miss if dont use such constructs?

what abstract class?
abstract class special kind of class cannot instantiated. question why need class cannot instantiated? abstract class sub-classed (inherited from). in other words, allows other classes inherit cannot instantiated. advantage enforces hierarchies subclasses. in simple words, it kind of contract forces subclasses carry on same hierarchies or standards.

what interface?
interface not class. entity defined word interface. interface has no implementation; has signature or in other words, definition of methods without body. 1 of similarities abstract class, contract used define hierarchies subclasses or defines specific set of methods , arguments. main difference between them class can implement more 1 interface can inherit 1 abstract class. since java doesn’t support multiple inheritance, interfaces used implement multiple inheritance.

both together
when create interface, creating set of methods without implementation must overridden implemented classes. advantage provides way class part of 2 classes: 1 inheritance hierarchy , 1 interface.
when create abstract class, creating base class might have 1 or more completed methods @ least 1 or more methods left uncompleted , declared abstract. if methods of abstract class uncompleted same interface. purpose of abstract class provide base class definition how set of derived classes work , allow programmers fill implementation in derived classes.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -