Answers for "superclass and subclass in java"

1

SuperClass Java example

// Computer.Java Ths is the Super class from whch laptops and Desktops will inherit the attributes of this class
public abstract class ComputerObject {
	//Here we declare all of the superclass variables that will be used by both Laptops and desktops
	//These are attributes of all computers
	private int usefulLife = 1; 
	private String stockCode = "XXXXXXXX";
	private int purchasingCost = 10000;
	private String staffNumber = "XXXXXXXX";
	private String serialNumber = "ABCDEFG";
	private String cpuType = "Multicore Parallel Threading";
	private int ramAmount = 12;
	  
	/**Default construct Need only be present and we do not need any additional constructors except the one below*/
	 protected ComputerObject() { }
	 
	/**Construct a full Computer object. We receive all of the variable information and place them in the correct private variables using our mutators*/
    /* we use mutators because validation is often done in mutators*/	
	protected ComputerObject(int usefulLife, String stockCode,int purchasingCost,String staffNumber,String serialNumber ,String cpuType, int ramAmount) { 
		setUsfulLife(usefulLife);
		setPurchasingCost(purchasingCost);
		setRamAmount(ramAmount);
		setStockCode(stockCode);
		setStaffNumber(staffNumber);
		setCpuType(cpuType);
		setSerialNumber(serialNumber);
	}
	 
	/**Getter/Accessor method for usefulLife*/ 
	public int getUsfulLife(){
		return usefulLife; 
	 }
	 

	/**Setter/Mutator method for usefulLife*/ 
	public void setUsfulLife(int usefulLife){
		this.usefulLife = usefulLife;
	 } 
	 /**Getter/Accessor method for purchasingCost*/ 
	 public int getPurchasingCost(){
		return purchasingCost; 
	 }
	 
	/**Setter/Mutator method for purchasingCost*/ 
	public void setPurchasingCost(int purchasingCost){
		this.purchasingCost = purchasingCost;
	 } 
	 
	 /**Getter/Accessor method for ramAmount*/ 
	 public int getRamAmount(){
		return ramAmount; 
	 }
	 
	/**Setter/Mutator method for ramAmount*/ 
	public void setRamAmount(int ramAmount){
		this.ramAmount = ramAmount;
	 } 
	 
	 
	 /**Getter/Accessor method for stockCode*/ 
	public String getStockCode(){
		return stockCode; 
	 }
	 
	/**Setter/Mutator method for stockCode*/ 
	public void setStockCode(String stockCode){
		this.stockCode = stockCode; 
	 } 
	 
	 
	  /**Getter/Accessor method for staffNumber*/ 
	public String getStaffNumber(){
		return staffNumber; 
	 }
	 
	/**Setter/Mutator method for staffNumber*/ 
	public void setStaffNumber(String staffNumber){
		this.staffNumber = staffNumber ;
	 } 

	 /**Getter/Accessor method for cpuType*/ 
	public String getCpuType(){
		return cpuType; 
	 }
	 
	/**Setter/Mutator method for cpuType*/ 
	public void setCpuType(String cpuType){
		this.cpuType = cpuType ;
	 } 
	 
	 
	 
	  /**Getter/Accessor method for serialNumber*/ 
	public String getSerialNumber(){
		return serialNumber; 
	 }
	 
	/**Setter/Mutator method for serialNumber*/ 
	public void setSerialNumber(String serialNumber){
		this.serialNumber = serialNumber ;
	 } 
	 
	/**Abstract method AnnualDepreciation*/ 
	//The implimentations of this method can be found in the Laptop and Desktop sub classes
	public abstract double AnnualDepreciation(); 
}
Posted by: Guest on August-16-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

Code answers related to "superclass and subclass in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language