Transcript lecture7

Object Oriented Programming
in
Java
Habib Rostami
Lecture 7
Today’s Presentation

Inheritance
Constructor and field initialization
Abstract classes
Generalization
• Classes may have simmilar responsibilities
• identical methods
• common things in the superclass (generalized)
• Specific methods remain in the subclass
• concept: generalization/specialization
• inhertance – implementation element (extends)
Inheritance
• Subclasses inherit methods (and attributes) from a superclass
• Subclass can redefine (override) inherited methods
• Subclass can have additional (its own) methods and attributes
• Inheritance is used to organize class hierarchy
• Eliminates unnecessary duplication
• base class - superclass
• derived class - subclass
• single and multiple inheritnece
(Java supports:
single inheritance beetween classes)
Person
Inhertiance
+izracunStarosti()
class Person {
…
calculateAge (){…}
}
class Student extends Person {
…
hasPassedExam (Exam e){…}
}
Student
class Professor extends Person {
…
getCoursesTaught(){…}
}
all classes are derived from java.lang.Object
Professor
Object
Differentiate!
Employee
Student
Person
PartTimeStudent
Student
multiple inheritance
Depth of Inheritance
Abstract class
Abstract class can not be instatiated
Shape
{abstract}
+move(dx,dy)
+draw (){abstract}
Rectangle
Circle
The Object Class
• The class Object is parent of all classes
public class point {
}
Equals
public class point extends java.lang.Object{
}
The Object Class
• The Object class is the root of all Java
classes.
• The equals() method compares the
contents of two objects.
• The toString() method returns a string
representation of the object.
• The clone() method copy objects
The Object Class, cont.
The equals() method compares the
contents of two objects. The default
implementation of the equals
method in the Object class is
as follows:
public boolean equals(Object obj)
{
return (this == obj);
}
The Object Class, cont.
• The toString() method returns a string
representation of the object. The default
implementation returns a string
consisting of a class name of
which the object is an instance,
the at sign (@), and a number
representing this object.
The clone() method copy objects
The Object Class, cont.
To create a new object with
separate memory space, you need to
use the clone() method, as follows:
newObject = someObject.clone();
NOTE: Not all objects can be
cloned. For an object to be
cloneable, its class must implement
the java.lang.Cloneable interface.
Interfaces are introduced in next
session.
Inheritance and initialization
Inheritance and initialization
Inheritance and initialization
•
Sequence of Initialization
1. Parent is initialized
2. Instance variables is initialized
3. Constructor is called
Calling one constructor in
another
Explicit call to super constructor
Caution
•
You can just call one constructor in a
constructor
– You may call this(..) or super(…) but not
both together.
Abstract class Shape
abstract class Shape
{ abstract double area ();}
class Triangle extends Shape {
double b, h;
Triangle (double b, double h)
{ this.b=b; this.h=h;}
double area () {return 0.5*b*h;}
}
class Square extends Shape {
double a;
Square (double a) { this.a=a;}
double area () {return 0.5*a*a;}
}
Polymorphism
• Overloading and Overriding
• Overloading means having different
methods with the same name
• Overriding means changing behavior of a
super class method in its subclass