Question:
is it possible to use modifiers before abstract class or method in java?
Sravankumar
2011-07-13 00:23:38 UTC
like public,private,protect before abstract class or method if it is possible in what case it is?
Four answers:
2011-07-13 09:11:47 UTC
At root level an Abstract Class can be Public only. As an inner class they can be public, private or protected. There are various reasons for declaring them any of these.



Abstract public classes can be useful if you want to use the class from inside but want to leave the option of using it outside. [common]



Abstract private classes can be useful if you intend on extending the class inside but do not want it accessible to any outside classes or extended classes. [rare]



Abstract protected classes can be useful if you want to make the class extendable by sub-classes only. [common]



The same rules and motivations apply to methods as well except abstract methods cannot can be declared private. The only situation in which a private abstract method would be needed would be the same as the rare class access above but in this scenario the word abstract is redundant so it is prohibited.



I'll only show the rare example here:



public abstract class RootClass

{

    private abstract class InsidePrivate

    {

        private void method2()

        {

            // abstract is redundant for this method so it's not allwoed

            // the same functionality can be provided by leaving the body blank

        }

        

        private class InsideExtender extends InsidePrivate

        {

            // this class is extending a private abstract class

            // InsidePrivate can only be extended by inner classes

            // it is invisible to outside classes, and to extended-classes of RootClass

            private void method2()

            {

                // replaces method2 in InsidePrivate,

            }

        }

    }

}



Use of this functionality is rare, but it is mainly used in data structures such as HashMaps etc where these classes are utility classes defined to do a specified function which no outside class should ever need to access or know about i.e. in areas where Abstraction is used.
nishi
2011-07-13 02:11:57 UTC
The abstract modifier is placed before classes or methods. For a class, it means that it cannot be directly instantiated, but has to be subclassed. For a method, it means that it does not have an implementation in the class, but has to be implemented in a subclass. It cannot be applied to variables.





here is it in more detail now.....



This question leads to two things

1. modifier for abstract class or interface

2. modifier for members of abstarct class or interface



For first in abstract class we can have public , protected and default and not private if it is top level otherwise private also if it is nested.

Interface can only be Public/Default or abstract



for point 2

in an interface every member variable is public and final.....

in an abstract class you can assign any modifier to member variables

abstract methods cannot be declared private, static, final, native, strictfp or synchronized
Vaibhav
2011-07-13 05:30:59 UTC
The abstract class and its abstract methods can be public,protected or default but not private because the subclass has to override those methods

also you have to not define an abstract class final otherwise you may not be able to extend it and thus you cannot instantiate its object anyway
2016-10-23 04:32:26 UTC
to have an precis technique, the class itself could desire to be precis additionally. you may not instatiate an precis type once you subclass the precis type, then you definately could desire to fill out the precis technique physique. that is empty, eg void replace() { // does not something } making use of this form of heirachy it truly is achieveable to have many categories of cellular Sprites animations all partaking in a 2d activity. In my occasion, replace() ought to upload to a rocket's coordinates, have a ball be certain if it replaced into hit. the two rocket and ball could have their very own drawGraphic( Graphics2D g2) to maintain the action shifting on the exhibit. Having an excellent sort of precis Sprite could enable me to create an array of Sprite[] placed the two rocket and ball into that array and make contact with replace() throughout the time of each of diverse subclasses.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...