Posts

Showing posts with the label where we have to use interface and abstract class

Difference between an interface and an abstract class

What’s the difference between an interface and an abstract class in Java? It’s best to start answering this question with a brief definition of abstract classes and interfaces and then explore the differences between the two. A class must be declared  abstract  when it has one or more abstract  methods . A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do. When to use abstract methods in Java? Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below: /* the Figure class must be declared as abstract    because it contains an abstract method  */ public abstract class Figure {         /* because this is an abstract method the            body will be blank  */  ...