Wednesday, February 2, 2011

final keyword

Q: Explain about Final Keyword ?

Ans:
final is keyword we can apply these key word to the variable,methods and classes
  1. final variable: if we apply a final keyword to a variable, that variable cannot be modified the value of the variable i.e we can gives final for a CONSTANTS
Ex: final float constant_pie=22.
// we cant increment or decrement i.e constant_pie++;

2. final methods: if we apply a final keyword to a method, that method cannot be overloaded int the sub classes


Ex:
public class Car{
final maruthi(float speed,float mileage)
{
//car
}
}
public class MaruthiCar extends Car{
super.maruthi(float,float) //invalid
{
//code
}
}


3. final class:if we apply a final keyword to a class, that class cannot be extended to other classthat means in not support

Ex:
public class Car{
final maruthi(float speed,float mileage)
{
//car
}
}
public class MaruthiCar extends Car
{
//code
}

No comments:

Post a Comment