class Car { final int speedlimit = 180; public void Start() { System.out.println
ID: 3754638 • Letter: C
Question
class Car {
final int speedlimit = 180;
public void Start() {
System.out.println("Car is started..");
}
}
class Van extends Car {
int speedlimit = 130;
@Override
public void Start() {
System.out.println("Van is started..");
}
}
public class CarTest {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Van();
}
}
Answer the following questions:
a. The output for the following code
System.out.printf("Car 1 Speed's limit = %d ",c1.speedlimit);
System.out.printf("Car 2 Speed's limit = %d ",c2.speedlimit);
will be:
Car 1 Speed's limit = 180
Car 2 Speed's limit = 180
a. Explain why?
b. How can you achieve the following output without changing anything in Car and Van class:
Car 2 Speed's limit = 130
And using instance c2 created in line 22?
c. What is the output for the following code?
c1.Start();
c2.Start();
d. Why static binding called sometimes “early binding” where dynamic binding called “late binding”?
please help ( in java programing language)
Explanation / Answer
Answer for question a:
A method is overridden, not the data members, so runtime polymorphism can't be achieved by data members.
In the example given , both the classes have a data member speedlimit.
First we are accessing the datamember by the Parent(own) class, so obviously, c1.speedlimit prints 180 as output.
Secondly,we are accessing the data member by the reference variable of Parent class(Car) which refers to the subclass object(Van). Since we are accessing the data member which is not overridden, hence it will access the data member of the Parent class always.
Answer for question b:
Just change Car reference to Van at line 22.
Example: Van c2 = new Van(); This line prints "Car 2 Speed's limit = 130" as output.
Answer for question c:
Outputs:
c1.Start() method calls Start() method present in Car class and prints Car is started...
beacuse c1 is an instance of Car class.
c2.Start() method calls Start() method present in Van class and prints Van is started...
beacuse c2 is an instance of Van class and assigning instance of Van class to Parent(reference) class reference and Van class overrides the Start() method in Van class.This is called Runtime polymorhpism.
Answer for question d:
Binding usually means associating. There are two kinds of binding namely early binding and late (dynamic) binding
Static Binding
It is a binding that happens at compile time.
Actual object is not used for binding.
It is also called early binding because binding happens during compilation.
Method overloading is the best example of static binding.
Private, static and final methods show static binding. Because, they can not be overridden.
Dynamic Binding
It is a binding that happens at run time.
Actual object is used for binding.
It is also called late binding because binding happens at run time.
Method overriding is the best example of dynamic binding.
Other than private, static and final methods show dynamic binding. Because, they can be overridden.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.