The course is part of this learning path
In this course, we'll learn about object-oriented programming and its implementations.
Learning Objectives
- Inheritance
- Method Overriding
- Super and This Keywords
- Final Keyword
- Abstract Classes
- Interfaces
- Polymorphism
- Encapsulation
Intended Audience
- 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
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In the previous video, we talked about the inheritance concept. In this video, we'll do some practice about inheritance. First, let's remember again the inheritance through another example. We have a Superclass vehicle. All vehicles have a type, model, and maxSpeed. Also, all vehicles can start and stop, so this is the base class. Also, we have two classes named Car and Motorcycle. Car and Motorcycle classes are subclasses of the Vehicle class. We use the extends keyword by the subclass to inherit the features of the superclass. So, the car and motorcycle classes inherit variables and methods from the vehicle class. There is no need to declare these variables or methods again for car and motorcycle classes.
Car class has its own variable named isAutomatic, and motorcycle class has its own variable, named cylinderVolume. Let's move on to the Eclipse and do some practice. Let's start to write a program that prints the properties of subclasses by using inheritance. First, I will create a Superclass. For this, in the ObjectOriented programming project, right click on the source folder and select, "New" class. I will specify the package name as inheritance, and the class name can be vehicle. This class will have some fields and a method, and using the inheritance, we will reuse these properties in other classes we create. So, the vehicle class will be superclass. Let's declare four instance variables with four access modifiers in the vehicle class. First, protected String model. Second, private String type. Third, String color. Fourth, public int maxSpeed.
Now, let's declare a void type method print to display values of type, model, color, and maxSpeed variables, public void print(). In this method, we will use the println method, S.out.printIn(''type'' + type) and I'll copy this line and paste it three times. The second print method will print the model. The third print method will print the color, and the last print method will print the maxSpeed. Finally, I'll save the code. Now, let's start to declare subclasses. I'll right click on the inheritance package and select "New" class. I'll specify the class name as Car and I click the 'Finish' button. This class will be a subclass and it will inherit from the vehicle class we created. In order to implement inheritance, we have to add the extends keyword after the class name. Now, our subclass Car extends superclass Vehicle. This means that subclass Car inherits the print method in variables from superclass Vehicle. Currently, the Car class is empty, but we can also add some fields to this class because this class can have its own properties. For example, let's add a Boolean variable, Boolean isAutomatic and let's print this on the console with the help of a method, public void printGear(). And I'll create a println method,
S.out.printIn(''isAutomatic = '' +) and after the plus sign I write, isAutomatic variable. Now, let's save the code again. Don't forget to save the code. Let's create another subclass. Right click on the inheritance package and select "New" class. I will specify the class name as Motorcycle. This class will also be a subclass and it will inherit from the vehicle class we created. In order to implement the inheritance, we have to add the extends keyword after the class name. Now, our subclass Motorcycle extends superclass vehicle. This means that subclass Motorcycle inherits the print method in variables from the superclass Vehicle. Currently, the Motorcycle class is empty, but we can also add some fields to this class because this class can have its own properties.
For example, let's add a string variable, string cylinderVolume. And let's print this on the console with the help of a method, public void, printVolume. And I'll create a println method, S.out.printIn(''cylinder Volume = '' +) and after the plus sign, I write, the cylinderVolume variable. Now let's save the code again. Don't forget to save the code. Let's create a new class for the test of inheritance. Right click on the inheritance package and select "New" class. I'll specify the class name as InheritanceTest and select the checkbox to add the main method. First, let's start to create a new car object in the main method, Car myCar = new Car. If you write myCar. object and dot operator, then you will see the variables, maxSpeed, color, model, and print method coming from superclass Vehicle.
But if you notice, we cannot see the type variable because the access modifier of the type variable is private and the private fields cannot be inherited. We can also see the isAutomatic variable and printGear() method of the Car class itself. Now, let's continue. First, I will specify the value of the isAutomatic variable; car.isAutomatic = true, and let's print it with the printGear()method, Car.printGear(). Secondly, let's specify the model; car.model. This property comes from the Vehicle class and its model can be Ferrari. Also in the next line, I write, car.color, the color can be red. And let's specify the maxSpeed. MaxSpeed can be 300, and last, let's call the print method from the superclass Vehicle. Let's run the code. You see the results in the console, isAutomatic is ''true.'' The model is Ferrari, the color is red and maxSpeed is 300. Except for the isAutomatic, all of these properties are inherited from superclass Vehicle. Let's start to create a new motorcycle object.
Motorcycle myMotorcycle = new Motorcycle. And I write, myMotorcycle. After the dot, you can see the same properties and method coming from the superclass Vehicle. And again, we cannot inherit the type property because of it is private. Now, let's specify the properties. The model of the motorcycle can be the Kawasaki, the color of the motorcycle can be blue, and the maxSpeed can be 200. And let's call the print method from superclass Vehicle. Also, I'll specify the cylinder volume. The cylinder volume can be 900cc and I'll call the printVolume() method. The cylinderVolume variable and the printVolume() method are elements of the Motorcycle subclass. Let's run the code. You see the results in the console. The model is Kawasaki, the color is blue, the maxSpeed is 200, cylinder volume is 900 cc. I think you understand the concept of inheritance a little better now. Let's take a little 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.