Vous êtes sur la page 1sur 9

Java Questions What is the difference between abstraction and encapsulation?

o Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it's inside view, where the behavior of the abstraction is implemented. o Abstraction solves the problem in the design side while Encapsulation is the Implementation. o Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.
How does Java implement polymorphism?

(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java). Polymorphism manifests itself in Java in the form of multiple methods having the same name. o In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods). o In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
Explain the different forms of Polymorphism.

There are two types of polymorphism one is Compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface. Note: From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java: o o o Method overloading Method overriding through inheritance Method overriding through the Java interface

What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
What is Dynamic Binding?

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given

procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
What is method overloading?

Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type. Note: o o o o o Overloaded methods MUST change the argument list Overloaded methods CAN change the return type Overloaded methods CAN change the access modifier Overloaded methods CAN declare new or broader checked exceptions A method can be overloaded in the same class or in a subclass

What is method overriding?

Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. Note: o The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can't override a method marked public and make it protected). o You cannot override a method marked final o You cannot override a method marked static
What are the differences between method overloading and method overriding?

Method overriding is when a child class redefines the same method as a parent class, with the same parameters. For example, the standard Java class java.util.LinkedHashSet extends java.util.HashSet. The method add() is overridden in LinkedHashSet. If you have a variable that is of type HashSet, and you call its add() method, it will call the appropriate implementation of add(), based on whether it is a HashSet or a LinkedHashSet. This is called polymorphism. Method overloading is defining several methods in the same class, that accept different numbers and types of parameters. In this case, the actual method called is decided at compile-time, based on the number and types of arguments. For instance, the method System.out.println() is overloaded, so that you can pass ints as well as Strings, and it will call. a different version of the method. Overriding is an example of run time polymorphism. The JVM does not know which version of

method would be called until the type of reference will be passed to the reference variable. It is also called Dynamic Method Dispatch. Overloading is an example of compile time polymorphism.
Can overloaded methods be override too?

Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.
Is it possible to override the main method?

NO, because main is a static method. A static method can't be overridden in Java.
What is the difference between constructors and other regular methods?

Constructors Regular methods Constructors must have the same name as the class name and cannot return a value. The constructors are called only once per creation of an object while regular methods can be called many times. E.x. for a ? Car.class 1 publicCar() {}// constructor 2 Regular methods can have any name and can be called any number of times. E.x. for a Car.class. ? publicString getCarName(){}// regular method has a String return type. 1 publicvoidcreateCar(){}// regular method has a void return type 2

What happens if you do not provide a constructor?

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit "Car(){}". If a class includes one or more explicit constructors like "public Car(int id)" or "Car(){}" etc, the java compiler does not create the default constructor Car(){}.
Can you call one constructor from another?

Yes, by using this() syntax, Ex ? 1publicCar(intid) {

2 this.id = id;// "this' means this object 3} 4 publicCar (intid, String type) { 5 this(id);// calls constructor public Car(int id) 6 this.type = type;// this means this object 7}

How to call the superclass constructor?

If a class called "SpecialCar" extends your "Car" class then you can use the keyword "super" to invoke the superclass's constructor. E.x. ? publicSpecialCar(intid) { 1 super(id);//must be the very first statement in the constructor. 2 } 3 To call a regular method in the super class use: "super.myMethod( );"

What's the difference between an interface and an abstract class?

An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.
Why would you use a synchronized block vs. synchronized method?

Synchronized blocks place locks for shorter periods than synchronized methods.
Explain the usage of the keyword transient?

This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).
How can you force garbage collection?

You can't force GC, but could request it by calling ? 1 System.gc(). JVM does not guarantee that GC will be started immediately.

How do you know if an explicit object casting is needed?

If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example: ? Object a; 1 Vehicle b; 2 b = (Vehicle) a; 3 When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

What's the difference between the methods sleep() and wait()

The code ? 1 sleep(2000); puts thread aside for exactly one second. The code ? 1 wait(2000) , causes a wait of up to one second. A thread could stop waiting earlier if it receives the ? 1 notify() or ? 1 notifyAll() call. The method ? 1 wait() is defined in the class Object and the method ? 1 sleep() is defined in the class Thread.

Can you write a Java class that could be used both as an applet as well as an application?

Yes. Add a ? 1 main() method to the applet.

What's the difference between constructors and other methods?

Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
Can you call one constructor from another if a class has multiple constructors

Yes. Use ? 1 this() syntax.

Explain the usage of Java packages.

This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.
If a class is located in a package, what do you need to change in the OS environment to be able to use it?

You need to add a directory or a jar file that contains the package directories to the ? 1 CLASSPATH environment variable. Let's say a class Employee belongs to a ? 1 packagecom.xyz.hr; and is located in the file

? 1 c:\dev\com\xyz\hr\Test.java . In this case, you'd need to add c:\test to the variable ? 1 CLASSPATH . If this class contains the method ? 1 main() , you could test it from a command prompt window as follows: ? 1 c:\>java com.xyz.hr.Test

What would you use to compare two String variables - the operator == or the method equals()?

I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.
How could Java classes direct program messages to the system console, but error messages, say to a file?

The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: ? Stream st =newStream(newFileOutputStream("test.txt")); 1 System.setErr(st); 2 System.setOut(st); 3

What is difference between JDK,JRE and JVM?

JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms (so JVM is plateform dependent). JRE : JRE stands for Java Runtime Environment. It is the implementation of JVM and physically exists.

JDK : JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
How many types of memory areas are allocated by JVM?

Some types are: o o o o o Class(Method) Area Heap Stack Program Counter Register Native Method Stack

What is JIT compiler?

Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
What is classloader?

The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
Explain static vs. dynamic class loading?

Static class loading Classes are statically loaded with Java's "new" operator. ? classMyClass { 1 publicstaticvoidmain(String args[]) { 2 Car c =newCar(); 3 } 4 } 5 Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically. ? Class.forName (String className);//static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Once the class is dynamically loaded the following method returns an instance of the loaded class. It's just like creating a class object with no arguments.

? 1 class.newInstance ();//A non-static method, which creates an instance of a

Vous aimerez peut-être aussi