Answers for "use of inheritance in java"

1

inheritance in java

Builds relations between classes, main purpose: 
create a TEST BASE CLAS and use it in other classes.
Inheritance allows a class to inherit properties 
(objects, variables, methods) from another source (class or interface). 
Allows code reusability and easy to maintain.
SUPER CLASS (also known as parent or base class): 
is the class where the fields are being inherited from. 
SUB CLASS (also known as the child or derived class): 
is the class inheriting the properties
INHERITANCE EXAMPLE
In my framework I have a TestBase class where 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. 
My framework follow POM and some pages have similar actions, 
so I can easily use those similar actions and fields 
by inheriting them from the ready classes.

  Example: Base Page Class and Test Base Class. 
These 2 class are being inherited from so many different classes. 
For Example; In Pages Package, Base Page Class is being extended 
by all the class by Base Package. So that Constructor 
can be automatically be called in the sub classes. 
That way you will be able to locate the elements 
by using same driver. 
Test Base Class can also be inheritance. 
One driver, TestNG framework one before method to 
setup browser and reuse it every single test class it. 
By inheriting them to other test class. These 2 class are 
abstract class and meant to be inherited to other classes. 
We are not creating any object in these 2 class. 
These class is super class. Comes from Selenium library WebDriver, 
takes Screenshots, javascriptexecuter these are interface. 
List and Set also interface. You cannot create object in interface. 
They are only being reference. These are also example for abstraction.
Posted by: Guest on June-17-2021
2

inheritance in java

Inheritance in Java is a mechanism in which one object acquires 
all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
Posted by: Guest on May-03-2021
1

java inheritance

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);
  }
}
Posted by: Guest on November-28-2020
0

inheritance in java

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.
Posted by: Guest on November-28-2020

Code answers related to "use of inheritance in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language