Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I implement all the methods present in an abstract class?

Tags:

java

oop

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?

like image 293
violet kiwi Avatar asked Feb 03 '14 21:02

violet kiwi


People also ask

Should I implement all methods in abstract class?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

Do All methods in an abstract class need to be abstract?

An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class.

Is it necessary to override all methods of abstract class?

In Java, it is compulsory to override abstract methods of the parent class in its child class because the derived class extends the abstract methods of the base class. If we do not override the abstract methods in the subclasses then there will be a compilation error.

Is it necessary to implement all methods of abstract class C#?

No, that's how abstract classes work. If you make your class abstract as well you don't need to implement all methods. Show activity on this post. It is necessary to override all the methods which are declared as abstract .


2 Answers

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 classes 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.       }  } 
like image 107
mdewitt Avatar answered Oct 01 '22 12:10

mdewitt


Q-1:- Should I implement all methods in abstract class?

  • Yes, you must implement all abstract methods.

Q-2 :- Can the implementing class have its own methods?

  • Yes, you can declare own (more specfic) methods.
like image 34
Christian Tapia Avatar answered Oct 01 '22 11:10

Christian Tapia