Expected Understanding: Interface, access modifiers, Method Overriding
1) Create a package called tamilnadu.chennai
2) Create an interface ‘TrafficRules’ under this package
3) Make sure this interface is public interface
– Add variable String trafficCommisssioner = “Kavin”;
– Add below methods
– void goByDieselVehicle();
– void goByBicycle();
4) Create class called ‘CommonManInChennai’ with main method in the same package tamilnadu.chennai
– Implement interface ‘TrafficRulesChennai’
– Create instance for this class and access all the methods
5) Now, create another package called ‘india.newDelhi’
6) Create an interface ‘TrafficRulesDelhi’ under this package
7) Make sure this interface is not public interface – it should be default interface
– Add variable String trafficCommisssioner = “Navin”;
– Add below methods
– void dontGoByDieselVehicle();
– void goByBicycle();
8) Create class called ‘CommonManInDelhi’ with main method in the same package india.newDelhi
– Implement interface ‘TrafficRulesDelhi’
– Create instance for this class and access all the methods
– Now, implement interface ‘TrafficRulesChennai’ also.
– Add unimplemented methods
– Access all the methods and observe the difference.
programe-
package Tamilnadu.chennai;
public interface TrafficRules {
String trafficCommisssioner = "Kavin";
void goByDieselVehicle();
void goByBicycle();
}
package Tamilnadu.chennai;
public class CommonManInChenna implements TrafficRules{
public static void main(String[] args) {
// TODO Auto-generated method stub
CommonManInChenna Gugan = new CommonManInChenna();
Gugan.goByBicycle();
Gugan.goByDieselVehicle();
}
@Override
public void goByDieselVehicle() {
System.out.println("Diesel refil");
// TODO Auto-generated method stub
}
@Override
public void goByBicycle() {
System.out.println("Go bicycle");
// TODO Auto-generated method stub
}
}
package india.newDelhi;
public interface TrafficRulesDelhi {
String trafficCommisssioner = "Navin";
void dontGoByDieselVehicle();
void goByBicycle();
}
package india.newDelhi;
public class CommonManInDelhi implements TrafficRulesDelhi {
public static void main(String[] args) {
CommonManInDelhi cmd =new CommonManInDelhi();
cmd.dontGoByDieselVehicle();
cmd.goByBicycle();
}
@Override
public void dontGoByDieselVehicle() {
System.out.println("Dont go by diesel vehicle");
// TODO Auto-generated method stub
}
@Override
public void goByBicycle()
{
System.out.println("go By Cycle");
}
// TODO Auto-generated method stub
}
output --
Go bicycle
Diesel refil
output --
Dont go by diesel vehicle
go By Cycle
Top comments (0)