guest11
0
Q:

inheritance in java

class Vehicle {
  protected String brand = "Ford";        // Vehicle attribute
  public void honk() {                    // Vehicle method
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle {
  private String modelName = "Mustang";    // Car attribute
  public static void main(String[] args) {

    // Create a myCar object
    Car myCar = new Car();

    // Call the honk() method (from the Vehicle class) on the myCar object
    myCar.honk();

    // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
    System.out.println(myCar.brand + " " + myCar.modelName);
  }
}

/* output:
Tuut, tuut!
Ford Mustang
*/
14
// Hybrid inheritance in java
class A 
{
   public void display()
   {
      System.out.println("class A display method");
   }
}
interface B 
{
   public void print();
}
interface C
{
   public void print();
}
public class HybridInheritanceDemo extends A implements B, C
{
   public void print()
   {
      System.out.println("implementing print method");
   }
   public void show()
   {
      System.out.println("show method of class HybridInheritanceDemo");
   }
   public static void main(String[] args) 
   {
      HybridInheritanceDemo obj = new HybridInheritanceDemo();
      obj.show();
      obj.print();
   }
}
1
// single inheritance example
import java.util.*;
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion is roaring.");
   }
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
      obj.roar();
   }
}
0
// Multiple inheritance in java
interface M
{
   public void helloWorld();
}
interface N
{
   public void helloWorld();
}
// implementing interfaces
public class MultipleInheritanceExample implements M, N
{
   public void helloWorld()
   {
      System.out.println("helloWorld() method");
   }
   public static void main(String[] args) 
   {
      MultipleInheritanceExample obj = new MultipleInheritanceExample();
      obj.helloWorld();
   }
}
1
// Hierarchical inheritance in java
class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Dog extends Animal
{
   void bark()
   {
      System.out.println("dog barking");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Dog obj3 = new Dog();
      obj3.eating();
      obj3.bark();
   }
}
1
// Multilevel inheritance in java
class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Cub extends Lion
{
   void born()
   {
      System.out.println("cub drinking milk");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Cub obj3 = new Cub();
      obj3.eating();
      obj3.roar();
      obj3.born();
   }
}
1
// super class
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
// subclass
class Lion extends Animal
{
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
   }
}
2
it is used to define relationship between two class, 
which a child class occurs all the properties and behaviours of a parent class. 
Provides code reusability.
Ex: in my framework I have a TestBase class which I store 
all my reusable code and methods. My test execution classes and 
elements classes will extend the TestBase in order to reuse the code.
0
abstract class Pesan {
   public void success() {
     System.out.println("Mobil Berhasil Dibeli");
   }
   
   public void error() {
     System.out.println("Uang Anda Tidak Cukup");
   }
}
 
class Car extends Pesan {
    protected String nama = "toyota supra";
    protected String warna = "merah";
    protected int harga = 2000000000;
    protected String brand = "toyota";
}
 
class ShowRoom extends Car {
  protected String namaShowroom = "Catur Sentosa Raya";
  protected String alamatShowroom = "Jl.siliwangin kec pancoranmas kota depok 16436";
}
 
class Pembeli extends ShowRoom {
    protected String namaPembeli = "anto jayabaya";
    protected String alamatPembeli = "jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";
    protected int saldoPembeli = 50000000;
}
 
class BeliMobil extends Pembeli {
 
 public BeliMobil(String nama, String warna, int harga, String brand, String nsr, String asr, String np, String ap, int sdp) {
    super();
    
    super.nama = nama;
    super.warna = warna;
    super.harga = harga;
    super.brand = brand;
    super.namaShowroom = nsr;
    super.alamatShowroom = asr;
    super.namaPembeli = np;
    super.alamatPembeli = ap;
    super.saldoPembeli = sdp;
 }
    
void getResult(String nama, String warna, int harga, String brand, String np, String ap) {
    
     if(super.harga > super.saldoPembeli) {
       System.out.println("=======================");
       super.error();
       System.out.println("=======================");
     } else {
       System.out.println("=======================");
       super.success();
       System.out.println("=======================");
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Jenis Mobil");
       System.out.println("=======================");
       System.out.println("");
       System.out.println("Nama Mobil:" + nama);
       System.out.println("Warna Mobil:" + warna);
       System.out.println("Harga Mobil:" + harga);
       System.out.println("Brand Mobil:" + brand);
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Nama Pembeli Mobil");
       System.out.println("=======================");
       System.out.println("Nama Pembeli:" + np);
       System.out.println("Nama Pembeli:" + ap);
     }
  }
    
 public static void main(String[] args) {
   BeliMobil beli = new BeliMobil("avanza", "hitam", 128000000, "toyota", "Jaya Mobil", "Jakarta", "Anton", "Depok", 228000000);
   beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);
  }
}
 
0

New to Communities?

Join the community