Beruflich Dokumente
Kultur Dokumente
...
SuperClass superClass1 = new SuperClass();
SuperClass superClass2 = new SubClass();
...
Hence, at compile time the compiler can't be sure if the call to the
method 'someMethod()' on these references actually refer to which
version of the method - the super class version or the sub class
version.
Thus, we see that dynamic binding in Java simply binds the method
calls (inherited methods only as they can be overriden in a sub class
and hence compiler may not be sure of which version of the method to
call) based on the actual object type and not on the declared type of
the object reference.
• The Object class sits at the top of the class hierarchy tree in
the Java development environment.
• Every class in the Java system is a descendent (direct or
indirect) of the Object class.
• The Object class defines the basic state and behavior that all
objects must have, such as the ability to compare oneself to
another object, to convert to a string, to wait on a condition
variable, to notify other objects that a condition variable has
changed, and to return the object's class.
if (one.equals(anotherOne))
System.out.println("objects are equal");
This code will display objects are equal even
though one and anotherOne reference two different, distinct objects.
They are considered equal because they contain the same integer
value.
The following method gets and displays the class name of an object:
Copying
Original=
Copy= Employee
• In order to copy an old object into another new object which should
not refer to the old object but the values has to be copied to a new
reference then use the clone method
Employee copy=original.clone();
Copy.raiseSalary(10);//OK—original unchanged
employee
Copy=
employee
Shallow Cloning
• Shallow cloning means field by field copying.
• If each and every field is numbered or basic types their is no
problem.
• If the fields in turn are objects of another class, then field by field
copying will make the original and copy objects to refer to the same
sub-objects.
• This happens because the object class has no information about the
fields of the class which calls the clone method.
• If the subobject that is shared between the original and the shallow
clone is immunable, then the sharing is safe.
• This certainly happens if the subobjects belong to an immunable
class, such as string, otherwise the subobject may simply remain
constant throughout the lifetime of the object (no method changing
the value or yielding a reference)
• Consider the Employee class with fields’ name, salary and
hireday.the shallow copy is shown
Copying
Employee
Original= name
string
salary
hireDay
Employee
Copy=
name date
salary
hireDay
• But mostly the sub objects are mutable. So must go for deep copy.
• In the above example,the hireDay field is an object of Date class
which is a mutable class.
• An example-Employee class with two fields-name and salary.
• As hireDay is a mutable object, will be included when talking about
deep cloning
Deep Cloning
Example
import java.util.*;
public class Employee implements Cloneable
{
public Employee()
{
salary=500.00;
name=”xxx”;
}
public Employee (String n,double s,int y,int m,int d);
{
salary=s;
name=n;
hireday=newDate(new GregorianCalendar(y,m-1,d).getTimeInMillis());
}
public void raiseSalary(int percent)
{
this.salary=salary+salary*percent/100;
}
public String getName()
{
return(this.name);
}
public double getSalary()
{
return(this.salary);
}
public void setDate()
{
hireday.setTime(new GregorianCalender(2007,7,20).getTimeMillis());
}
public Date getDate()
{
return hireday;
}
public Employee clone() throws CloneNotSupportedException
{
Employee cloned=((Employee)super.clone());
cloned.hireday=(Date)hireday.clone();
return cloned;
}
private String name;
private double salary;
private Date hireday;
}
public class EmployeeTest
{
public static void main(String args[])
{
Employee original=new Employee(“sam”,2000.00,2010,7,23);
try
{
Employee copy=original.clone();
System.out.println(“Before Changing”);
System.out.println(“original object : Name=”+copy.getName()
+”sal=”+original.getSalary()+”HireDay=”+original.getDate());
System.out.println(“Copy Object:Name”+copy.getName()
+”sal=”+copy.getSalary()+”HireDay=”+copy.getDate());
}
catch(CloneNotSupportedException e)
{
System.out.println(e);
}
}
}
Inner classes
An inner class is a class that is defined inside another class. There are
three reasons to have a inner class:
• Inner class methods can access the data from the scope in which
they are defined-including data that would otherwise be private
• inner classes can be hidden from other classes in the same
package
• anonymous inner classes are handy when you want to define
callbacks without writing a lot of code
There are several Inner Classes.They are,
1. Simple inner class-that acess an instance field of its outer
class
2. Local inner classes that can access local variable of the
enclosing scope
3. Anonymouus inner classes and show how they are commonly
used to implement callback
4. Static inner classes can be used for nested helper classes
inner outer
field1
outer=
field2
Example
public class SimpleInnerClassTest
{
public static void mian(String args[])
{
OuterClass obj=new OuterClass();
obj.outerDisplay();
}
}
class OuterClass
{
private int x=1;
public void outer Display()
{
InnerClass inn=new InnerClass();
inn.DisplayOne();
}
class InnerClass
{
Public void DisplayOne()
{
System.out.println(“This is a simpler Inner Class “);
System.out.println(“the value of variable x is”+x);
}
}
}
Sample input & output
This is simple inner class
The value of variable X is 1
Example program
public class LocalInnerClassTest
{
public static void main(String args[])
{
OuterClass obj=new OuterClass();
obj.outerDisplay();
}
}
class OuterClass
{
private int x=1;
public void outerDisplay()
{
class InnerClass
{
public void DisplayOne()
{
System.out.println(“this is a local inner class”);
System.out.println(“the vqlue and variable X is”+x);
}
}
InnerClass inn=new InnerClass();
inn.DisplayOne();
}
}
In general,the syntax is
Example program
public class ClassAnonTest
{
public static void main(String args[])
{
OuterClassobj=newOuterClass();
obj.displayAnonymous();
}
}
class OuterClass
{
private int x=1;
public void displayAnonymous()
{
Anon inAnon=new Anon)
{
public void displayAnon()
{
System.out.println(“This is Anonmyous Inner Class”);
System.out.println(“The value of x=”+x);
}
}
inAnon.displayAnon();
}
}
interface Anon
{
public void displayAnon();
}
Reflection
• The reflection library gives a very rich and elaborate toolset to
write programs that manipulates Java code dynamically.
• this features is heavily used in JavaBeans,the components
architecture for Java.
• Aprogram that can analyze the capabilities of classes is called
reflective.
Class c=e.getClass().getName();
String className=”java.util.Date”;
Class cl=Class.forName(className);
Class cl1=Date.class;
1.Field
2.Method,and
3.Constructor in the java.lang.reflect package describe the
fields,methods,and constructor of a class, respectively
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionExample {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}