When to use an abstract class in Java - 038

preview_player
Показать описание
In Java, an abstract class is a class definition where we don't provide all the implementation details. The class has the keyword "abstract" added to it here. We denote abstract classes in our diagrams by making the class name in italics.

We can also mark some of the methods as abstract. That means we just provide the method signature and return type, but we don't provide the Java block that implements the method. We mark the methods with the keyword "abstract". Note the method has no braces, no implementation, and just ends with a semi-colon. Again we use italics for abstract methods in our diagrams.

If we declare a class as abstract, the methods may or may not be abstract. We don't need to have any abstract methods. It would be weird, because what would be the point, but we can do it. The opposite isn't true. If we mark any method as abstract, we must mark the class as abstract.

Another note. We can’t have a method that is abstract and private. We couldn’t access it, and there’s really no point. If you’re protecting it for subclasses, you’ll want to use protected.

#java #programming #tutorial





Concepts: Java, classes, abstract classes, Compile-time safety

Social Links: Don't hesitate to contact me if you have any further questions.

Hosted by DJ Spiess

Related Videos:

What are static factory methods in Java? - 036

Free Java Course Online

Media credits: All images are owned by DJ Spiess unless listed below:

Cookies

Music
Рекомендации по теме
Комментарии
Автор

Came here to find out about JAVA, ended up discovering something far more important about bread...

SirGibbels
Автор

62 people failed their Intro to Object Oriented Programming class...

Bravo, excellent video. Using cooking recipes to explain Abstract classes is my most favorite way to describe them to the person who's interviewing me

motorheadbanger
Автор

Awesome awesome awesome. Seriously, your tutorials are the only ones that I, or any beginner programmers, can understand. I can also tell that you put a ton of effort into editing. Thank you so much!

danielchoi
Автор

This was the clearest explanation of any java concept I’ve ever seen in 4 minutes. Subscribing immediately

thomasl
Автор

I cannot believe that these great tutorials have really low number of subscribers. I am not even a Java person but I visit these videos to learn basic concepts. Great. Thanks.

jasper
Автор

Thoses videos are great, they are dynamic and slow enough to understand

colletteso-noon
Автор

Didn't know Mike from Breaking Bad did Java tutorials...

fallartifact
Автор

Many thanks! Stay healthy, stay happy, Teacher!

FarizDarari
Автор

Very well explained in less than 5 minutes. Thanks!

lucasmelo
Автор

What an absolutely awesome tutorial.
Deege U does it again!

Ali-miup
Автор

nice explanation, also the song is great

palkia
Автор

we can implement the methods using interface as well? I just wanted to know why abstract class should be chosen if we have interface?

guruprasad
Автор

I kind of like when he stumbles through a word every now and then, but then he just keeps going. Shows confidence.

MR-ycmy
Автор

Thank you so much!! I have gone through a lot of videos but i was really confused, now I finally understand. 😊

ayushimathur
Автор

Thank you for your video!

I have a question. I want a simple employee hierarchy where testers, developers and designers, among others in the future, are all employees, and all employees have unique IDs and a state (meeting, working, break, etc...), among others, which could be modelled as abstract class with protected attributes and some methods, abstract and otherwise. In this case, it is better to go with abstract classes instead of interfaces since we need a state, that is going to be shared by all possible subclass instances of an employee; and there is no such a thing as a generic employee, which goes hand in hand with not being able to instantiate an employee directly.

I'd appreciate any thoughts on this.

Thanks!

HernisQ
Автор

My teacher doesnt teach us why we use certain things in Java so this is very useful, thank you for the simply and clear explanation!

tobicipher
Автор

Finally an explanation that made sense 👍

michaelsteinberg
Автор

THIS WAS AN AMAZING EXPLANATION. Thanks

shaun
Автор

I completely agree with comment below. Thanks for all this video.

ihorsolntsev
Автор

Can you please explain interfaces in more detail? I have seen your youtube video on Interfaces but I am not able to understand when to use Interfaces.

jasper