0% found this document useful (0 votes)
5 views17 pages

Java Interfaces and Inner Classes Guide

The document provides an overview of interfaces and inner classes in Java, highlighting their structure and usage. It includes examples of the Comparable interface for sorting Employee objects, object cloning with the Cloneable interface, and the implementation of inner and static inner classes. Additionally, it demonstrates how to create and use nested classes effectively within Java applications.

Uploaded by

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

Java Interfaces and Inner Classes Guide

The document provides an overview of interfaces and inner classes in Java, highlighting their structure and usage. It includes examples of the Comparable interface for sorting Employee objects, object cloning with the Cloneable interface, and the implementation of inner and static inner classes. Additionally, it demonstrates how to create and use nested classes effectively within Java applications.

Uploaded by

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

INTERFACES

and
INNER CLASSES
Interfaces
• Blue print for class structure
• Exists to specify what to be done and not
how to do
• Useful for multiple inheritance
• Contains set of requirements
Comparable interface
• Defined in Arrays class
public interface Comparable
{
int compareTo(Object other);
}
public interface Comparable <T>
{
int compareTo(T other);
}
Example:
import [Link].*;

public class EmployeeSortTest


{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];

staff[0] = new Employee("Harry Hacker", 35000);


staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000);

[Link](staff);

// print out information about all Employee objects


for (Employee e : staff)
[Link]("name=" + [Link]() + ",salary=" + [Link]());
}
}
class Employee implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
}

public String getName()


{
return name;
}

public double getSalary()


{
return salary;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

/**
Compares employees by salary
@param other another Employee object
@return a negative value if this employee has a lower
salary than otherObject, 0 if the salaries are the same,
a positive value otherwise
*/
public int compareTo(Employee other)
{
if (salary < [Link]) return -1;
if (salary > [Link]) return 1;
return 0;
}

private String name;


private double salary;
Object cloning
• Required in deep object copy
• Problem arises when there are referencing
objects
• cloneable interface need to be
implemented
Example:
import [Link].*;

public class CloneTest


{
public static void main(String[] args)
{
try
{
Employee original = new Employee("John Q. Public", 50000);
[Link](2000, 1, 1);
Employee copy = [Link]();
[Link](10);
[Link](2002, 12, 31);
[Link]("original=" + original);
[Link]("copy=" + copy);
}
catch (CloneNotSupportedException e)
{
[Link]();
}
}
}
class Employee implements Cloneable
{
public Employee(String n, double s)
{
name = n;
salary = s;
}

public Employee clone() throws CloneNotSupportedException


{
// call [Link]()
Employee cloned = (Employee)[Link]();

// clone mutable fields


[Link] = (Date)[Link]();

return cloned;
}
/**
Set the hire day to a given date
@param year the year of the hire day
@param month the month of the hire day
@param day the day of the hire day
*/
public void setHireDay(int year, int month, int day)
{
hireDay = new GregorianCalendar(year, month - 1, day).getTime();
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

public String toString()


{
return "Employee[name=" + name
+ ",salary=" + salary
+ ",hireDay=" + hireDay
+ "]";
}

private String name;


private double salary;
private Date hireDay;
}
Inner classes
• Defined within another class
• Can be hidden from other classes
• Nested classes in …
Example:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];

public class InnerClassTest


{
public static void main(String[] args)
{
TalkingClock clock = new TalkingClock(1000, true);
[Link]();

// keep program running until user selects "Ok"


[Link](null, "Quit program?");
[Link](0);
}
}
class TalkingClock
{
/**
Constructs a talking clock
@param interval the interval between messages (in milliseconds)
@param beep true if the clock should beep
*/
public TalkingClock(int interval, boolean beep)
{
[Link] = interval;
[Link] = beep;
}

/**
Starts the clock.
*/
public void start()
{
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
[Link]();
}
private int interval;
private boolean beep;

private class TimePrinter implements ActionListener


{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
[Link]("At the tone, the time is " + now);
if (beep) [Link]().beep();
}
}
}
Static Inner classes

public class StaticInnerClassTest


{
public static void main(String[] args)
{
double[] d = new double[20];
for (int i = 0; i < [Link]; i++)
d[i] = 100 * [Link]();
[Link] p = [Link](d);
[Link]("min = " + [Link]());
[Link]("max = " + [Link]());
}
}
class ArrayAlg
{
/**
A pair of floating point numbers
*/
public static class Pair
{
/**
Constructs a pair from two floating point numbers
@param f the first number
@param s the second number
*/
public Pair(double f, double s)
{
first = f;
second = s;
}

/**
Returns the first number of the pair
@return the first number
*/
public double getFirst()
{
return first;
}
public double getSecond()
{
return second;
}

private double first;


private double second;
}

/**
Computes both the minimum and the maximum of an array
@param values an array of floating point numbers
@return a pair whose first element is the minimum and whose
second element is the maximum
*/
public static Pair minmax(double[] values)
{
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
return new Pair(min, max);
}
}

You might also like