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!
====Inheritance==== Classes in Java can only [[Inheritance (object-oriented programming)|inherit]] from ''one'' class. A class can be derived from any class that is not marked as <code>final</code>. Inheritance is declared using the <code>extends</code> keyword. A class can reference itself using the <code>this</code> keyword and its direct superclass using the <code>super</code> keyword. <syntaxhighlight lang="java"> class Foo { } class Foobar extends Foo { } </syntaxhighlight> If a class does not specify its superclass, it implicitly inherits from <code>java.lang.Object</code> class. Thus all classes in Java are subclasses of <code>Object</code> class. If the superclass does not have a constructor without parameters the subclass must specify in its constructors what constructor of the superclass to use. For example: <syntaxhighlight lang="java"> class Foo { public Foo(int n) { // Do something with n } } class Foobar extends Foo { private int number; // Superclass does not have constructor without parameters // so we have to specify what constructor of our superclass to use and how public Foobar(int number) { super(number); this.number = number; } } </syntaxhighlight> =====Overriding methods===== Unlike C++, all non-<code>final</code> methods in Java are [[Virtual function|virtual]] and can be overridden by the inheriting classes. <syntaxhighlight lang="java"> class Operation { public int doSomething() { return 0; } } class NewOperation extends Operation { @Override public int doSomething() { return 1; } } </syntaxhighlight> =====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)