14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java syntax 192<br />

Modifiers<br />

• static - Makes the field a static member.<br />

• final - Allows the field to be initialized only once in a constructor.<br />

• transient - Indicates that this field will not be stored during serialization.<br />

• volatile - If a field is declared volatile, it is ensured that all threads see a consistent value for the variable.<br />

• Access modifiers - Identical to those used with classes<br />

Inheritance<br />

Classes in Java may only inherit from one class. A class may derive from any class that is not marked as final.<br />

Inheritance is declared using extends keyword. A class may reference itself using this keyword and its direct<br />

superclass using super keyword.<br />

class Foo {<br />

}<br />

class Foobar extends Foo {<br />

}<br />

If a class does not specify the superclass, it implicitly inherits from java.lang.Object class. Thus all classes in Java<br />

are subclasses of Object class.<br />

Overriding methods<br />

Unlike C++, all non-final methods in Java are virtual and can be overridden by the inheriting classes.<br />

class Operation {<br />

}<br />

public int do() {<br />

}<br />

return 0;<br />

class NewOperation extends Operation {<br />

}<br />

public int do() {<br />

}<br />

Abstract classes<br />

return 1;<br />

Abstract classes are classes that only serve as templates and cannot be instantiated. Otherwise it is just like an<br />

ordinary class.<br />

Only abstract classes are allowed to have abstract methods. Abstract methods do not have any implementation and<br />

must be overridden by a subclass unless it is not abstract itself.<br />

abstract class Mammal {<br />

}<br />

public abstract void walk();<br />

class Human extends Mammal {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!