Interfaces in Object-Oriented Programming

 
Module 3 - Part 4
 
Interfaces
 
2
 
I
n
t
r
o
d
u
c
t
i
o
n
 
To understand interfaces, let’s do two
things:
 
1.
Start with a real world example
2.
Revisit an old problem from OOP
 
I
n
t
e
r
f
a
c
e
s
 
i
n
 
t
h
e
 
r
e
a
l
 
w
o
r
l
d
 
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();
}
 
T
h
o
u
g
h
t
s
 
a
b
o
u
t
 
h
a
v
e
_
a
c
c
e
s
s
_
t
o
_
c
o
m
p
u
t
e
r
 
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.
 
R
e
v
i
e
w
 
 
M
u
l
t
i
p
l
e
 
I
n
h
e
r
i
t
a
n
c
e
 
n
o
t
 
a
l
l
o
w
e
d
(
e
x
c
e
p
t
 
i
n
 
C
+
+
)
 
 
 
 
 
 
 
 
 
Werewolf w = 
new
 Werewolf( );
w.run(); 
// which run method is called?
?
Man
- weight
 
+ run( )
Wolf
- num puppies
 
+ run( )
Werewolf
 - weight
 - num puppies
 
+ run( )
 
B
e
t
t
e
r
 
e
x
a
m
p
l
e
 
Programmers call this the “Diamond of Death”
Man
- weight
 
+ run( )
Wolf
- num puppies
 
+ run( )
Werewolf
 - weight
 - num puppies
 
+ run( )
Mammal
- weight
 
+ run( )
 
D
i
a
m
o
n
d
 
o
f
 
D
e
a
t
h
 
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 
interface
s
 
W
h
a
t
 
i
s
 
a
n
 
i
n
t
e
r
f
a
c
e
?
 
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
 
M
o
r
e
 
r
u
l
e
s
 
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.
 
// 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();
  }
}
 
A
n
 
E
x
a
m
p
l
e
 
I
n
t
e
r
f
a
c
e
 
-
 
J
a
v
a
 
// 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();
  }
}
 
A
n
 
E
x
a
m
p
l
e
 
I
n
t
e
r
f
a
c
e
 
C
#
 
L
a
s
t
 
r
u
l
e
 
A class that implements an interface has a
choice:
1.
Override the methods, OR
2.
Declare itself as abstract
 
// 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();
  }
}
 
A
b
s
t
r
a
c
t
 
c
l
a
s
s
 
i
m
p
l
e
m
e
n
t
i
n
g
 
i
n
t
e
r
f
a
c
e
 
I
m
p
l
e
m
e
n
t
i
n
g
 
m
o
r
e
 
t
h
a
n
 
o
n
e
 
i
n
t
e
r
f
a
c
e
 
// 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"
);}
}
 
I
m
p
l
e
m
e
n
t
i
n
g
 
m
o
r
e
 
t
h
a
n
 
o
n
e
 
i
n
t
e
r
f
a
c
e
 
// 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"
);}
}
 
A
n
o
t
h
e
r
 
E
x
a
m
p
l
e
 
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?
 
S
u
m
m
a
r
y
 
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
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.

  • Interfaces
  • Object-Oriented Programming
  • OOP Concepts
  • Programming Principles

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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#