The course is part of this learning path
In this course, we'll learn about object-oriented programming and its implementations.
- Method Overriding
- Super and This Keywords
- Final Keyword
- Abstract Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the sealed class. Actually, the sealed class is not the topic of the Oracle Certified Associate Java SE8 programmer exam. Because the release of Java SE15 introduces sealed classes as a preview feature. But since it's related to inheritance, I want to touch on the sealed class. This feature is about enabling more fine-grained inheritance control in Java. Sealing allows classes and interfaces to define their permitted subtypes. In other words, a class or an interface can now define which classes can implement or extend. It's a useful feature for domain modeling and increasing the security of libraries. One of the primary purposes of inheritance is code reuse. When you want to create a new class and there's already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write and debug them yourself. However, what if you want to model the various possibilities that exist in a domain
by defining its entities and determining how these entities should relate to each other. For example, you're working on a graphics library. You want to determine how your library should handle common geometric primitives, like circles and squares. You've created a Shape class that these geometric primitives can extend. However, you're not interested in allowing any arbitrary class to extend Shape. You don't want clients of your library declaring any further primitives. By sealing a class, you can specify which classes are permitted to extend it, and prevent any other arbitrary class from doing so. Now, let's look at defining the sealed classes. To seal a class, add he sealed modifier to its declaration.
Then, after any extends and implements clauses, add the permits clause. This clause specifies the classes that may extend the sealed class. If we examine the example on the slide, the declaration of shape specifies three permitted sub-classes; Circle, Square and Rectangle. In this case, only the Circle, Square and Rectangle classes can inherit from the Shape class. Other than these three classes, other classes cannot inherit from the Shape class. Yes friends, we talked theoretically about the sealed class. I think you understand what the sealed classes are used for, but we're not going to do any practice with this class, because as I mentioned at the beginning of the lesson, these are outside our topics. But when talking about inheritance, I didn't want to go without mentioning sealed classes. Now, let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.