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
Yes, this question is about the switch case statement. A variable of type int is given, and this variable is controlled by the switch case. The question is, what will be the output of this code?
Pause the video and try to solve it yourself. I'll be sharing the answer with you on the next slide. Yes, your answer is correct if you chose option C. If you remember, only constant expressions were allowed as the case parameter in the switch case structure. If variables a and b were not defined as final, we would get a compilation error on line 7 and line 8, but once a variable is declared as final, it's now constant. Therefore, we can use a and b in the cases here. The variable we're checking in the switch parentheses is c, and the value of c is nine. But since there is no such value in cases, the code in the default statement will work and 'Hello' will be printed to the console. In addition, since the break keyword is not used in default in case 1, the program will continue to progress and the statements in case 1 and case 2 will be printed to the console. Therefore, the statement 'Hello Java Developers' is printed on the console screen.
The answer is option C. Let's move on to the next question. Yes, this question is about the while loop, but its outcome depends on the use of the final keyword. A variable of type boolean is declared and this variable is defined as final. Then this variable is initialized with the false value. Finally, some messages were trying to be printed to the console by using the while loop. The question is, what will be the output of the program? Pause the video here and try to find the answer yourself. I'll be giving the answer on the next slide. Yes, if you selected option B, your answer is correct. Because it's an infinite loop and writes 'Hello' to the console forever. Let's explain this now. Notice that using the final keyword, the boolean variable a is declared first and initialized in the next line.
If we had defined this variable as an instance of the class, not in the main method, we would not be able to write it this way, because we need to initialize the final variables, but when we define it this way it becomes a local variable and we do not get any error. Another point is, that if we set the value of the variable a here at the declare stage, that is, at line 4, then the second print method would not be reachable because the value of variable a will always be false and cannot be changed again. Therefore, the program will never be able to exit the while loop and the second print method will never be reached.
However, since the declaration is made first, and then the initialization is done, such a situation would not be the case and the program will work successfully. Since the local complement operator is used in the while condition, this condition will be true and the while loop will run forever. So, the 'Hello' statement will be printed on the console forever. The second print method is outside of the while loop. Because no space is specified with curly braces, only the first print method is accepted inside the loop. The other method is outside the loop. As a result, this code is compiled and 'Hello' is printed to the console forever. Let's move on to the next question. Yes, this question is about inheritance. Two classes are given in the same package, the class name Car inherits from the class named Vehicle. Also, the constructor is defined in both classes.
And in the class named Car, an object from the Vehicle class was created with the keyword new in the main method, and the number 120 was passed as the constructor parameter. The question is, what will be the output of this code? Pause the video here and try to solve it yourself. We'll be solving this question together on the next slide. Yes, if your answer is option D, you have given the correct answer because this code will not compile. Now let's look at why option D is the correct answer, i.e. why this code cannot compile. Notice that the constructor in the Vehicle class is not an empty constructor, but a constructor with a parameter. Therefore, in the Car class that inherits from the Vehicle class, this constructor must be called with the super call statement. Otherwise, you will get a compilation error. If the super class Vehicle had only an empty constructor, then the code would be compiled without a compilation error.
Therefore, this code will not compile as is. The answer is option D. Let's move on to the next question. Yes, this question is about inheritance. Two classes are given in the same package. The class named Car inherits from the class named Vehicle. Also, the constructor is defined in both classes, and in the class named Car an object from the Car class was created with the keyword 'new' in the main method and the number 100 was passed to the constructor parameter. The question is, what will be the output of this code? Pause the video here and try to solve it yourself. We'll be solving this question together on the next slide. Yes, if your answer is option C, you have given the correct answer.
Let's examine the question now. Notice that there is a constructor in the super class Vehicle, and this constructor has a parameter with int type. Therefore, this constructor must be called with a super call in the Car class, which inherits from the Vehicle class. And this calling has been done correctly. This code will compile successfully as there are no omissions or errors in other parts of the code. So, what will be written on the console screen? Let's look at this now. If you notice, an object of the Car class is being created in the main method, so the constructor of the Car class will be called. When we look at the constructor of the Car class, first there is the super call statement, then the print method that prints the Car statement to the console. That is, since the super call statement is called first, it will call the super class's constructor, that is, the Vehicle class's constructor. Since there is a print method in the constructor of the Vehicle class, which prints the expression 'Vehicle' to the console, first the Vehicle will be printed to the console and then the Car statement. Therefore, the answer is option C.
Let's move on to the next question. In this question, there is an interface called CanGo, and in this interface there is an abstract method called getSpeed. We also have an abstract class called Vehicle, which implements the CanGo interface. And in Vehicle class there's an abstract method called getYear. Finally, a class named Car was created and this class extends the Vehicle class. And the getYear method is used in this class, and the value of 2022 is returned. The question asks us to choose the correct statement about this code block? Pause the video here and try to answer it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you chose option D, because this code cannot be compiled due to line 5. Now, let's look at why. In line 2, an abstract method is created in the CanGo interface. This is legal because methods and interfaces can be abstract.
Also, since this method has no body, it's defined correctly. So, there is no error in line 2. Let's look at the Vehicle class. Since the Vehicle class that implements the CanGo interface is abstract, it does not need to override the getSpeed method in the CanGo interface, and the getYear method is also defined as abstract. Therefore, there is no error here either. But the Car class that extends the Vehicle class must override the abstract methods in its super classes. Notice that only the getYear abstract method in the Vehicle super class is overridden. However, the getSpeed method in the CanGo interface is not implemented. In this case, the compiler will underline the Car class and tell us that there is a compiler error. There are two different ways to fix this error. The first is to override the getSpeed method in the Car class, just like the getYear method. The second is to make the Car class abstract as well.
Both solutions will fix the compilation error, but this code cannot be compiled as it is. Therefore, the correct answer is option D. Let's move on to the next question. This question is about the interface. Some modifiers are given in the options. The question asks, which modifier is implicitly implemented in all interface methods? Pause the video here and try to answer it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you selected option a. Now let's examine the options one by one. The public access modifier is given an option A. The public modifier is implicitly applied to all methods in an interface even if you do not define the method as public. Private access modifier is given in option B and the protected modifier is given in option C. However, these modifiers cannot be applied to a method inside an interface. You can only use the keywords: public, static, default, abstract or strictfp in a method in an interface. Let's talk briefly about the strictfp keyword.
The strictfp keyword is used in Java for restricting floating point calculations and ensuring the same results on every platform while performing operations in the floating point variable. At this stage, you just need to know that the keyword, strictfp, can be used in a method inside an interface. Therefore, options B and C are not correct answers. Void in option D is a return type. The methods in the interface can also have different return types. Therefore, option D is also not the correct answer. In option E, the static keyword is given. The static keyword can be applied to methods inside interfaces, but not to all methods. For example, if a method in an interface is declared as abstract, you cannot apply the static keyword to this method. In option E, the default keyword is given. The use of the default keyword is similar to static. The default keyword can be applied to methods inside an interface, but not to all methods.
For example, if a method in an interface is declared as abstract, you cannot apply the default keyword to this method. In the G option, the abstract keyword is given. In fact, before Java 8, all methods and interfaces were implicitly abstract. But after Java 8, this situation changed with the use of static and default keywords. So, if a method inside an interface is static, we cannot use the abstract keyword. So, the method only can be abstract, static or default. Therefore option G is also not correct. As a result, only the public keyword can be implicitly applied to all methods in an interface. Yes, we tried to solve together the questions that reinforce the topics we learned in this section, and are similar to the questions you may encounter in the exam. I hope it was useful. 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.