#67 More on Interfaces in Java
Summary
TLDRThe video script discusses the concept of interfaces in object-oriented programming, highlighting that a class can implement multiple interfaces but only extend one abstract class. It explains the use of 'implements' for classes and 'extends' for interfaces, emphasizing the need to define all methods declared in the interfaces a class implements. The script also clarifies that an interface can extend another, inheriting its methods. It concludes with the importance of using the correct reference type to access specific methods, illustrating the distinction between class and interface inheritance.
Takeaways
- 📚 A class can implement multiple interfaces, but it can only extend one abstract class.
- 🔍 When a class implements multiple interfaces, it must define all the methods declared in those interfaces to avoid errors.
- 🔑 The 'implements' keyword is used when a class is implementing an interface.
- 🔄 Interfaces can also extend other interfaces using the 'extends' keyword, allowing for inheritance of methods.
- 🚫 If an interface extends another, and a class implements the extending interface, it must provide implementations for all abstract methods from both interfaces.
- 🛑 An error will occur if a class does not implement all the required methods from the interfaces it claims to implement.
- 🔄 The concept of inheritance is valid for interfaces, allowing for the creation of a hierarchy of interfaces.
- 📝 When creating an object, the methods that can be called are limited to those defined in the interface through which the object is referenced.
- 🔍 If a method is defined in an interface, any class implementing that interface must also define that method.
- 🔄 The 'extends' keyword is used for both class-to-class and interface-to-interface relationships, indicating inheritance.
- 📌 It's important to remember the correct terminology: 'extends' for class-to-class or interface-to-interface, and 'implements' for class-to-interface.
Q & A
Can a class implement multiple interfaces?
-Yes, a class can implement multiple interfaces. This is demonstrated in the script where Class B implements both Interface A and Interface X without any errors.
What happens if a class implements multiple interfaces and there is a method name conflict?
-If there is a method name conflict, the class must define the conflicting method as well. The script shows an error when the 'run' method is not defined in Class B, which implements two interfaces with a 'run' method.
What is the difference between a class implementing an interface and a class extending another class?
-A class can extend only one other class, but it can implement multiple interfaces. The script explains that while a class can have only one superclass, it can implement multiple interfaces.
How does a class implement multiple methods from different interfaces?
-A class can implement multiple methods from different interfaces by defining all the methods declared in those interfaces. The script shows Class B implementing three methods from two different interfaces.
Can interfaces extend other interfaces?
-Yes, interfaces can extend other interfaces using the 'extends' keyword. The script demonstrates Interface Y extending Interface X.
What is the consequence of an interface extending another interface?
-When an interface extends another, it inherits the methods of the parent interface. The script shows Interface Y inheriting the 'run' method from Interface X.
What error occurs if a class implements an interface that extends another interface but doesn't implement all required methods?
-The class must implement all abstract methods from the interfaces it implements. The script shows an error when Class B implements Interface Y but does not implement the 'one' method inherited from Interface X.
What is the correct syntax for a class to implement an interface?
-The correct syntax is to use the 'implements' keyword followed by the interface name. The script mentions this when discussing Class B implementing Interface X.
What is the correct syntax for an interface to extend another interface?
-The correct syntax is to use the 'extends' keyword followed by the interface name. The script demonstrates this with Interface Y extending Interface X.
Why can't a reference of a class object call methods from an interface that the class does not implement?
-A reference of a class object can only call methods that the class implements. The script explains that a reference of Class A cannot call the 'run' method because Class A does not implement Interface X, which contains the 'run' method.
How can a class object call methods from an interface it implements?
-A class object can call methods from an interface it implements by creating a reference of the interface type and assigning an object of the class to it. The script shows creating a reference of Interface X and assigning an object of Class B to it to call the 'one' method.
Outlines
🔄 Interface Implementation and Inheritance
This paragraph discusses the capability of a class to implement multiple interfaces, contrasting it with the limitation of extending only one abstract class. It provides an example where class B implements two interfaces, A and X, and clarifies the need to define all methods from these interfaces within the class to avoid errors. The concept of interface inheritance is introduced with interface Y extending interface X, emphasizing the necessity to implement all inherited methods. The paragraph also highlights the importance of using the correct keywords 'extends' for class inheritance and 'implements' for interface implementation. It concludes with a note on the limitations of method accessibility when creating references to objects of a class implementing multiple interfaces.
Mindmap
Keywords
💡Interface
💡Class
💡Implement
💡Abstract Class
💡Method
💡Inheritance
💡Extends
💡Error
💡Reference
💡Object
💡Abstract Method
Highlights
A class can implement multiple interfaces, unlike an abstract class which can only extend one class.
When implementing multiple interfaces, a class must define all methods declared in those interfaces to avoid errors.
Interfaces can also extend other interfaces, using the 'extends' keyword, allowing for inheritance of methods.
If an interface extends another, all methods from the parent interface must be implemented in the child interface.
A class implementing an interface must provide an implementation for all abstract methods declared in the interface.
When creating a reference to an object, only methods from the class or directly implemented interfaces can be called.
To access methods from an extended interface, a reference to that specific interface type must be created.
The 'implements' keyword is used when a class is implementing an interface.
The 'extends' keyword is used for both class inheritance and interface extension.
A class cannot call methods from interfaces it does not implement directly.
Interfaces play a crucial role in enabling a class to adhere to a contract of method implementations.
The concept of interface inheritance allows for the creation of more specialized interfaces that build upon existing ones.
Error handling is essential when implementing interfaces to ensure all required methods are accounted for.
Understanding the difference between class inheritance and interface implementation is key to proper object-oriented design.
Interfaces provide a way to achieve polymorphism by allowing multiple classes to implement the same set of methods.
The transcript emphasizes the importance of method implementation when dealing with multiple interfaces in a class.
The need for interfaces is to define a blueprint of methods that classes must implement to ensure consistency across different implementations.
Transcripts
in fact before talking about the need
for interface we have one more thing to
add here so let's say if I have a
interface and I'm getting a class which
implements a one interface here can a
class Implement multiple interface
example let's say if I have interface
which is called X so if I say interface
X so B Class implements a and X both and
there says yes if you can see there is
no error here the only thing is whatever
method you define here so let's say the
method name here is run you can see we
got an error here the only thing you
have to do is you have to Define run as
well so you will say public void run so
basically we can have one class
implementing multiple interfaces now
that's not the case with abstract class
right because in abstract class we can
we can extern only one class in
interface you can have a class
implementing multiple interfaces
so here I can simply print running okay
so what we are doing is we are using
multiple methods here we have three
methods coming from two different
interfaces this interface says two
methods show in config and this
interface is has only one method which
is one and you can see we got three
methods here uh there's one more thing
let's say we have one more interface
which is called uh interface y now the
thing is when you talk about class and
Class A Class can extend another class
right what about interface but can we do
interface and interface what keyword we
have to use here see whenever you have
interface to interface we say extends so
interface y extends X so inheritance is
is valid here as well now in this y
interface you will be having a method
called run so that's the inheritance
okay so you will get this declaration in
the interface y as well let me show you
if I remove this X and if I keep y even
now we have to use one otherwise if you
remove run from here if you you comment
this part you can see it will give you
error it says the type B must implement
the United abstract method one
okay but we are implementing y right
it's just that y extends X so we have to
use one
now Point remember here is whenever you
have a class to class we say the extends
whenever you have class to interface
basically a class is in implementing
interface so we have to say implements
and whenever you have interface to
interface we say that extends again okay
this is the point you have to remember
so again I will comment uncomment this
part
okay now there's only one thing you have
to remember whenever you create a
reference of a phase and object of B
which is a class I can only call two
methods which is of part A if I try to
call run with a it will not work because
a has no idea what run method is if you
can see in a we don't have one in fact
what we will do is I will just remove
this code for time in yeah and then if
you want to implement one if you want to
call one in that case we need to create
the reference of X which is obj1 is
equal to Nu B now with this obj one I
can call one but again I can't call
other two two other methods okay so
that's one thing you have to remember
okay now once we are good with this
let's try to understand what's the need
of the interface
関連動画をさらに表示
Abstract Classes and Methods in Java Explained in 7 Minutes
Inheritance in Python | Python Tutorials for Beginners #lec89
Python OOP Tutorial 1: Classes and Instances
CH01_VID02_Encapsulation
Java Tutorial: Access modifiers, getters & setters in Java
Classes and Objects in Python | OOP in Python | Python for Beginners #lec85
5.0 / 5 (0 votes)