Interfaces vs Abstract Classes

Raw Coding
25 Oct 202314:42

Summary

TLDRThis video script delves into the nuances of abstract classes and interfaces, contrasting their syntactical differences and use cases. It highlights that while both cannot be instantiated and can contain static members, they serve different purposes in code design. Abstract classes encapsulate commonality among related entities, whereas interfaces decouple components, enhancing flexibility. The script uses analogies like shapes and vehicles to illustrate concepts and discusses scenarios where one might be favored over the other, emphasizing the trade-offs between inheritance and composition in structuring code for maintainability and flexibility.

Takeaways

  • 📜 Abstract classes and interfaces are common interview topics and are used to encapsulate common behaviors or roles in code.
  • 🔍 The syntactical difference lies in that interfaces can only have public methods and properties, while abstract classes allow for various access modifiers and constructors.
  • 🚫 Neither abstract classes nor interfaces can be instantiated directly.
  • 🔗 A class can implement multiple interfaces but can only inherit from one abstract class due to the concept of identity.
  • 🎯 Abstract classes are used when there is a commonality among classes, while interfaces are chosen for their role in decoupling components and increasing flexibility.
  • 📈 Interfaces can provide scope control and enable immutability, whereas abstract classes cannot offer this level of control.
  • 🏗️ Traits or roles, a programming paradigm, can be implemented using interfaces but not abstract classes, as a class can play multiple roles through interfaces.
  • 🔄 In scenarios where additional responsibilities are anticipated, abstract classes might be more suitable than interfaces.
  • 🔧 Inheritance leads to succinct code but may limit reusability, whereas composition with interfaces allows for greater flexibility but can increase code volume.
  • 🔎 The choice between inheritance and composition depends on the specific needs of the project, with composition generally preferred for its flexibility.
  • 💡 Understanding the differences and appropriate use cases for abstract classes and interfaces is crucial for writing maintainable and flexible code.

Q & A

  • What is the primary purpose of discussing abstract classes and interfaces in the context of the video?

    -The primary purpose is to clarify the syntactical differences, use cases, and to provide examples of when to use abstract classes or interfaces in programming, especially in the context of interviews and software design decisions.

  • What are some key syntactical differences between abstract classes and interfaces?

    -Abstract classes can have access modifiers, default implementation methods, fields, and constructors. Interfaces, on the other hand, can only have public methods, default implementation methods (but not consumed within the implementing class), and properties (but without fields).

  • Why might you choose to use an interface over an abstract class in a particular scenario?

    -You might choose an interface when you want to introduce more flexibility into your code by decoupling components, allowing for multiple roles or behaviors to be played by a class, and when you need to maintain a contract without enforcing a specific implementation.

  • How do abstract classes and interfaces handle the concept of immutability and state management?

    -Interfaces can be used to enforce immutability by defining properties that can only be initialized and not reinitialized, effectively controlling the state of an object. Abstract classes do not inherently support this pattern without additional implementation.

  • What is the significance of the shape and vehicle analogies in understanding abstract classes?

    -The analogies help to illustrate that a class can represent a general concept (like a shape or a vehicle) and share common properties or behaviors with other classes within that concept, which is the essence of using an abstract class to encapsulate commonality.

  • How can you explain the use of default implementation methods in abstract classes versus interfaces?

    -In abstract classes, default implementation methods can be consumed within the inheriting class, whereas in interfaces, the default implementation cannot be directly accessed by the implementing class; instead, the class must provide its own implementation or use the default implementation from the interface.

  • What is the main advantage of using composition with interfaces over inheritance with abstract classes?

    -Composition with interfaces provides greater flexibility, as it allows for the decoupling of components and the ability to mix and match behaviors in different contexts. It enables reusability of interfaces across various parts of an application, whereas inheritance may lead to tightly coupled and less flexible code structures.

  • Why might you prefer an abstract class in a scenario where multiple context-specific behaviors are needed?

    -In such scenarios, abstract classes can be used to define a common structure or behavior that is shared across different contexts, making it easier to manage and reuse the shared logic without having to implement multiple interfaces for each context-specific behavior.

  • What are the trade-offs between using inheritance and composition in software design?

    -Inheritance provides a succinct way to express related concepts and share common logic, but it can lead to tightly coupled code that is difficult to refactor. Composition, on the other hand, offers more flexibility and decoupling but may require more code and explicit orchestration of components.

  • How does the video script illustrate the concept of 'scopes' in relation to interfaces?

    -The concept of 'scopes' is illustrated by showing how interfaces can define a set of behaviors or properties that are accessible only within a certain scope or state of an object, providing a structured way to control access and maintain immutability.

  • What is the role of traits or roles in object-oriented programming, and how do interfaces support this concept?

    -Traits or roles represent specific behaviors or functionalities that an object might adopt within certain contexts. Interfaces support this concept by allowing a class to implement multiple interfaces, effectively playing multiple roles, which is not possible with abstract classes that enforce a single role or identity.

