5o Ergastirio

APXIKH 1o Ergastirio 2o Ergastirio 3o Ergastirio 4o Ergastirio 5o Ergastirio 6o Ergastirio 7o Ergastirio Programma Exetastikis

AbstractDemo

abstract class A  {
     abstract void callme();

     //πλÞρεις μÝθοδοι επιτρεπονται επßσης !!!
     void callmetoo()  {
 System.out.println("This is a concrete method.");
     }
}

class B extends A {
     void callme()  {
 System.out.println("B's impletation of callme.");
     }
}

class AbstractDemo  {
     public static void main(String args[])  {
 B b = new B();

 b.callme();
 b.callmetoo();
     }
}

Override

class A {
     int i, j;

     A(int a, int b)  {
 i=a;
 j=b;
     }

     //εμφÜνιση των i και j
     void show()  {
 System.out.println("i and j:  " + i + "  "  + j);
     }
}

class B extends A {
     int k;

     B(int a,int b, int c)  {
 super(a,b);
 k = c;
     }

     //εμφÜνιση του k. Υπερφüρτωση της show() της Α.
     void show()   {
 System.out.println("k = " + k);
     }
}

class Override {
     public static void main(String args[])  {
 B subOb = new B(1,2,3);
 subOb.show();  //κλÞση της show() στο Β.
     }
}

Simple Inheritance


//δημιουργßα superclass
class A {
      int i,j;

     void showij() {
 System.out.println("i and j: " + i + ","  +  j);
     }
}

//δημιουργßα μιας subclass που κληρονομεß την Α.
class B extends A {
     int k;

     void showk()  {
 System.out.println("k:  "  +  k);

     }
     void sum()  {
 System.out.println("i+j+k:  "  +  (i+j+k));
     }
}

class SimpleInheritance {
     public static void main(String args[])  {
 A superOb = new A();
 B subOb = new B();

 //Η superclass μπορεß να χρησιμοποιεß τα δικÜ της.
 superOb.i = 10;
 superOb.j = 20;

 System.out.println("Contents of superOb: ");
 superOb.showij();
 System.out.println();

 // H subclass Ýχει πρüσβαση σε üλα τα δημüσια μÝλη
 //της κλÜσης που δημιουργεß.
 subOb.i = 7;
 subOb.j = 8;
 subOb.k = 9;
 System.out.println("Contents of subOb: ");
 subOb.showij();
 subOb.showk();
 System.out.println();

 System.out.println("Sum of i, j, and k in subOb:");
 subOb.sum();
     }
}