Java Object-Oriented Concept
In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner 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
Hello friends. Now let's look at some questions about what we've learned. In this question, a simple program is given and it asks what will be the outputs of this program. Here, stop the video and try to figure out the output of this program yourself. I will answer the question on the next slide. Yes, if you chose E as the output, you answered correctly. Let's examine the question now. A class named Car is defined. This class has two instance variables. The first is of type string and is called color, and the second is of type boolean and is called isAutomatic. Then an object of this class is created in the main method. Therefore, color and isAutomatic properties can be accessed with this object.
And there are two print methods here. In the first print method, the value of the color variable is printed to the console. Since the color variable has not been assigned a value yet, it has a null value by default because the string data type is null by default. In the second print method, the value of the isAutomatic variable is printed to the console. Since this variable has not been assigned a value yet, we see the default value of the boolean data type, that is, false, on the console. Therefore, the program will compile successfully and the output color: null isAutomatic,
False, will be printed to the console. Let's move on to the next question. In this question, two variables are defined in the main method and the values of these variables are printed on the console. Let's find out what the output of the program can be. Pause the video here and try to figure it out yourself. I will answer the question on the next slide. Yes, if you ticked the E option, you gave the correct answer. Notice that these variables are created inside the method. So, these are local variables. Local variables must be initialized to print to the console, otherwise, you will get a compilation error. So, on lines 5 and 6, we get a compilation error and therefore the program cannot compile.
The answer is option E. Let's move on to the next question. In this question, it wants us to determine which of the following about method naming conventions is valid. Here, you can pause the video and answer the question. I'll be giving the answer on the next slide. Yes, the B and C options are not valid in accordance with the variable naming convention. Now, let's examine the options one by one. In option A, Java is written in capital letters. This is valid. Since the word Java is not a keyword, it will still be valid even if it's written in lowercase. Therefore, this nomenclature is valid. In option B, the method name starts with a digit. However, method names cannot start with a digit.
Therefore, this naming is invalid and will cause a compilation error. A special character is used in option C. The @ sign is a special character in Java, therefore, it cannot be used in variable or method naming. In option D, the method name starts with an underscore. This is legal. Also, if only the word public was used, it would be invalid, but since an underscore is used at the beginning of the method name, this is now a valid naming. In option E, the first letter 'Private' is used. This is also valid. If the word private consisted of all lowercase letters, then it would be included in the keyword group in Java and could not be used for naming. But since the first letter is capitalized and Java is a case-sensitive language, this no longer causes any errors. As a result, the names given in options B and C are not valid and will cause a compilation error. Let's move on to the next question. Yes, this question is about ordering elements in a class in Java.
The question asks, which is correctly defined? Pause the video here and try to solve it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you selected option D. Now let's look at the options in order. In option A, import is used after class, but import should be used after the package definition and before the class definition. Therefore, this definition will cause a compilation error. In option B, the import statement is used before the package definition. This is also an incorrect usage. In the first place should be the package definition. Therefore, this definition also causes a compilation error. In the C option, the class definition is used before the package and import definitions.
This is also an incorrect order and will cause a compilation error. In option D, the package name is written first and then the class definition is made. This is the correct ordering. It doesn't matter if the import statement exists or not, but if you're going to use the import statement, you should use it after the package name and before the class definition. Consequently, the correct answer is option D. Let's move on to the next question. Yes, this question is about the import concept. The question is, which correctly imports the "Animals" class from the package "com.academy"? Here, pause the video and try to answer it yourself. I'll be giving the answer on the next slide. Yes, the correct answer is given in option C.
Now let's examine the options one by one. In option A, the expression com.academy is used after the import keyword, but this is incorrect usage and will cause a compilation error. Because here, instead of the class, the academy package seems to be imported and it's wrong because only a type can be imported. In option B, an asterisk is used after com.academy, but a dot must be used before the star, so this will cause a compilation error. Importing the Animals class in the C option is done correctly. Also, a star could be used instead of Animals here. If we used the star, we would have imported all of the classes in the academy package. This way we only import the Animals class.
Since the question asks about importing the Animals class, this option contains the statement that correctly imports the Animals class. In option D, the places of the star and the dot are changed again. The dot should be placed after the word academy and then a star should be used. Consequently, the correct definition is given in option C. Let's move on to the next question. Yes, this question is about Local Classes. Some expressions about Local Classes are given in the options. The question asks us to find which statement is false regarding Local Classes. Pause the video and try to figure it out yourself. I'll be giving the answer on the next slide. Yes, if you selected option B, you have given the correct answer.
Now, let's examine the options one by one. In option A, it says that the scope of local classes is limited by the block in which they are defined. This is true. Because of this feature, they are called Local Class. In option B, it's said that we can access non-final variables defined in the block containing the local class from within the local class. The trick here is that this feature comes with Java 8. Before Java 8, in order to access the variable in the block containing the local class, this variable had to be defined as final. We cannot say this statement is correct as it does not specify this detail here.
In the C option, it says that if we define local classes inside a method, we can access the method's parameters. This is true. We have already mentioned this while describing Local Classes. In option D, it says local classes are not static. This is true. If they are static then they cannot access the instance numbers of the class they are in. As a result, we cannot say that the statement in option B is correct due to missing information, but the statements in the other options are true. 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.