Discussion in 'Object Oriented Concepts' started by parulmahajan, Jun 10, 2010.
Can we have final variables in abstract classes?
No. we cant have final variables in abstract classes . Final declaration resists inheritance and also dont allow a final variable to be modified once it is declared . But as abstract classes might contain abstract methods, these abstract methods needs to be implemented in the class which extends from this class. And if at all the abstract method which is being overridden by the class which extends form this one uses this final variable ,there results in an error as final variables cant be modified once they are declared as they are constants .
The abstract class is a class which contains abstract method and general methods. These methods have to be override for our purpose in the application. if we use a final keyword before any abstract class then we cant extend or inherit that class.
If use final before methods we cant override them.If use final before Variables we cant change the value ,i.e remains constant throughout the program
I think we can keep final variable in abstract class....
please give some example
ya sure, we can have final variable in the abstarct class there is no problem , but as we know we can use final variable in subclass which extends abstarct class but we cannt change its value.
I just explain you with example:
abstract class A
abstract void show();
final int wid=10;
public class Test2 extends A
public void show()
public static void main(String args)
Test2 t1=new Test2();
---------- Run Java Class ----------
Output completed (0 sec consumed) - Normal Termination
yes it is possible to place final variables in abstract class.
interface is a pure abstract class.
A abstract class allows both final methods and final variables, but the abstract methods inside a abstract class cannot be final. In simple words, a method can either be final or abstract.
Yes You can use final in abstract class but just remember that both the key word (abstract and final) can not use for a member
abstract class Xyz
final int i = 10;//ok
abstract final void abc();// not allowed
yes we can have final variables in abstract class.
Class, which can't a chance to be instantiated should make its Questions. Instantiated Assuming that it overrides the sum theoretical routines Toward usage them. Superclass theoretical strategies may be itself abstract; Furthermore it can't be instantiated. ¥ java backs this is through the Pivotal word “final”.
Separate names with a comma.