Outlines

00:00

📚 Introduction to Abstract Classes and Interfaces

This paragraph introduces the topic of abstract classes and interfaces, highlighting their importance in software development interviews. It emphasizes the need to understand the syntactical differences between the two and their use cases. The speaker shares personal experience of asking this question in interviews and suggests that even those who feel confident may struggle to articulate the nuances. The paragraph sets the stage for a detailed discussion on when and why to use abstract classes or interfaces, and it invites viewers to engage by liking, subscribing, and commenting.

05:01

🔍 Exploring Analogies and Use Cases

The second paragraph delves into analogies and practical examples to better understand abstract classes and interfaces. It uses shapes and vehicles to illustrate the concepts and contrasts them with programming realities. The speaker discusses the limitations and capabilities of both abstract classes and interfaces, such as the inability to instantiate them and their potential to contain static members. The paragraph also touches on the idea of implementing multiple interfaces versus inheriting from a single abstract class, and it provides examples of scenarios where one might be preferred over the other.

10:01

🤔 Decision-Making Between Abstract Classes and Interfaces

This paragraph focuses on the decision-making process when choosing between using an abstract class or an interface. It presents a scenario where both can be used interchangeably but advises against using abstract classes when additional responsibilities can be attached to the operation. The speaker introduces the concept of 'Scopes' and 'traits' or 'roles', explaining how interfaces enable these programming paradigms, which abstract classes cannot support. The paragraph concludes with a discussion on inheritance versus composition, emphasizing the flexibility and potential code volume trade-off when using interfaces over abstract classes.

Mindmap

Keywords

💡Abstract Class

An abstract class is a foundational class in object-oriented programming (OOP) that cannot be instantiated on its own but can serve as a base for other classes. It allows for the definition of both abstract methods (which must be implemented by subclasses) and concrete methods (which can provide default behavior). In the context of the video, abstract classes are depicted as a way to capture commonalities among different entities, suggesting that they are used to extract and encapsulate shared attributes or behaviors, which subclasses can then inherit and specialize.

💡Interface

An interface in OOP defines a contract or a blueprint of methods that classes must implement, without specifying how these methods should be implemented. Interfaces are used to ensure a certain set of functionalities across different classes. The video highlights interfaces as tools for decoupling components in code, facilitating the substitution of dependencies and increasing flexibility. By using interfaces, developers can define roles or behaviors that classes can adopt, without prescribing the exact way these behaviors should be executed.

💡Syntactical Difference

Syntactical differences refer to the distinct rules and structures used when defining abstract classes and interfaces. The video mentions, for example, that interfaces cannot have access modifiers for their methods (they are implicitly public), and do not support the declaration of fields, while abstract classes allow for more versatility, including the specification of access modifiers, fields, and constructors. These syntactical distinctions guide programmers on how to properly use each construct within their code.

💡Dependency Breaking

Dependency breaking is a technique highlighted in the video where interfaces are used to decouple components of the software, reducing direct dependencies between different parts of the code. This increases the code's flexibility and maintainability, as changes to one component do not necessarily require changes to the dependent components. By defining an interface, one can substitute the implementation behind the interface without altering the code that relies on it.

