bude vhodné, aby barva byla zapouzdřená a v metodě pro její změnu (setColor, setBarva) bylo překreslení tohoto objektu.
Object
.
implements
.
Například:
public class Obdelnik extends DveDObjekt implements Srafovatelny,Serializable, Comparable{
}
finalize
,
kterou volá GC při uvolňování místa. Pozor, nikdo nezaručí, že tato
metoda bude zavolána a kdy. Program může skončit bez potřeby uklízení
paměti pomocí GC, pak není tato metoda volána). String s = new String("abc"); // proměnná s je typu reference na String
GrO
třídu reprezentující grafický objekt s atributem barva a metodou
kresliSe. Tato metoda je abstraktní, její implementaci provedou až
potomci (Usecka, Bod, Obdelnik) podle své povahy.
package mujBalik1.mujBalik2; [abstract] [public] class PrvniTrida [extends Predek] [implements Rozhrani1 [, Rozhrani2...]] { ... tělo ... }
java.lang.Object
), pokud neuvedeme extends, tak je jím právě třída Object.
PrvniTrida(int x) { super(x); // další příkazy }
Serializable
, Runnable
(definuje metodu run), …
class TridaA { TridaA() { System.out.println("Class A constructor"); } } class TridaB extends TridaA { TridaB() { System.out.println("Class B constructor"); } } class TridaC extends TridaB { TridaC() { System.out.println("Class C constructor"); } public static void main(String[] args) { TridaC x = new TridaC(); } } // konstruktor předka je volán i když potomek žádný konstruktor // nemá definován (protože má implicitní konstruktor)
int foo(int x) { return 1; } int foo(double x) { return 1; } int foo(int x, int y) { return 1; } int foo(String x) { return 1; } double foo(int x) { return 1; } // nelze, koliduje s int foo(int x)
public static void main(String[] args) { class VnorenaTrida { public void call() { System.out.println("print called"); } } VnorenaTrida vt = new VnorenaTrida(); vt.call(); } // zde už není VnorenaTrida "známa" (mimo rámec platnosti)
public class BigClass { private int x = -2; private SmallClass sc = new SmallClass(); class SmallClass { public void printx() { System.out.println(x); } } public void print() { sc.printx(); } public static void main(String[] args) { BigClass bc = new BigClass(); bc.print(); } }
interface I1 { void f(); } interface I2 { int f(int i); } interface I3 { int f(); } abstract class CompositeI12 implements I1, I2 { } // ok abstract class CompositeI123 implements I1, I2, I3 { } // fail // funkce f u I1 a I3 mají různé návratové typy
public class Main implements java.lang.Runnable { public void run() { } public static void main(String[] args) { Main m = new Main(); if (m instanceof java.lang.Runnable) { System.out.println("Is Runnable"); } } }
Spuštění této třídy:
public class Test{ public static void main(String[] args) { Thread t = new Thread(new Main()); t.start(); } }