Inheritance 8: Object Oriented Programming in Java 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Inheritance 8

If a class defines a finalize method, any subclass finalize method should


call the superclass finalize method as its last action. The following
simplified example illustrates the order in which constructor and finalize
methods are called:
class Point
{
protected int x, y;
public Point()
{
x=0;
y=0;
System.out.println(“Point Cons: “ + this);
}

Object Oriented Programming in Java 1


Inheritance 9
public Point(int a, int b){
x=a; y=b;
System.out.println(“Point Cons” “ + this);
}
protected void finalize(){
System.out.println(“Point finalize: “+this);
}
public String toString(){
return “[“ + x+ “, ” + y + “]”;}
}
}
class Circle extends Point{
protected double radius;
public Circle() {
setRadius(0);
System.out.println(“Circle Cons: “ + this);
}
Object Oriented Programming in Java 2
Inheritance 10
public Circle(double r, int a, int b){
super(a, b);
radius=r;
System.out.println(“Circle Cons: “ + this);
}
public String toString(){
return “Center= “ + “[“ x + “, “ + y +
“; Radius= “ + radius;
}
protected void finalize(){
System.out.println(“Circle finalize:“+this);
super.finalize();
}
public String toString(){ return “Center=“ +
super.toString() + “; Radius=“+radius;
}
}
Object Oriented Programming in Java 3
Inheritance 11
The keyword super can be used to access parent class members
public class Test{
public static void main(String[] args){
Circle c1, c2;
c1=new Circle(4.5,72,29);
c2=new Circle(10,5,5);
c1=null; c2=null;
System.gc(); }
}// the output:
Point Cons Center=[72, 29]; Radius=0.0
Circle Cons: Center=[72, 29]; Radius=4.5
Point Cons Center=[5, 5]; Radius=0.0
Circle Cons: Center=[5, 5]; Radius=10.0
Circle finalize: Center=[72, 29]; Radius=4.5
Point finalize: Center=[72, 29]; Radius=4.5
Circle finalize: Center=[5, 5]; Radius=10.0
Point finalize: Center=[5, 5]; Radius=10.0

Object Oriented Programming in Java 4


Inheritance 12
In Java, every class has a superclass called Object; the class Object
has a number of methods which are inherited by any class you create.
The method toString() is one such method which returns a textual
representation of the object. You should always try to override this method
as in this example. Outputting the this reference of an object, invokes the
toString() method. Also, if you try to output a reference object you
implicitly invoke the toString() method:

System.out.println(c1);
In this example, after we finish with c1 and c2 objects, we set them to null
to indicate that they are no longer needed and then ask that the system’s
garbage collector be called with the call System.gc(). Java guarantees
that before the garbage collector runs to reclaim the space for each
object, the finalize method for each object is called but it does not
guarantee the order in which objects will be garbage collected.

Object Oriented Programming in Java 5


Polymorphism

You can have an inheritance hierarchy where a number of classes extend


from a parent class:
Person ---> Students ---> Undergraduate ---> …
This is called an inheritance hierarchy. In OOP, when you invoke a
method on an object, you send that object a message. When a method is
applied to an object of class in an inheritance hierarchy the following
occurs:

- the class (subclass) checks whether or not it has a method with


that name and with exactly the same parameters. If so, it uses it.
If not:
- the parent class becomes responsible for handling the message
and looks for a method with that signature. If so, it uses it.
This process continues until a match is found. If no match is found, a
compile-time error is reported.

Object Oriented Programming in Java 6


Polymorphism 2

Remember that inheritance defines the is-a relationship. The is-a


relationship allows subclass objects to be treated as superclass objects,
because a subclass object IS A superclass object. Because a student IS A
person.

Method-overriding refers to the idea of having a subclass contain a method


with the same signature as that of a method in its parent class. In this
case, the subclass method overrides the parent class method.

An objects ability to decide what method to apply to itself, depending on


where it is in the inheritance hierarchy, is called polymorphism. What
makes polymorphism work is late-binding, which means that the compiler
does not generate the code to call a method at compile-time; instead the
compiler generates code to calculate which method to call, using type
information from the object.

Object Oriented Programming in Java 7


Polymorphism 3
In the following example, we see how polymorphism is achieved in java:

import java.util.Date;
public class ManagerTest
{
public static void main(String[] args)
{
Manager boss = new Manager("Carl Cracker",
75000, new Date(1987,12,15));
boss.setSecretaryName("Harry Hacker");
System.out.println(boss.Name());
Employee[] staff = new Employee[3];

staff[0] = boss;
staff[1] = new Employee("Harry Hacker", 35000,
new Date(1989,10,1));
staff[2] = new Employee("Tony Tester", 38000,
new Date(1990,3,15));

Object Oriented Programming in Java 8


Polymorphism 4
int i;
for (i = 0; i < 3; i++)
staff[i].raiseSalary(5);
for (i = 0; i < 3; i++)
staff[i].print();
System.out.println("The department secretary is
" + boss.getSecretaryName());
}
}
import java.util.Date;
class Employee
{
public Employee(String n, double s, Date d)
{ name = n;
salary = s;
hireDay = d;
}
public void print()
{ System.out.println(name + " " + salary + " "
+ hireYear()); }

Object Oriented Programming in Java 9


Polymorphism 5
public void raiseSalary(double byPercent)
{
salary *= 1 + byPercent / 100;
}
public int hireYear()
{
return hireDay.getYear();
}
String Name() { return name;}

private String name;


private double salary;
private Date hireDay;
}

Object Oriented Programming in Java 10


Polymorphism 6
import java.util.Date;
class Manager extends Employee
{
public Manager(String n, double s, Date d)
{
super(n, s, d);
secretaryName = "";
}
public void raiseSalary(double byPercent)
{ // add 1/2% bonus for every year of service
Date today = new Date();
double bonus = 0.5 * (today.getYear()+1900 -
hireYear());
super.raiseSalary(byPercent + bonus);
}

Object Oriented Programming in Java 11


Polymorphism 6
public void setSecretaryName(String n)
{
secretaryName = n;
}
public String getSecretaryName()
{
return secretaryName;
}

private String secretaryName;


}

Object Oriented Programming in Java 12


Polymorphism 7
objects, 2 of type employee and one of type Manager. Method
raiseSalary() is overridden in subclass Manager. Then we create an
array of type Employee and store the 3 objects in the array, even the
object boss of type Manager.

Then we invoke the method raiseSalary() on all the array elements.


Since this method is overridden, its correct version is applied to each
individual element.

Also note that since salary is declared private Manager’s


raiseSalary() cannot directly access it; it uses the public interface
methods of the superclass Employee to access it, even though every
Manager object has a salary field. We could have declared it as
protected and bypass this restriction.

If you don’t want a class to be inherited or a method overridden, declare


them as final. You may do this for efficiency (inlining) and safety.

Object Oriented Programming in Java 13

You might also like