0% found this document useful (0 votes)
15 views2 pages

Java Interface Implementation Examples

Uploaded by

Mgm Mallikarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Java Interface Implementation Examples

Uploaded by

Mgm Mallikarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Base interface

interface Vehicle
{
int MAX_SPEED = 120; // Interface variable (public static final)
void start(); // Abstract method
void stop();
}

// Extended interface
interface ElectricVehicle extends Vehicle
{
void charge();
}

// Implementing the extended interface


class Tesla implements ElectricVehicle
{
public void start()
{
[Link]("Tesla is starting");
}

public void stop()


{
[Link]("Tesla is stopping");
}

public void charge()


{
[Link]("Tesla is charging");
}

void showMaxSpeed()
{
// Accessing interface variable
[Link]("Maximum speed of Tesla: " + MAX_SPEED + " km/h");
}
}

// Main class
public class TestAllInterfaces
{
public static void main(String[] args)
{
Tesla myCar = new Tesla();

[Link](); // Method from Vehicle


[Link](); // Method from ElectricVehicle
[Link](); // Method from Vehicle
[Link](); // Access interface variable
}
}

OUTPUT
Tesla is starting
Tesla is charging
Tesla is stopping
Maximum speed of Tesla: 120 km/h
// Base interface
interface Person
{
String ORGANIZATION = "Tungal School"; // Interface variable
void showRole();
}

// Extended interface
interface Teacher extends Person
{
void teach();
}

// Implementing the extended interface


class MathTeacher implements Teacher
{
public void showRole()
{
[Link]("Role: Teacher");
}

public void teach()


{
[Link]("Teaching Math");
}

void showOrganization()
{
// Access interface variable
[Link]("Organization: " + ORGANIZATION);
}
}

// Main class
public class SchoolSystem
{
public static void main(String[] args)
{
MathTeacher teacher = new MathTeacher();
[Link](); // Method from Person
[Link](); // Method from Teacher
[Link](); // Accessing interface variable
}
}

You might also like