From the course: Advanced Java Programming
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
The substitution principle in Java - Java Tutorial
From the course: Advanced Java Programming
The substitution principle in Java
- [Instructor] The substitution principle, which is also known as the Liskov Substitution Principle is an important concept in object-oriented programming because it allows you to write maintainable and reusable code. The name makes it sound like it's going to be a complicated concept, but it is actually very simple. It just means that if you have a variable of a given type you can assign it to a value that is a subtype of that type. In this example I have a simple class called Building which simply overwrites the toString method to print out that it is a Building. I then have another simple class called Office which extends the Building class and overrides the toString method again to print out that this is an office. I then have a Main class. In the main method I have created a building variable and assigned it to a new Building object. I have then created a second building object. But instead of assigning it to a building, I have assigned it to a new Office object. In this class I…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.