💡Composition vs. Inheritance

The video discusses the debate between using composition (achieved through interfaces) versus inheritance (through abstract classes or base classes) to extend or enhance the functionality of objects in OOP. Composition is favored for its flexibility and the ease with which it allows objects to be constructed from smaller, reusable components. In contrast, inheritance provides a straightforward means of sharing behavior and structure among related classes, but can lead to rigid and fragile code structures. The video illustrates both approaches, underscoring the context in which each is most beneficial.

💡Default Implementation

Default implementations refer to methods within an interface or an abstract class that are given a concrete implementation. This allows classes implementing the interface or inheriting from the abstract class to use these methods without the need for their own implementation. The video explains that while both abstract classes and interfaces can provide default implementations, the way they are consumed differs: default methods in interfaces cannot be accessed directly by the implementing class without referring to the interface.

💡Commonality Extraction

Commonality extraction is a concept described in the video as the process of identifying shared attributes or behaviors among a set of entities and encapsulating them into an abstract class. This technique allows for the creation of a more organized and modular code structure, where shared logic is centralized, reducing redundancy and facilitating maintenance. The video uses the example of shapes to illustrate how different types, like squares and circles, might share common properties or methods, which are then abstracted into a superclass.

💡Contract

In the context of the video, a contract refers to a set of defined methods in an interface that a class agrees to implement. This concept is crucial for understanding how interfaces are used to specify a minimum set of behaviors that must be provided by any class that implements the interface. The idea of a contract emphasizes the role of interfaces in ensuring a consistent API across different implementations, allowing for interchangeability and flexibility in the use of different concrete classes.

💡Decoupling

Decoupling is a principle in software engineering aimed at reducing dependencies between different parts of a system, thereby making it easier to change, maintain, and extend. The video illustrates how interfaces can be used to achieve decoupling by serving as a layer of abstraction between components. This allows for parts of the system to interact through well-defined interfaces rather than direct references, enhancing modularity and the ability to substitute components without affecting others.

💡Identity vs. Behavior

The distinction between identity and behavior is discussed in the video as a key factor in deciding whether to use an abstract class or an interface. Identity refers to what an object is, captured by inheritance and the 'is-a' relationship, suggesting a more static and hierarchical relationship. Behavior, on the other hand, focuses on what an object can do, represented by interfaces and the 'has-a' or 'can-do' relationship, which is more dynamic and flexible. This distinction helps guide the choice between abstract classes and interfaces based on whether the focus is on an object's inherent identity or its capabilities.

Highlights

The video discusses the differences between abstract classes and interfaces, a popular interview question.

Abstract classes and interfaces are syntactically different, with interfaces being more restrictive in terms of access modifiers.

Interfaces can only have public methods and cannot have access modifiers specified, unlike abstract classes.

Abstract classes can have fields, constructors, and any access modifier methods, while interfaces cannot.

Both abstract classes and interfaces cannot be instantiated and can contain static members.

A class can implement multiple interfaces but can only inherit from one abstract class due to the concept of identity.

Abstract classes are used when there is a commonality among classes, while interfaces are used to break dependencies and increase flexibility.

Examples of using abstract classes include shapes like squares and circles, while interfaces can be used for vehicles or animals.

In web development, abstract classes are less commonly used compared to interfaces.

Abstract classes can be used to create a structure where operations share commonalities, like in the example of a stopwatch timing operations.

Interfaces can be used for scope control, allowing for immutability and a structured way to use code.

Traits or roles, a programming paradigm, can only be implemented with interfaces, not abstract classes.

The debate between inheritance (using abstract classes) and composition (using interfaces) is discussed, with a preference for composition in most cases.

Inheritance provides succinct code but may lead to tightly coupled components, while composition offers flexibility at the cost of increased complexity.

The video concludes with a recommendation to use interfaces and composition for their flexibility, unless a clear commonality among classes justifies an abstract class.

