Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8944

Re: Abstract Class and class-method

$
0
0

Hi Madhu,

 

in your example, both approaches will work just fine.

 

But...

 

Abstract classes vs. non abstract base classes

 

Abstract classes are tools to make coding easier by making it easier to communicate the intent of your code to your fellow programmer.

In your example, if I only look at the code you have, I could try to create an instance of the class Main_class which could work or not depending on the implementation. But if you make the class abstract, I know that the Main_class is not intended to be used directly. This will prevent me from making a mistake.

 

Abstract classes vs. interfaces

 

You can achieve that I never try to create an instance of Main_class by making it an interface. But now you force me to implement all methods of Main_class anytime I want to create a new class to be used by the static method. If it is possible to provide meaningful default implementation for some of the methods, it makes sense to provide an abstract base class that implements all these method for me so that I can create a new class with less effort.

 

Best regards,

 

Frank


Viewing all articles
Browse latest Browse all 8944

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>