Should I implement all the methods present in an abstract class?
java, oop
Solution
When you extend an `Interface` or an `Abstract` class you are creating a contract of sorts with that `superclass`. In the contract you are saying:
"I will implement all unimplemented methods in my superclass"
If you do not, implement all the unimplemented methods, then you are breaking your contract. A way to not break your contract is make your subclass `Abstract` as well as a way of saying
"I have not implemented all the methods in my contract, I am going to have my subclasses implement them".
For your class `bar` right now, you must implement `b()` or make `bar` an `Abstract` class or you are not fulfilling your contract with `MyAbstractClass`
The basic idea is:
Interface: None of my methods are implemented. A subclass must implement all my methods in order to implement me. (Note: I believe default interfaces have been added to Java 8 which may change this a bit)
Example:
public interface myInterface
{
//My subclasses must implement this to fulfill their contract with me
public void methodA();
//My subclasses must implement this to fulfill their contract with me
public void methodB();
}
Abstract: I may implement some of my methods, but I will also leave methods as abstract so that my subclasses must implement because they can implement those classes to suit their needs better than I can.
Example:
public abstract class myAbstractClass
{
//My subclasses must implement this to fulfill their contract with me
public abstract void methodC();
public void helloWorld()
{
System.out.println("Hello World");
}
}
`Abstract` classes can also extend interfaces so they can implement some of their methods. But they can also leave some of the methods unimplemented so the subclass can implement them. If you leave an interface method unimplemented, there is not need to declare it abstract, it is already in the contract.
Example:
public abstract class myAbstractClass2 implement myInterface
{
@Override
public void methodA()
{
// this fulfills part of the contract with myInterface.
// my subclasses will not need to implement this unless they want to override
// my implementation.
}
//My subclasses must implement this to fulfill their contract with me
public abstract void methodD();
}
So in essence, an abstract class doesn't have as strict a contract with it's superclass because it can delegate its methods to its subclasses.
Regular Class: (I use regular to mean non-interface, and non-abstract). I must implement all unimplemented methods from all of my superclasses. These classes have a binding contract.
Example:
public class mySubClass extends myAbstractClass2
{
@Override
public void methodB()
{
//must be implemented to fulfill contract with myInterface
}
@Override
public void methodD()
{
//must be implemented to fulfill contract with myAbstractClass2
}
public void myMethod()
{
//This is a method specifically for mySubClass.
}
}
Problem
Below is the code snippet: ``` public abstract class MyAbstractClass { public abstract void a(); public abstract void b(); } public class Foo extends MyAbstractClass { public void a() { System.out.println("hello"); } public void b(){ System.out.println("bye"); } } public class Bar extends MyAbstractClass { public void a() { System.out.println("hello"); } public void delta() { System.out.println("gamma"); } } ``` There are couple of questions that I have: Q-1 :- Should I implement ALL the methods in `abstract` class? Q-2 :- Can the implementing class have its own methods?