The presenter invites viewers to like, subscribe, and leave comments or questions for further discussion.

The source code for the examples in the video is available for patrons on Patreon.

Transcripts

play00:00

welcome back boys and girls today we're

play00:01

going to be talking about abstract

play00:02

classes and interfaces it is a very

play00:05

popular interview question and I

play00:07

personally liked asking it myself as

play00:09

well the interview question goes like

play00:11

this you first ask to explain the

play00:13

difference between an abstract class and

play00:15

an interface so what is the syntactical

play00:17

difference how do you use it why would

play00:19

you use one over the other one and

play00:22

perhaps can you give me some examples as

play00:24

you go through the explanation of these

play00:26

questions there are a lot of points

play00:27

where you can ask the candidate to drill

play00:29

down into their EXP ation and at some

play00:31

points people just run out of things to

play00:34

say or they just don't know what to say

play00:36

even now if you're sitting there and

play00:37

you're thinking I I know everything

play00:38

about abstract classes and interfaces if

play00:40

I actually start interviewing you about

play00:43

it you may struggle to explain some

play00:45

points so if you ever struggled with

play00:47

articulating your thoughts on abstract

play00:50

classes and interfaces the differences

play00:52

between the two and why would you use

play00:53

one over the other one hopefully this

play00:56

video can help you don't forget if

play00:58

you're enjoying the video leave a like

play00:59

And subscribe if you if you have any

play01:00

questions make sure to leave them in the

play01:01

comment section if you think there is

play01:03

anything that I have missed go ahead and

play01:04

leave it in the comment section as well

play01:06

I have a C Core status out if you would

play01:08

like to know c as I did highly recommend

play01:10

you take a look at it before we actually

play01:12

start looking at the code I want to

play01:14

outline a general idea for abstract

play01:16

classes and interfaces so you can get a

play01:19

feeling for why would you want to use

play01:21

one over the other one and perhaps an

play01:24

idea of their shape so for abstract

play01:26

classes there is a set of things there

play01:28

is then a commonality amongst all of

play01:30

those things and you're taking that

play01:32

commonality and extracting it into an

play01:34

abstract class with interfaces you have

play01:37

two parts of your code to components

play01:39

where one depends on the other one and

play01:41

you want to break this dependency

play01:44

because you want to substitute the thing

play01:46

that it's depending on or the thing that

play01:47

it's consuming essentially introducing

play01:50

more flexibility into your code this is

play01:52

where the contract the bridge the

play01:55

interface comes in ultimately used to

play01:57

decouple your components in code with

play02:00

this let's go ahead and get started here

play02:03

I have a bunch of files we're going to

play02:05

go through them one by one the first is

play02:07

the abstract class over here and then

play02:08

the interfaces we're going to take a

play02:11

look at the difference between the two

play02:13

syntactically first interfaces are a

play02:16

little bit smaller so the first thing

play02:17

that we can take a look at is you cannot

play02:20

actually have access modifiers or you

play02:23

cannot put the ones that you want you

play02:25

can only put public you can make them

play02:26

explicit but you can't actually specify

play02:29

them with abstract classes you can put

play02:31

whatever access modifier that you want

play02:34

and the only thing that you can Define

play02:36

on interfaces are methods either

play02:39

abstract methods or just method

play02:40

definitions which are the same on the

play02:42

abstract class which are abstract

play02:44

methods over here and then default

play02:46

implementation methods you can also have

play02:49

them on the abstract class however there

play02:51

is a slight difference between the two

play02:53

so a default implementation method on an

play02:55

abstract class can be consumed within

play02:57

the inheriting class so s string can be

play03:00

used over here and then if I instantiate

play03:02

a square I can also consume that

play03:05

function on there if I'm using an

play03:07

interface the space which is

play03:09

implementing the interface I cannot

play03:12

reach the default implementation on here

play03:14

I will actually have to go to the

play03:16

interface itself so here I will be able

play03:19

to reach this function okay now you can

play03:22

also specify properties on an interface

play03:24

however that is only going to be

