What will be the output of these question and answer them by comment i'll be sharing the correct answers later.
These question are frequently asked by interviewer to check your basic java concepts regarding final
Answering them correctly will ensure you that concept of final in java is cleared to you. Please do comment the answers thanks.
1. final class ParentClass{
These question are frequently asked by interviewer to check your basic java concepts regarding final
Answering them correctly will ensure you that concept of final in java is cleared to you. Please do comment the answers thanks.
1. final class ParentClass{
protected int parentId;
public ParentClass(){
parentId=1000;
}
}
class ChildClass extends ParentClass{
private int childId;
public ChildClass(){
childId=2000;
}
public void display(){
System.out.println("Parent Id:"+parentId+" Child Id:"+childId);
}
}
class Demo{
public static void main(String args[]){
ChildClass childOne = new ChildClass();
childOne.display();
}
}
2. class ClassExample{
protected int classId;
private final int MAX=10;
private static int counter;
public ClassExample(){
classId=counter++;
if(counter>MAX){
MAX=20;
counter=0;
}
System.out.println("MAX:"+MAX+"counter :"+counter);
}
}
class Demo{
public static void main(String args[]){
ClassExample exampleOne = new ClassExample();
}
}
3 .class ClassExample{
protected int classId;
private final int MAX=10;
private static int counter;
public ClassExample(){
classId=counter++;
if(counter>MAX){
counter=0;
}
System.out.println("MAX:"+MAX+" counter :"+counter);
}
}
class Demo{
public static void main(String args[]){
ClassExample exampleOne = new ClassExample();
}
}
4.class ClassExample{
protected int classId;
private final int MAX=10;
private static int counter;
public ClassExample(){
classId=counter++;
if(counter>MAX){
counter=0;
}
}
public final void display(){
System.out.println("MAX:"+MAX+" counter :"+counter+ " classId:"+classId);
}
}
class DerivedExample extends ClassExample{
private int derivedId;
public DerivedExample(){
derivedId=10;
}
public void display(){
super.display();
System.out.println("derivedId:"+derivedId);
}
}
class Demo{
public static void main(String args[]){
DerivedExample exampleOne = new DerivedExample();
exampleOne.display();
}
}
0 comments :
Post a Comment