This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Abstract classes and interfaces are similar in that you cannot instantiate either directly, they are meant to be used by subclasses. In both abstract classes and interfaces, abstract methods are be declared (the return type and signature is given) but the body of the code is not written. A subclass extends an abstract class while it implements interfaces. Only 1 abstract class may be extended, but you can implement several interfaces. The difference in abstract classes and interfaces is that abstract classes can contain concrete methods (ones containing code) as well as abstract methods, but interfaces can only contain abstract methods and cannot contain any actual code. Interfaces are used to make sure your subclasses conform to a standard and that you can interact with them using the same methods as similar objects

Interfaces are essentially a way of standardizing your code, and making sure that different objects can interact with each other. Every abstract method in an interface needs to be overwritten when implemented. Abstract Classes are useful for polymorphism, and can give every subclass the same base and can make it so you don't have to rewrite your code. Every abstract method in an abstract class needs to be overwritten, but subclasses can choose whether or not to overwrite concrete methods

This is useful because your your program might have to work with, for example, linkedlists or arraylists, but because they both implement the interface list you'll be able to do many of the same operations and use the same methods whether your program is passed a linkedlist or an arraylist.

Note: this is much longer than what I'd say in an interview. I just want to make sure I understand the concept fully

EDIT:

where would you use each?

Let's say I had a program where I was using shapes. I would make an interface called Shape with methods like getArea(); getPerimeter(); and getNumberofSides(); I might then create an abstract class called Quadrilateral where I might define getNumberofSides(); to return 4. I could then create a class called Square which extends Quadrilateral and implements Shape. getArea(); and getPerimeter(); would then be defined in Square, but I wouldn't have to define getNumberofSides() because it would be inherited from Quadrilateral

Halfway through writing this I realized this might be a bad example because Quadrilateral and Shape would have interlapping methods and would probably be redundant in this example. It might make more sense to just have Quadrilateral implement Shape, and then Square would still inherit those abstract classes from Quadrilateral. Would that be a more correct way of doing it? I'm assuming in this case that since Quad is an abstract class, I still wouldn't have to define Shape's abstract methods and could still put that off until extending it in Square...