Q 1: What happens if you don’t override equals method?
A: If you don’t override equals method then you won’t be able to use those objects as key in hashtable and you probably won’t get accurate Sets such that there are no conceptual duplicates.
Q2: How do you implement equals method?
A: Suppose you have an Employee Class which looks like below
public class Employee{
private long employeeId;
private String name;
private int salary;
//... public getter and setter methods
public boolean equals(Object other){
if((other instanceof Employee) &&
((Employee)other).getEmployeeId() == this.employeeId){
return true;
}else{
return false;
}
}
}
A: Equals contract says that:
- It is reflexive. For any any reference value x, x.equals(x) should return true.
- It is symmetric. For any reference values x and y, x.equals(y) should return true if and only if y.equals(x) is true.
- It is transitive. For any reference values x, y and z, if x.equals(y) returns true and y.equals(z) returns true then x.equals(z) must return true.
- It is consistent. For any reference values x and y, multiple invocations of x.equals(y) consistently returns true or consistently returns false, provided no information used in equals comparision on the object is modified.
- For any non-null reference value x, x.equals(null) should return false.
Q4: In How Many Ways can you create an object for a class in java?
A: There are four ways in which we can create an object for a class in java
- Using New Operator: like Test t = new Test();
- Using newInstance(): like
Test t = (Test) Class.forName("").newInstance();
- Using Clone: like
Test x=new Test();
Test t=x.clone();
- Using Object Deserialization : like ObjectInputStream istream=new ObjectInputStream(some inputStream);
Q5: Can we create constructor for an abstract class?
A: Yes, we can create Constructor for abstract
class. The constructor will be invoked while instantiating its subclass
object. The constructor can be invoked explicitly by using super
keyword.
Q3: Is it possible to override static method?
A: No it is not possible to override a static method. This because there is only 1 instance of a static method at class level.
Q4: Is it possible to access non-static method from a static method
A: No static method can not access non-static methods Java
has two types of methods, instance methods and static methods. A static
method can be accessed without creating an instance of the class. If
you try to use a non-static method and variable defined in this class
then the compiler will say that non-static variable or method cannot be
referenced from a static context. Static method can call only other
static methods and static variables defined in the class.
Q5: Why is Java not fully object oriented?
A: In order to be fully object oriented a language must support classes, objects, inheritance and polymorphism. C++ is fully object oriented as it supports all the types of inheritances i.e. single, multilevel, multiple, hierarchical
and multipath inheritances and if talk about polymorphism, C++ supports
static binding and operator overloading which come under static polymorphism.
Java is not fully object oriented as Java does NOT support operator overloading and multiple inheritances Java supports multiple inheritance through interfaces though.
Q6: What is the difference between class variable, member variable and automatic(local) variable.
A: Instance
variables are shared among thread while local variables are not shared.
Instance variables are also shared among method invocations while local
or automatic variables are created fresh for each invocation.
Ie. Instance variables persist their value among method invocations
while local variables are destroyed as and when their scope block ends,
and are recreated each time the method is invoked.
Q7: Are
the imports checked for validity at compile time? e.g. will the code
containing an import such as java.lang.ABCD com compile successfully?
A:
Yes the imports are checked for the semantic validity at compile time.
The code containing above line of import will not compile. It will throw
an error saying,can not resolve symbol
symbol : class ABCD
location: package io
import java.io.ABCD;
No comments:
Post a Comment