Dynamic Method Dispatch or RunTime Polymorphism
Dynamic Method Dispatch is a process in which a call to an overriden method is resolved at runtime rather than compile time. An overriden method is called through the reference variable of a super class.
When reference variable of a parent class refers to the object of a child class, it is known as upcasting.
class India{}
class Mumbai extends class India{}
India ind = new Mumbai() // This is called upcasting
Example :-
class Vehicle {
void drive(){
System.out.println("Driving Vehicle");
}
}
class Bentley extends Vehicle {
void drive() {
System.out.println("Driving Bentley");
}
public static void main(String ar[]){
Vehicle v = new Bentley();
v.drive();
}
}
OUTPUT: Driving Bentley
1. What is the difference between an executable file and a .class file ?
A) ".exe" file contains machine language instructions for the microprocessor and is system dependent. ".class" file contains byte code instructions for the JVM and is system independent.
Dynamic Method Dispatch is a process in which a call to an overriden method is resolved at runtime rather than compile time. An overriden method is called through the reference variable of a super class.
When reference variable of a parent class refers to the object of a child class, it is known as upcasting.
class India{}
class Mumbai extends class India{}
India ind = new Mumbai() // This is called upcasting
Example :-
class Vehicle {
void drive(){
System.out.println("Driving Vehicle");
}
}
class Bentley extends Vehicle {
void drive() {
System.out.println("Driving Bentley");
}
public static void main(String ar[]){
Vehicle v = new Bentley();
v.drive();
}
}
OUTPUT: Driving Bentley
- Runtime polymorphism cannot be achieved by data members.
- Runtime polymorphism can be achieved through multilevel inheritance.
1. What is the difference between an executable file and a .class file ?
A) ".exe" file contains machine language instructions for the microprocessor and is system dependent. ".class" file contains byte code instructions for the JVM and is system independent.
2. Why Java is suitable for Internet ?
A) Java is suitable for Internet because of two main reasons.
1) It is system independent and hence its programs can run on any type of computer system available on internet.
2) It eliminates a lot of security problems for data on internet.
3. Why Pointers are eliminated in Java ?
A) 1) Pointers lead to confusion for a programmer.
2) Pointers may crash a program easily, for example, when we add two pointers, the program crashes immediately. The same thing could also happen when we forget to free the memory allotted to a variable and reallocate it to some other variable.
3) Pointers break security. Using pointers, harmful programs like Virus and other hacking programs can be developed.
Because of the above reasons pointers have been eliminated from Java.
4. What is the difference between a function and a method ?
A) A method is a function that is written in a class. We do not have functions in Java; instead we have methods. This means whenever a function is written in Java, it should be written inside the class only. But if we take C++, we can write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.
5. Is Java a purely object oriented language or not ?
A) The following reasons are put forward by many people to say Java is not a purely object oriented programming language.
1. 'Purely object oriented' means it should contain only classes and objects. It should not contain primitive datatypes like int, float, char etc, since they are neither classes nor objects.
2. In pure object oriented languages, we should access every thing by messages passing (through objects). But, Java contains static variables and methods which can be accessed directly without using objects.
3.Java does not contain multiple inheritance. It means an important feature of object oriented design is lacking. So how can we say it is purely object oriented ?
No doubt Java is purely object oriented programming language. The preceding points represent lack of in depth understanding of Java.
1. Even if Java has primitive datatypes, these types are used inside a class and never outside of it. So, they are part of a class. See the API specification on the class. 'Class'.Java specification says that all the arrays and the primitive Java types(boolean,byte,char,short,int,long,float, and double), and the keyword void are also represented as objects of the class 'Class'.
2. Even static variables and static methods are written inside a class. When accessing them from outside, we should use classname. It means they are part and parcel of class definition and should not be considered as individual elements. For reducing memory utilization, only one copy of them will be created in memory and shared by all objects.
3. Any purely object oriented language should follow all the 5 features of Object oriented Programming System(OOPS). They are :-
- Classes and objects.
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
6. Which part of JVM will allocate the memory for a Java program ?
A) Class loader subsystem of JVM will allocate the necessary memory needed by the Java program.
7. Which algorithm is used by garbage collector to remove the unused variables or objects from memory ?
A) Garbage collector uses many algorithms but the most commonly used algorithms is mark and sweep.
8. How can you call the garbage collector ?
A) Garbage Collector is automatically invoked when the program is being run. It can be also called by calling gc() method of Runtime class or System class in Java.
No comments:
Post a Comment