play03:26

concerned for the methods of the

play03:29

property because a property is three

play03:31

parts two methods getter and Setter

play03:33

depending on which ones you specify and

play03:35

also a field with an abstract class you

play03:37

can actually get Fields if you specify a

play03:40

property on an interface you don't

play03:42

actually get a field and then another

play03:44

thing that an abstract class can have

play03:45

that an interface cannot have is a

play03:48

Constructor and that is pretty much it

play03:50

for the differences between the two you

play03:52

can also talk about commonalities

play03:54

between the two how are abstract classes

play03:55

and interfaces similar neither of them

play03:58

can be instantiated they both both can

play04:00

still contain static members and that's

play04:02

about it now moving on to how you would

play04:04

actually use an abstract class versus an

play04:06

interface you implement an interface and

play04:09

you inherit from an abstract class you

play04:11

can Implement multiple interfaces on a

play04:14

single class and then you can only

play04:16

inherit from one abstract class the

play04:19

reason you can only inherit from one is

play04:22

because there is a notion of identity a

play04:25

square is a shape a square cannot be an

play04:28

animal and shape a square at that point

play04:31

has schizophrenia so when we're talking

play04:33

about implementing an interface it is

play04:35

more do I have this Behavior am I

play04:38

playing this role these are partly the

play04:40

ideas that you can figure out when

play04:42

you're thinking or you're talking about

play04:44

implementing some kind of feature is it

play04:46

a thing or do we have this Behavior May

play04:49

sway you in one direction or the other

play04:52

whether you should be choosing an

play04:53

abstract class or an interface so that's

play04:56

about it for the difference between the

play04:58

two if you're wondering about the

play04:59

example that I have over here it's a

play05:01

shape example a square is a shape a

play05:03

circle is a shape you can then have

play05:04

triangles you can have other examples

play05:07

such as Vehicles animals so truck

play05:11

motorcycle car those are vehicles for

play05:14

Animals you have red cat Etc these are

play05:17

very good analogies to understand

play05:19

abstract classes however when you

play05:20

actually get to programming uh the

play05:23

concepts vary slightly and generally in

play05:25

web development you don't really get

play05:27

that many opportunities to create

play05:29

abstract classes like that although

play05:30

there are places like video games where

play05:33

if you have some kind of objects on the

play05:35

plane you get a lot of options to

play05:37

basically say ah I should have this

play05:39

abstract class where all of the objects

play05:41

have some kind of position in the game

play05:43

for the interfaces I have an example of

play05:45

a cache so if for example I have my

play05:48

application using cash everywhere if I

play05:50

want to substitute a cash at

play05:52

implementation at any point going from

play05:55

redis to M cach or to some kind of other

play05:58

cache that is going to come out in the

play05:59

in the future I don't have to change the

play06:01

whole application to depend on a

play06:02

different service I just substitute the

play06:05

implementation for the interface and and

play06:07

my application should just keep working

play06:09

as it does why would you actually use an

play06:12

abstract class over an interface or an

play06:14

interface over an abstract class in some

play06:16

cases there really is no difference

play06:19

whether you use one or the other one and

play06:21

Abstract classes can behave like

play06:23

interfaces and this is what this example

play06:24

is about I'm going to tell you what to

play06:26

do in this situation for example we have

play06:28

something that depicts an operation this

play06:31

is really to Mark functions okay we have

play06:34

an operation that is going to increment

play06:37

we then have an I try so we try to

play06:40

execute an operation and the operation

play06:42

that we depend on is this interface we

play06:45

then have an night time again we start

play06:48

up a stop stopwatch and we try to time

play06:51

the time it takes to execute an

play06:53

operation and again we depend on this

play06:54

eye operation we then have the

play06:56

individual permutations where we compose

play06:58

things together now with abstract

play07:01

classes you can effectively arrive at

play07:03

the same structure where you have your

play07:07

abstract operation abstract try uh

play07:10

really the same implementation abstract

play07:12

time again the same implementation and

play07:14

then abstract operation and the same

