Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=====Abstract classes===== An [http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.1.1 Abstract Class] is a class that is incomplete, or is to be considered incomplete, so cannot be instantiated. A class C has abstract methods if any of the following is true: * C explicitly contains a declaration of an abstract method. * Any of C's superclasses has an abstract method and C neither declares nor inherits a method that implements it. * A direct superinterface of C declares or inherits a method (which is therefore necessarily abstract) and C neither declares nor inherits a method that implements it. * A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class and, therefore, the execution of the field initializers for instance variables of that class. <syntaxhighlight lang="java"> package org.dwwwp.test; /** * @author jcrypto */ public class AbstractClass { private static final String hello; static { System.out.println(AbstractClass.class.getName() + ": static block runtime"); hello = "hello from " + AbstractClass.class.getName(); } { System.out.println(AbstractClass.class.getName() + ": instance block runtime"); } public AbstractClass() { System.out.println(AbstractClass.class.getName() + ": constructor runtime"); } public static void hello() { System.out.println(hello); } } </syntaxhighlight> <syntaxhighlight lang="java"> package org.dwwwp.test; /** * @author jcrypto */ public class CustomClass extends AbstractClass { static { System.out.println(CustomClass.class.getName() + ": static block runtime"); } { System.out.println(CustomClass.class.getName() + ": instance block runtime"); } public CustomClass() { System.out.println(CustomClass.class.getName() + ": constructor runtime"); } public static void main(String[] args) { CustomClass nc = new CustomClass(); hello(); //AbstractClass.hello();//also valid } } </syntaxhighlight> Output: <syntaxhighlight lang="text"> org.dwwwp.test.AbstractClass: static block runtime org.dwwwp.test.CustomClass: static block runtime org.dwwwp.test.AbstractClass: instance block runtime org.dwwwp.test.AbstractClass: constructor runtime org.dwwwp.test.CustomClass: instance block runtime org.dwwwp.test.CustomClass: constructor runtime hello from org.dwwwp.test.AbstractClass </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)