SHARE
TWEET

Untitled

a guest May 24th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Main {
  2.     protected String categorie;
  3.     protected String nume;
  4.  
  5.     public Main() {
  6.         this.nume = "-";
  7.         this.categorie = "-";
  8.     }
  9.  
  10.     public Main(String nume) {
  11.         this.nume = nume;
  12.         categorie = new String("motor");
  13.     }
  14.  
  15.     public String returneazaInfo() {
  16.         return "Sunt un/o " + nume + " si apartin de: " + categorie + " (sunt afisat/a in clasa Main)";
  17.     }
  18.  
  19.     public void metodaSpecificaMain() {
  20.         System.out.println("Metoda Specificata in Main");
  21.     }
  22. }
  23.  
  24. class Second extends Main {  // clasa derivata
  25.  
  26.     public Second(String nume) {
  27.         this.nume = nume;
  28.         categorie = new String("transmisie");
  29.     }
  30.  
  31.     //suprascrierea metodei originale
  32.     @Override
  33.     public String returneazaInfo() {
  34.         return "Numele obiectului este " + nume + " si apartine de: " + categorie;
  35.     }
  36.  
  37.     public void metodaSpecificaSecond() {
  38.         System.out.println("Medoda Specificata in Second");
  39.     }
  40. }
  41.  
  42. class Test {
  43.     public static void main(String a[]) {
  44.         Main c1 = new Main("bujie");
  45.         System.out.println(c1.returneazaInfo());
  46.         c1.metodaSpecificaMain();
  47.         Second v1 = new Second("ambreiaj");
  48.         System.out.println(v1.returneazaInfo());
  49.         v1.metodaSpecificaSecond();
  50.         Second v2 = new Second("roata dintat");
  51.         Main c2 = v2; // upcasting prin initializare
  52.         System.out.println(c2.returneazaInfo());
  53.         c2.metodaSpecificaMain();
  54. //c2 nu are acces la metodaSpecificaSecond
  55. //c2.metodaSpecificaSecond();
  56.         Second v3 = new Second ("brat selectie viteza");
  57.         System.out.println(v3.returneazaInfo());
  58.         // downcasting
  59.         if (v3 instanceof Second) {
  60.             v3 = (Second) (c2);
  61.             System.out.println("Dupa down casting: " + v3.returneazaInfo());
  62.         }
  63.         v3.metodaSpecificaSecond();
  64.         v3.metodaSpecificaMain();// e mostenita
  65.        
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top