play07:17

permutation so really no difference

play07:20

between the two what should you be using

play07:22

in this situation you shouldn't be using

play07:24

abstract classes because at that point

play07:27

you can actually bolt on more

play07:29

responsibilities to this operation

play07:31

rather than just being used in this

play07:33

mechanism if all operations are sharing

play07:36

some kind of commonality between all of

play07:39

them which is not present over here you

play07:41

should be reaching for abstract classes

play07:44

and again to reiterate in this example

play07:46

we don't have that commonality so

play07:47

there's no reason to use an abstract

play07:49

class unless you're actually foreseeing

play07:51

that commonality coming in the future

play07:53

then you want to opt in for an abstract

play07:55

class now for favoring interfaces there

play07:58

are just some things that you you can do

play07:59

with interfaces that you cannot do with

play08:01

abstract classes and here we have the

play08:04

first example I am naming it Scopes

play08:07

because you can effectively control how

play08:10

much of the object you want to access

play08:13

using an interface so for example we

play08:16

have states that you can initialize and

play08:18

that after initialization it is ready to

play08:21

be read so we accept some kind of state

play08:23

we initialize it and then it's ready to

play08:25

be consumed where we cannot change it we

play08:27

cannot reinitialize it again effectively

play08:30

giving us immutability and a way a

play08:33

structured way to use our code so we

play08:36

can't actually make mistakes another

play08:39

things that interfaces can be used for

play08:41

and it's very rare in the C world but a

play08:44

concept called traits or roles where you

play08:47

have some kind of context where the

play08:49

functionality only belongs to that

play08:51

context a class that can be used inside

play08:55

that context implements this interface

play08:57

effectively enabling this programming

play09:00

Paradigm which you cannot do with

play09:02

abstract classes because if you have

play09:03

many context many roles and a class can

play09:06

play many roles you should be able to

play09:08

implement multiple interfaces if these

play09:10

are abstract classes you can only play

play09:12

one role which defeats the whole purpose

play09:14

of this Paradigm so interfaces can only

play09:17

be used in these scenarios and by the

play09:19

way for Scopes you can get away with

play09:21

using classes for it but you're going to

play09:22

have to create a new class with its own

play09:25

individual functionality and if you have

play09:27

shared States between those classes and

play09:29

you have to constantly transition it it

play09:31

just becomes more code volume rather

play09:33

than bolting on all of those Scopes onto

play09:36

one class okay and now for the big

play09:38

debate of inheritance versus composition

play09:41

inheritance meaning you are trying to

play09:43

use classes and Abstract classes and

play09:44

inheritance in order to extend your

play09:47

application or extend Behavior or add

play09:49

more Behavior to your application and

play09:51

then composition is for interfaces where

play09:54

you're decoupling everything with

play09:56

interfaces and then composing it

play09:58

together it's uh not really a big debate

play10:01

everybody will generally tell you prefer

play10:03

composition over inheritance and I

play10:05

generally agree however in this example

play10:08

we are implementing the same thing in

play10:10

two different ways using abstract

play10:12

classes and interfaces and you're going

play10:14

to see that okay

play10:17

inheritance has its place with some

play10:21

benefits however composition also has

play10:23

its benefits and we're going to see what

play10:25

they are in just a second here we have

play10:27

an example we want to send an in

play10:29

interface there is some kind of

play10:30

commonality and abstraction between all

play10:32

of the notifications that we can send

play10:34

where we check should this notification

play10:36

be sent and should we execute it these

play10:39

are the two main abstractions we have a

play10:42

check and we have an execute when we

play10:44

want to alert an admin this is what the

play10:46

this is the condition that we're

play10:47

checking and this is the actual

play10:49

execution Logic for how we're sending a

play10:52

notification then we have a notify sweet

play10:55

spot so again condition and then

play10:58

actually sending the the notification

play11:00

currently these two implementations are

play11:02

the same however imagine that we're just

play11:04

getting different users that we want to

play11:05

notify the notification itself is a

play11:08

slightly different slightly different

