I have an interface called Hospital.java
public interface Hospital {
public void operate();
public void doScan();
public void doVaccination();
}
I have an abstract class called StreetHospital.java
public abstract class StreetHospital implements Hospital{
public void operate(){
System.out.println("Street Hospital operate");
}
}
Now I am using another class CommunityHospital.java to extend StreetHospital.java
public class CommunityHospital extends StreetHospital{
public void doScan(){
System.out.println("CommunityHospital doScan");
}
public void doVaccination(){
System.out.println("CommunityHospital doVaccination");
}
public void operate(){
System.out.println("CommunityHospital operate");
}
}
I am creating the CommunityHospital object in my MainHospital class
public class MainHospital {
public static void main(String[] args){
CommunityHospital ch = new CommunityHospital();
ch.operate();
ch.doScan();
ch.doVaccination();
}
}
I am getting this output:
CommunityHospital operate
CommunityHospital doScan
CommunityHospital doVaccination
My question is how do I print "Street Hospital operate" sysout statement in output?
You can't do that.
There can't be two methods in one class with same name and param. Once you override operate() in CommunityHospital , the same mthod in StreetHospital no longer exist.
Add another function to CommunityHospital
with another name say superOperate
like this:
public void superOperate(){
super.operate();
}
and call it from anywhere
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With