DEV Community

hema latha
hema latha

Posted on

1

Assignment

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      
}
Enter fullscreen mode Exit fullscreen mode

}
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

}
Enter fullscreen mode Exit fullscreen mode

output --

Go bicycle
Diesel refil

output --
Dont go by diesel vehicle
go By Cycle

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more