fork download
  1. //抽象クラス
  2. abstract class Foo {
  3. int x;
  4. int y;
  5. public abstract void print(); //抽象メソッド
  6. public void method(int x,int y){
  7. this.x=x;
  8. this.y=y;
  9. }
  10. }
  11.  
  12. class MyClass extends Foo{
  13. public void print(){
  14. System.out.println("x:" + x + "y:" + y);
  15. }
  16. }
  17.  
  18. class sample{
  19. public static void main (String[] args){
  20. //スーパークラスの変数にサブクラスのオブジェクトを代入
  21. Foo f = new MyClass();
  22. f.method(10,20); //スーパークラスのメソッド呼び出し
  23. f.print(); //サブクラスのメソッド呼び出し
  24. }
  25. }
Success #stdin #stdout 0.18s 55496KB
stdin
Standard input is empty
stdout
x:10y:20