04-02-25 ---------- Q)final modifier can be applied to which type of variables? 1)local variables 2)instance variables 3)static variables For eg. class Test{ final int b=10;//final instance variable static final int c=20;//final static variable void m(){ } public static void main(String[] args) { final int a=10;//a is a local final variable } } Note:- Once a final variable is initilized, its value can't be changed programmatically in the later part of the program. final method example ------------------------- class A{ final void x(){ } } class B extends A{ void x(){ //error.final method can't be overridden. } } class Test{ public static void main(String args[]){ B b=new B(); b.x();//OK. final method can be inherited } } Q)Which class is generally declared final? =>In a heirarchy of user defined classes, the most specifinalized class is declared final to prevent further specialization. =>Some times, to make a Java class immutable(non modifiable), it can be declared final. For eg. String class. class MyString extends String{//error } Q)Explain about "abstract" modifier. =>An abstract keyword can be associated only with a class and a method but not with variables. I.e. we don't have abstract variables. =>If a class is declared abstract, it can't be instantiated. But its reference can be created. For eg. abstract class A{ } class Test{ public static void main(String args[]){ A a=new A();//error } } =>We can create a reference of an abstract class. =>An abstract class can be inherited. =>An abstract class can have a constructor(for child sake) =>The most generalised class in a hierarchy of user defined classes is declared "abstract" For eg. abstract class Vehicle{ } =>Vehicle is too generalised and incomplete to create an instance of it. =>An abstract class can have zero or more abstract methods. =>An abstract class can have non-abstract methods also Q) What is an abstract method? =>bodyless method is an abstract method. For eg. abstract class A{ abstract void x(); //no body void m(){ } //m has null body }