play11:10

text subject are we sending to mobile

play11:13

email right so this can vary quite a bit

play11:16

but understand that what we have here is

play11:18

we have a coupling between a condition

play11:22

and the actual Logic for executing a

play11:25

notification to this notification

play11:28

abstract class into this process over

play11:30

here and this yields us a very succinct

play11:33

way of expressing what notifications we

play11:36

have and for what reasons and to where

play11:39

and how they're being sent the downsides

play11:41

here is that this logic cannot be reused

play11:44

in other places now if there are no

play11:47

other places to use it in this is 100%

play11:51

fine this is preferable if this logic

play11:54

should be used in other places you go

play11:57

with interfaces your de coupling

play12:00

notifications from the actual way that

play12:02

you're sending notification and you

play12:05

decoupling it from how you're actually

play12:07

going to perform the check you create

play12:09

interfaces for it so you have some kind

play12:11

of constraint and you have some kind of

play12:13

notification that you want to perform

play12:15

and then to replace that abstract class

play12:18

we actually have a class where we're

play12:19

composing the interfaces together and

play12:21

then we're performing The same logic and

play12:24

then the individual implementations

play12:26

here's the admin constraint The Sweet

play12:28

Spot constra straint and then the actual

play12:30

notification constraint these can now be

play12:32

surfaced in any other place in your

play12:34

application and where you're actually

play12:37

using them at those points you actually

play12:39

have to compose this feature together so

play12:41

the second option a lot more flexible

play12:44

but also a little bit more verbose and

play12:47

that is the main trade-off that you have

play12:49

between interfaces and Abstract classes

play12:52

and inheritance and composition

play12:54

inheritance when used correctly you have

play12:56

very succinct code which can only really

play12:59

be present in one place and you don't

play13:01

have to wonder about if it's being used

play13:03

in another place these are really

play13:05

isolated units where these notifications

play13:08

are notifications it's not a thing that

play13:10

behaves like a notification that can be

play13:12

placed anywhere within your application

play13:14

where with interfaces the reason people

play13:16

will just say just prefer composition

play13:18

because then you don't have to make this

play13:20

distinction between H is this a thing

play13:22

that doesn't need to be used anywhere

play13:24

else you just Define your interfaces

play13:27

everything is decoupled and then you

play13:28

have to explicitly start defining places

play13:31

where they compose and then you actually

play13:33

have to do the composition or

play13:35

orchestration at some point by going

play13:37

default composition you're paying the

play13:38

price of code volume and complexity you

play13:41

have a lot more components however that

play13:43

is going to give you flexibility over

play13:45

implementations with inheritance you

play13:47

generally get a very succinct

play13:49

implementation where the logic is very

play13:52

closely collocated and depends on each

play13:54

other so if the structure is correct

play13:56

it's very hard to mess it up up the

play13:59

potential price that you need to pay

play14:00

with inheritance is that abstract class

play14:02

that you have extracted if that

play14:05

abstraction has to be used in other

play14:07

places other than these set of classes

play14:10

that you extracted the common the

play14:12

commonality from then you're in trouble

play14:14

then you will need to go ahead and

play14:15

refactor your code into it being an

play14:17

interface and start defining these Place

play14:19

places of composition and this will be

play14:21

it for this video thank you very much

play14:23

for watching hopefully you have found it

play14:24

useful if you did don't forget to leave

play14:26

a like And subscribe if you have any

play14:28

questions again in the comment section

play14:29

if you would like the source code for

play14:31

this video as well as my other videos

play14:32

please come support me on patreon I will

play14:34

really appreciate it and a big and

play14:35

special thank you goes out to all of my

play14:37

Patron supporters you help me make these

play14:39

videos as always thank you for watching

play14:41

and have a good day

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
CodingInterviewAbstractClassesInterfacesSoftwareDesignInheritanceCompositionProgrammingBestPracticesCodeFlexibilityCodeSuccinctnessOOPConcepts
هل تحتاج إلى تلخيص باللغة الإنجليزية؟