Understanding Interfaces in Object-Oriented Programming

Slide Note
Embed
Share

Interfaces in OOP provide a way for unrelated classes to share common methods without implementation details. They serve as a set of requirements that must be implemented by classes. By creating interfaces, we can enforce standards and avoid issues like the "Diamond of Death" problem that arises from multiple inheritance. Learn how interfaces work, their significance, and how they differ from abstract classes in programming.


Uploaded on Jul 10, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Module 3 - Part 4 Interfaces

  2. 2 Introduction To understand interfaces, let s do two things: 1. Start with a real world example 2. Revisit an old problem from OOP

  3. Interfaces in the real world In order to succeed in this class you need to have access to a computer. You need the computer to code your labs, your assignments, to see the slides and perhaps watch the lectures You need the computer to take the quizzes and tests. In order to take the test, you need a webcam Perhaps you need to be able to print. If the requirements for this class were put in the most nerdy of terms, you would be required to implement have_access_to_computer. interface have_access_to_computer { public void run_IDE(); public void has_web_cam(); public void can_print(); }

  4. Thoughts about have_access_to_computer Note that it doesn t say you have to have a Mac, PC, Chromebook, Tablet or Linux Machine, it just says computer. Any computer that can satisfy the requirements (ie, that can implement the required methods) will satisfy the interface. Different students may have very different implementations of this interface Again, to make it nerdy: class student implements have_access_to_computer { } The simplest way to think of an interface is a set of requirements that must be met by all classes which implement that interface. In the real world, they are used to enforce standards and break up work across developers.

  5. Review Multiple Inheritance not allowed (except in C++) Man Wolf - weight - num puppies + run( ) + run( ) Werewolf - weight - num puppies + run( ) Werewolf w = new Werewolf( ); w.run(); // which run method is called??

  6. Better example Mammal - weight + run( ) Man Wolf - weight - num puppies + run( ) + run( ) Werewolf - weight - num puppies + run( ) Programmers call this the Diamond of Death

  7. Diamond of Death Man and Wolf inherit from Mammal If both override run( ), which version does Werewolf inherit? What s the solution to this? Don t allow multiple inheritance Instead, use interfaces

  8. What is an interface? Very similar to an abstract class Allows unrelated classes to share common methods The rules : Methods must be abstract methods (i.e. no code) Even if you don t list the word abstract or virtual Interfaces do not contain a constructor

  9. More rules Convention says to use a capital I as the first letter for the name of the interface (e.g. ITalkable) C# You can implement multiple interfaces using a comma You cannot instantiate it (i.e. make an object) Interfaces can have attributes in Java, but they must be initialized at declaration. In C# you cannot have attributes in an interface.

  10. An Example Interface - Java // Java version interface Talkable { void talk(); // talk is both public and abstract! } class Dog implements Talkable { // Note: talk must be public here too because // by default it is protected, which is more restrictive @Override public void talk() { System.out.println ("Woof"); } } class Main { public static void main(String[] args) { Dog d = new Dog(); d.talk(); } }

  11. An Example Interface C# // C# version interface ITalkable { void talk(); // talk is both public and abstract! } class Dog : ITalkable { // Note: talk must be public here too because // by default it is protected, which is more restrictive public void talk() { Console.WriteLine("Woof"); } } class Example { public static void Main (string[] args) { Dog d = new Dog(); d.talk(); } }

  12. Last rule A class that implements an interface has a choice: 1. Override the methods, OR 2. Declare itself as abstract

  13. Abstract class implementing interface // implement an interface but not override interface Talkable { // talk is still abstract void talk(); } abstract class Dog implements Talkable { // inherited abstract method, so class must // override or be abstract } class Main { public static void main(String[] args) { // no longer allowable //Dog d = new Dog(); } }

  14. Implementing more than one interface // C# version interface ITalkable { void talk(); // one method } interface IConsumer { void eat(); // another method } class Dog : ITalkable, IConsumer { // Override methods from both interfaces public void talk() { Console.WriteLine("Talk");} public void eat () { Console.WriteLine("Eat");} }

  15. Implementing more than one interface // Java version interface Talkable { void talk(); // one method } interface Consumer { void eat(); // second method } class Dog implements Talkable, Consumer { // Override methods from both interfaces public void talk() {System.out.println ("Talk");} public void eat() {System.out.println ("Eat");} }

  16. Another Example Imagine you re building a video game that uses Artificial Intelligence It s a magical world and non-animate objects can come to life How could interfaces be used here?

  17. Summary Interfaces Are an alternate to multiple inheritance and the Diamond of Death Are similar to abstract classes Contain only abstract methods and attributes A class implements an interface

Related


More Related Content