{"id":1565,"date":"2016-09-10T03:05:19","date_gmt":"2016-09-10T03:05:19","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1565"},"modified":"2016-09-10T03:13:15","modified_gmt":"2016-09-10T03:13:15","slug":"abstract-vs-interface","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/abstract-vs-interface\/","title":{"rendered":"Abstract vs Interface"},"content":{"rendered":"<p>I will give you an example first:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface LoginAuth{\r\n   public String encryptPassword(String pass);\r\n   public void checkDBforUser();\r\n}\r\n<\/pre>\n<p>Now suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class DBMySQL implements LoginAuth{\r\n          \/\/ Needs to implement both methods\r\n}\r\npublic class DBOracle implements LoginAuth{\r\n          \/\/ Needs to implement both methods\r\n}\r\npublic class DBAbc implements LoginAuth{\r\n          \/\/ Needs to implement both methods\r\n}\r\n<\/pre>\n<p>But what if encryptPassword() is not database dependent, and it&#8217;s the same for each class? Then the above would not be a good approach.<\/p>\n<p>Instead, consider this approach:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic abstract class LoginAuth{\r\n   public String encryptPassword(String pass){\r\n            \/\/ Implement the same default behavior here \r\n            \/\/ that is shared by all subclasses.\r\n   }\r\n\r\n   \/\/ Each subclass needs to provide their own implementation of this only:\r\n   public abstract void checkDBforUser();\r\n}\r\n<\/pre>\n<p>Now in each child class, we only need to implement one method &#8211; the method that is database dependent.<\/p>\n<p><strong>Technical Differences<\/strong><\/p>\n<ol>\n<li>Implementing an interface consumes very little CPU, because it&#8217;s not a class, just a bunch of names, and therefore there is no expensive look-up to do. It&#8217;s great when it matters such as in embedded devices\n<\/li>\n<li>Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.\n<\/li>\n<li>Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.<\/li>\n<li>Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).<\/li>\n<li>When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an an abstract class can extend another abstract class and abstract methods from the parent class don&#8217;t have to be defined.<\/li>\n<li>Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.<\/li>\n<li>A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.<\/li>\n<li>A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).<\/li>\n<\/ol>\n<p><strong> Consider using abstract classes if :<\/strong><br \/>\n    You want to share code among several closely related classes.<br \/>\n    You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).<br \/>\n    You want to declare non-static or non-final fields.<\/p>\n<p><strong>Consider using interfaces if :<\/strong><\/p>\n<p>    You expect that unrelated classes would implement your interface. For example,many unrelated objects can implement Serializable interface.<br \/>\n    You want to specify the behaviour of a particular data type, but not concerned about who implements its behaviour.<br \/>\n    You want to take advantage of multiple inheritance of type.<\/p>\n<p><strong>abstract class establishes &#8220;is a&#8221; relation with concrete classes. interface provides &#8220;has a&#8221; capability for classes.<br \/>\n<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will give you an example first: public interface LoginAuth{ public String encryptPassword(String pass); public void checkDBforUser(); } Now suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods: public class DBMySQL implements LoginAuth{ \/\/ Needs to implement both methods } public&hellip; <a href=\"https:\/\/codethataint.com\/blog\/abstract-vs-interface\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,98,142],"tags":[],"class_list":["post-1565","post","type-post","status-publish","format-standard","hentry","category-abstract","category-interface","category-java-concepts"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1565"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":1568,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions\/1568"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}