Saturday, July 28, 2012

Interfaces in Java

Before reading this post, I recommend you to read the following posts.
Runtime polymorphism in Java
Abstract Classes in Java:Runtime polymorphism

An interface is a collection of methods that must be implemented by the implementing class. An interface only tells the class what to do. Thus, the interfaces only have the declaration of member variables and methods. Implementing class defines all the methods declared in the interface. If a class implements an interface and does not implement all the methods then the class must be declared as abstract. Variables in interface automatically become static and final variables of the implementing class. Members of interface are implicitly public. A class cannot narrow the accessibility of an interface.

An object reference of an interface can refer to an object of implementing class. However, only methods that are present in the interface can be called using the object reference of interface.

Now lets put the interface into implementation. We consider the implementation of the Stack data structure. Below is the Stack interface and its implementing class MyStack.


The public class which uses the interface and its implementing class is as follows.




Interface Vs. Abstract Class:
Both interface and abstract class cannot have objects, but can have object references. In case of interface, object reference can reference an object of the implementing class. An object reference of abstract class can reference an object of subclass. However, abstract class can have non-abstract methods and instance variables but interface can have neither of them.

Java allows interfaces to be empty. An empty interface contains no variables and methods. It is used to mark a class to be having certain behavior.

Related Posts:
Abstract Classes in Java:Runtime polymorphism
Runtime polymorphism in Java
How Interfaces are used internally in Java

25 comments:

  1. In case, if you are facing QuickBooks Error code H202, you won’t be able to access your QuickBooks account. Such an error code generally occurs when one QuickBooks users attempt to use in the multiple- user modes. The problem appears with the many androids, I phones, Tablets, and desktop users. Sometimes the error resolution method is not in the user's hand due to less technical knowledge. In such a condition don't get panic as we have tech-specialists to deal with your problem. Therefore, contact to our technical experts as soon as possible via dialing helpline number. They are well adequately experienced in problem-solving.

    ReplyDelete
  2. Our services are realistic and proper for students seeking balanced social-academic lives. helpinhomework.org delivers the needed balance by assuming all the academic responsibilities and thereby offering students’ ample time in their social pursuits.

    We manage a team of highly motivated top writers that have acquired experience in related disciplines. Our team of writers comprises of brilliant and intellectual minds that are able to transform ideas into words.

    Our services are available 24/7. Talk to us any time. We guarantee you quality and 100% satisfaction.

    Visit us Now !!!******************
    how to write a good essay

    ReplyDelete
  3. Hi, Everyone, I am a Very naughty and extremely ravishing young woman, available for Udaipur Escorts for a discreet man to have a good time, unforgettable sensations. I'm a young woman, sweet, friendly, very charming, and natural Call Girl in Udaipur. I will be your friend and the lover or even more than that. You will feel how tension and stress leave your body and that space fills with passion and desire and which is possible only through Udaipur Call Girl.

    ReplyDelete
  4. Bachelor of Economics in Australia Economics offers understudies the scientific and quantitative aptitudes needed for a top to bottom comprehension of key monetary principles.Career alternatives incorporate financial aspects examination and displaying, monetary anticipating, econometrician, and planning monetary strategies in industry, government, counseling and monetary establishments.

    ReplyDelete
  5. The enhanced demand for online learning has arisen from the growing need to have a framework that not only increases medical professionals’ expertise but also allows them to keep up with new developments in the medical field. Digitalisation has helped to open up a slew of new opportunities for the global Medical Education Market.

    ReplyDelete
  6. Our paper helper is available online that provides expert-written paper writing assistance to students who fail to complete their school, university, and college tasks over time.

    ReplyDelete
  7. McAfee is the best and most trusted MCafee not working program that has been crafted with impressive features or ability to identify malicious viruses that are on your laptop, computer or other Android device. When you Mcafee password Reset your program, you will be able to benefit from its advanced technology and other features. If you're having problems while trying to updating McAfee Antivirus on Windows 10. Feel free to contact us at the Antivirus Support Team. You can also use live chat or email us for us for a quick resolution.
    Our expert team is all-hours available to help you. HP 4500 Printer Offline

    ReplyDelete

  8. Would you like to know how to Cash App Refund? If you are having a problem, then you are in the right place. If you have any issues with your cash app, we will provide you with instant steps by which you can resolve them. click here: - Cash App Merchant Refund

    ReplyDelete
  9. Thank you for this nicely written and educative blog... waiting for more update. check
    fupre past questions and answers
    fupre past questions and answers
    fupre past questions and answers for more info on past questions and answers

    ReplyDelete
  10. The way you explained java in this post is truly praiseworthy.

    We are on a mission to help students in their academics. Check out the for Accounting Assignment help, our experts never compromise the quality of the paper covering all branches and accurate data of accounting. Their hard work helps you to score top grades.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...