Java SE 8 Programmer I (1Z0–808) Important Questions 101 Day 1

sandeep negi
2 min readApr 19, 2020

--

Question 1. With inheritance, the instance variables bind at compile-time and methods bind at runtime?

Let's find out what is the meaning of the above problem and what are all the possible cases

Hi Everyone I am a Software Developer in one of the big tech companies. While I am preparing for the OCA 1Z0–808 this certification is for the java 8 and for the associate level after that we have one for the professional level. Coming back to the thing why I am writing this series of blog posts because these are some of the most important questions and difficult topics that I have faced while I was preparing for the exam. It will be easier for you to just read the full explanation for the questions and understanding the topics. Let's cut it short and dive in.

Check out this code snippet and then I will explain the output and why the output is like that.

public class Employee {
String name=”Employee”;
void printName()
{
System.out.println(name);
}
}

I don’t think I need to explain the above code it is easily understandable.

public class Programmer extends Employee{
String name=”programmer”;
void printName()
{
System.out.println(name);
}
}

In the above lines of code, we have created a class and extend the Employee class and override the method print name. Now let's see the main method and check out the output.

public class App
{
public static void main( String[] args )
{
Employee e1=new Employee();
System.out.println(e1.name);
e1.printName();
Employee programmer=new Programmer();
System.out.println(programmer.name);
programmer.printName();
}
}

Guess what the output of the above code

Employee
Employee
Employee
programmer

This is because the instance variables are bind at compile so for e1.name and e1.printName it is simple output. But in case of programmer, we have a reference Variable Employee so it will check the variable name in parent during compile-time and it will get the value as Employee. But in the case of programmer.printName(), it will check during runtime which method should be called so the method, in this case, will be the child class method.

In case of any doubt please feel free to comment and share with your friends and learn together.

--

--

sandeep negi
sandeep negi

Written by sandeep negi

Software Engineer, who loves challenges and new technologies. :)

No responses yet