final in abstract

Discussion in 'Object Oriented Concepts' started by parulmahajan, Jun 10, 2010.

  1. parulmahajan

    parulmahajan New Member

    Can we have final variables in abstract classes?
  2. nicky

    nicky New Member

    Hi Friend,

    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 .
  3. jagsjava

    jagsjava New Member

    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
  4. nadeshganesh

    nadeshganesh New Member

    I think we can keep final variable in abstract class....
  5. arulchandru8

    arulchandru8 New Member

    please give some example
  6. sagarmelmatti

    sagarmelmatti New Member

    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()
    System.out.println("Hello show"+wid);

    public static void main(String[] args)
    Test2 t1=new Test2();;

    ---------- Run Java Class ----------
    Hello show10

    Output completed (0 sec consumed) - Normal Termination
  7. ravikumar4199

    ravikumar4199 New Member

    yes it is possible to place final variables in abstract class.
    interface is a pure abstract class.
  8. Karthikheyan

    Karthikheyan New Member

    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.
  9. nagendra_gang

    nagendra_gang New Member

    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
  10. mvvsatyanarayana

    mvvsatyanarayana New Member

    yes we can have final variables in abstract class.
  11. anneroberts

    anneroberts Member

    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”.

Share This Page