#67 More on Interfaces in Java

Telusko
18 Jan 202303:21

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

00:00

🔄 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

An interface in programming is a contract that specifies a set of methods that a class must implement. In the video, interfaces are discussed as a way for a class to adhere to multiple contracts by implementing multiple interfaces, showcasing the flexibility and modularity of object-oriented design.

💡Class

A class is a blueprint for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). The script discusses how a class can implement multiple interfaces, but can only extend one abstract class, highlighting the fundamental differences between class inheritance and interface implementation.

💡Implement

To implement in the context of programming means to provide the specific code for the methods declared in an interface or abstract class. The video script uses the term to explain how a class can fulfill the requirements of multiple interfaces by defining the necessary methods.

💡Abstract Class

An abstract class is a class that cannot be instantiated on its own and is often used as a base class for other classes. The script contrasts abstract classes with interfaces, noting that while a class can only extend one abstract class, it can implement multiple interfaces.

💡Method

A method is a function that is associated with a class or interface. The script explains that when a class implements an interface, it must provide an implementation for all the methods declared in that interface, using the 'run' method as an example.

💡Inheritance

Inheritance is a mechanism in object-oriented programming where a class (subclass) can inherit properties and behaviors (methods) from another class (superclass). The video script discusses how interfaces can also extend other interfaces, using the 'extends' keyword, which is similar to class inheritance.

💡Extends

The 'extends' keyword is used in programming to indicate that one class is inheriting from another or that one interface is inheriting from another. The script uses this term to explain the relationship between an interface and a base interface from which it inherits methods.

💡Error

In the context of programming, an error refers to a situation where the code does not compile due to syntax issues or missing implementations. The script mentions errors that occur when a class fails to implement all the methods declared in the interfaces it has implemented.

💡Reference

A reference in programming is a way to access an object in memory. The script explains that when creating a reference to an object of a class that implements multiple interfaces, the methods that can be called depend on the interface through which the reference is created.

💡Object

An object is an instance of a class, embodying the state and behavior defined by the class. The video script discusses how objects of a class that implements multiple interfaces can have different methods called on them depending on the interface reference used to access the object.

💡Abstract Method

An abstract method is a method declared in an abstract class or interface with no implementation, requiring any subclass or implementing class to provide the implementation. The script refers to abstract methods in the context of interfaces and the necessity for a class to implement these methods to avoid compilation errors.

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

play00:00

in fact before talking about the need

play00:01

for interface we have one more thing to

play00:03

add here so let's say if I have a

play00:07

interface and I'm getting a class which

play00:08

implements a one interface here can a

play00:11

class Implement multiple interface

play00:13

example let's say if I have interface

play00:15

which is called X so if I say interface

play00:17

X so B Class implements a and X both and

play00:22

there says yes if you can see there is

play00:23

no error here the only thing is whatever

play00:25

method you define here so let's say the

play00:27

method name here is run you can see we

play00:30

got an error here the only thing you

play00:32

have to do is you have to Define run as

play00:34

well so you will say public void run so

play00:37

basically we can have one class

play00:39

implementing multiple interfaces now

play00:41

that's not the case with abstract class

play00:43

right because in abstract class we can

play00:44

we can extern only one class in

play00:46

interface you can have a class

play00:48

implementing multiple interfaces

play00:51

so here I can simply print running okay

play00:55

so what we are doing is we are using

play00:57

multiple methods here we have three

play00:58

methods coming from two different

play01:00

interfaces this interface says two

play01:02

methods show in config and this

play01:04

interface is has only one method which

play01:06

is one and you can see we got three

play01:09

methods here uh there's one more thing

play01:10

let's say we have one more interface

play01:12

which is called uh interface y now the

play01:15

thing is when you talk about class and

play01:17

Class A Class can extend another class

play01:19

right what about interface but can we do

play01:22

interface and interface what keyword we

play01:24

have to use here see whenever you have

play01:26

interface to interface we say extends so

play01:29

interface y extends X so inheritance is

play01:32

is valid here as well now in this y

play01:35

interface you will be having a method

play01:37

called run so that's the inheritance

play01:39

okay so you will get this declaration in

play01:42

the interface y as well let me show you

play01:44

if I remove this X and if I keep y even

play01:48

now we have to use one otherwise if you

play01:50

remove run from here if you you comment

play01:52

this part you can see it will give you

play01:54

error it says the type B must implement

play01:57

the United abstract method one

play02:00

okay but we are implementing y right

play02:03

it's just that y extends X so we have to

play02:06

use one

play02:07

now Point remember here is whenever you

play02:10

have a class to class we say the extends

play02:14

whenever you have class to interface

play02:17

basically a class is in implementing

play02:20

interface so we have to say implements

play02:21

and whenever you have interface to

play02:24

interface we say that extends again okay

play02:28

this is the point you have to remember

play02:30

so again I will comment uncomment this

play02:32

part

play02:33

okay now there's only one thing you have

play02:34

to remember whenever you create a

play02:36

reference of a phase and object of B

play02:39

which is a class I can only call two

play02:41

methods which is of part A if I try to

play02:44

call run with a it will not work because

play02:47

a has no idea what run method is if you

play02:51

can see in a we don't have one in fact

play02:53

what we will do is I will just remove

play02:54

this code for time in yeah and then if

play02:57

you want to implement one if you want to

play02:59

call one in that case we need to create

play03:02

the reference of X which is obj1 is

play03:06

equal to Nu B now with this obj one I

play03:09

can call one but again I can't call

play03:11

other two two other methods okay so

play03:14

that's one thing you have to remember

play03:15

okay now once we are good with this

play03:17

let's try to understand what's the need

play03:20

of the interface

Rate This

5.0 / 5 (0 votes)

Related Tags
JavaInterfacesInheritanceImplementationMethod ConflictAbstract ClassesProgrammingCode ExampleSoftware DevelopmentInterface Extends