What's the difference between abstraction and encapsulation?
A: Abstraction and encapsulation are two quite separate concepts in Java. Abstraction is a technique that is used to represent common functionality amongst a set of classes. An analogy is to look at a set of vehicles: Car, Truck and Bus have some common features that all road vehicles share. From a Java perspective, the mechanics of turning four wheels, an engine, could be handled in abstract form through a common superclass. Abstraction allows subclasses to share common code without duplication.
What is the difference between static and dynamic polymorphism?
A: The term static polymorphism is associated with overloaded methods because it gives the impression that a single named method will accept a number of different argument types. The System.out.println() method is an example that may take String or Object references, boolean and other primitive types as an argument. In fact, each overloaded method is separate and the compiler can see the difference between them. In this case, the argument types are fixed at compile time and are considered static. This has nothing to do with the Java keyword static.
Dynamic polymorphism is where a class overrides a superclass method or implements an interface. For example, any class may override the Object.toString() method and provide its own implementation, and this is known at compile time. However, for a simple Java program that instantiates a series of objects and calls their toString() method, the compiler does not consider the object references differently. Any differences in the objects' toString() implementations are only seen at runtime, so they are considered dynamic.
Is println overloading or overriding?
A: The PrintWriter println() method is an example of overloading because several methods in the class have the same name and return the same type. In this case, println(boolean), println(int) and println(String) all have the same basic method name, "println", and all return void. The only part of the method signature that varies is the type of the argument (including none), which is enough for the Java interpreter to identify the appropriate method to call at runtime.
Does the order of the operands in == matter?
A: With this sort of question it is often easiest to try it yourself and see. You will find that single boolean comparisons are equivalent whichever way round you have the values.
From a reader's point of view most people would put the "unknown" variable first because it is the subject of the comparison, but syntactically it does not matter.
if (a == 10) { }
What are keywords and reserved words?
A: Java keywords are standard English words that have a special meaning in the Java programming language. Keywords include class, interface, abstract, public, static and final, which are used to declare the type and nature of Java compilation units. The statements used to define variables and method bodies include keywords new, return, if, while and throws. These words are interpreted by the Java compiler and used to produce byte code that can be run as a program.
No comments:
Post a Comment