Answers for "what is super class 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
9

super keyword in java

Variables and methods of super class can
be overridden in subclass. 
In case of overriding , a subclass
object call its own variables and methods.
Subclass cannot  access the variables and
methods of superclass because the overridden
variables or methods hides the 
methods and variables of super class.
But still java provides a way to access 
super class members even if 
its members are overridden. Super is
used to access superclass variables, methods, constructors.
Super can be used in two forms :
1) First form is for calling super class constructor.
2) Second one is to call super class variables,methods.
Super if present must be the first statement.
Posted by: Guest on January-05-2021
0

how to use super class method in java

class Parentclass
{
   //no-arg constructor
   Parentclass(){
	System.out.println("no-arg constructor of parent class");
   }
   //arg or parameterized constructor
   Parentclass(String str){
	System.out.println("parameterized constructor of parent class");
   }
}
class Subclass extends Parentclass
{
   Subclass(){
       /* super() must be added to the first statement of constructor 
	* otherwise you will get a compilation error. Another important 
	* point to note is that when we explicitly use super in constructor
	* the compiler doesn't invoke the parent constructor automatically.
	*/
	super("Hahaha");
	System.out.println("Constructor of child class");

   }
   void display(){
	System.out.println("Hello");
   }
   public static void main(String args[]){		
	Subclass obj= new Subclass();
	obj.display();	 
   }
}
Posted by: Guest on May-31-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language