Professional Documents
Culture Documents
1. Justification
Object Oriented Programming (OOP) is an evolution of structured
programming.
Advantages:
development of software project is more easier
code reusing
divide the team work
program debugging
program maintenance, etc.
Characteristics:
Everything is an object or a class
A program is a bunch of objects telling each other what to do by sending
messages. Classes are accepted too.
Each object has its own memory made up of other objects
Every object has a type (class)
All objects of a particular type can receive the same messages.
An object has an interface=the request you can make of an object
the methods it implements.
The objects can be manipulated using their references.
2. Objects
An object is a block of data and operations. The executions of operations
depend on the objects states.
The object has variables (attributes) instantiated and methods to manipulate
them, composing an atomic unit.
Usually only the services are visible. The implementations are concealed
partially or totally. Data and methods are encapsulated.
method = a way to do something = a service provided for other objects
Each object has a memory area. It is allocated at the object creation
(instantiation). The variables storied in the object memory area are named
instance variables. (variabile de instan)
Encapsulation = the
procedure to pack the
objects data and methods
together. (ncapsulare)
Object
Message
An object has a
visible part and a hidden
part. The encapsulation
conceals the object
implementation and
forbid the access to data
and methods that are not
public.
Operation
solicitations are
method calls = send
messages
Method
Answer
Data
3. Classes
Class1
+at1: int
+at2: float
+met1(): int
+met(): float
Class2
+at3: double
+at4: Class1
+met3(): double
+met4(): Class1
Class defines the object internal structure (attributes or properties) and describes its
functionality (behavior) by methods
A class is a definition of properties common to a group of objects. class members
Tiberiu Leia: Software Engineering Object Oriented Programming
class members
ClassName
Data
ClassName = identifier
fields
Message
Package
PackageName.ClassName
setData
methods
getData
Methods
Answer
Constructor
s
Method
main
General structure of a class
Tiberiu Leia: Software Engineering Object Oriented Programming
Attributes of a class are represented by instances of property that are owned by the class. Some
of these attributes may represent the navigable ends of binary associations.
Objects of a class must contain values for each attribute that is a member of that class.
10
Inheritance (motenire)
superclass subclass
subclassing
Ex. class X extends Y
class SubClass extends SuperClass
C++ multiple inheritance
Java simple inheritance only one class
can be inherited
SuperClass
+Attribute1
+Attribute2
+Operation1()
+Operation2()
immediate inherited
SubClass
class hierarchies
+Attribute3
+Attribute4
+Operation1()
+Operation3()
+Operation4()
11
Object
attributes
methods
Class1
attributes
methods
Class2
attributes
methods
Class3
attributes
methods
12
13
Class definition
[modifiersList] class ClassName [extends SuperclassName] [implements
interfaceList]
{
ClassBody
}
[] optional.
modifierList keywords: public, abstract, final, etc.
Class Members
Member domain
Tiberiu Leia: Software Engineering Object Oriented Programming
14
15
4. Fields
Class variables and instance variables (variabile de clas)
A variable is an item for data named by an identifier.
Class variables = static variables
They have static modifier.
Example:
static int i=5;//class variable
static Type variableName;
Reference manner:
ClassName.variableName
objectReference.variableName It is not accepted by all compilers for static variables!
Instance Variables (variabile de instan)
Example:
Tiberiu Leia: Software Engineering Object Oriented Programming
16
17
Attribute declaration
Attributes are primitive data or reference data (constructed from class or arrays).
[modifiersList] DataType variableDeclarationList;
modifierList contains:
18
DataType contains:
19
5. Methods
method = function service or behavior
Method declaration
implementation = { //sequence of instructions }
A method has: definition and body
The method definition has:
return type of the variable may be void
name
list of arguments - may be empty.
method signature = returnType+name+parameterList
20
Method declaration:
[modifierList] ReturnType methodName([argumentList]) [throws exceptionList]
{
methodBody;
}
21
modifierList:
22
In a class can be two or more methods with the same name. They have to differ by:
number of parameters or their type
23
Example:
public class ClassName {
static void met1() {
System.out.prinln("Method1");
}
static void met2() {
System.out.println("Method 2");
}
public static void main(String args[]) {
met1();met2();
}
}
24
Method overwriting
Method signature
Overwriting a method in a subclass the method from the superclass is concealed in the
subclass by the new implementation
public class SuperClass {
//attributes .
static void met1() {
System.out.prinln("Method1");
}
static void met2() {
System.out.println("Method 2");
}
}
25
26
27
6. Constructors
A class can have one or more constructors.
A constructor is a kind of specialized method that is called at the object creation. It
initializes some object data or creates some new reference data.
In a class can be more constructors with different numbers of arguments or different types
of arguments.
default constructor it has no arguments
28
Constructor declaration
The name must be the same of the class name. It cannot return any value (including void).
[modifierList] ClassName([parameterList]) [throws exceptionList]
modifierList: private, protected, public, abstract, static, final, native, synchronized.
The constructors body can contain as a first line:
this(parameterList)
or
super(parameterList)
Constructor signature
If a class has not any declaration of a constructor, the default constructor is used.
29
30
Class Extension
The constructor overwriting.
Initialization:
Primitive type:
boolean
char
byte
short
int
long
float
double
Implicit value
false
null
0L
0.0F
0.0d
31
Initializers (iniializatori)
String[] tablou=new String[] {
nume1, nume2, nume3 };
What does perform the following instruction?
System.out.println(tablou[0]);
32
33
int a=1;
{
int a=2;
a= ;// confusion which a?
}
}
34
35
36
Example:
System.out.println(str);
System
System.out is an instance of PrintStream.
method println()
Operatorul instanceof
boolean b= referinta instanceof NumeClasa;
b takes true if referinta is an instance of the class NumeClasa.
37
Distruction
String s=new String(); //
............................
s=null; //the object is no anymore referred by s
System.gc(); // call the execution immediately of the garbage collector
Execution of an application
public void static main(String[] args) { ... }
The class that implements the method main() is not automatically instantiated.
How can be used non static variable declared outside main method?
What kind of variables can be used in static methods?
Tiberiu Leia: Software Engineering Object Oriented Programming
38
Object Class
Constructor:
public Object()
It is in the package java.lang.
39
40
41
42
43
Problems:
1) Fill the Complex class with others methods.
2) Transform the program in one that works with complex numbers of the type float.
Suggestions:
float x=(float) 3.4;
double y=(double) x;
float z=(float) y;
44
Class Diagram
TestComplexe
+main(String[]): void
ClassName
+attribute1
+attribute2: data_type
+attribute3: data_type = i_value
+operation1()
+operation(arg_list): result_type
Complex
+a: double = 0
+b: double = 0
+add2(Complex, Complex): Complex
+putAdd(Complex): void
+getAdd(Complex): Complex
+toString(): String
A Class diagram shows the existence of classes and relationships in a logical view of a system.
45
TestComplexe
+main(String[]): void
Complex
+a: double = 0
+b: double = 0
+add2(Complex, Complex): Complex
+putAdd(Complex): void
+getAdd(Complex): Complex
+toString(): String
System
+out: PrintStream
+print(String): void
+println(String): void
46
9. Inheritance + modifiers
The procedures used to construct classes from other classes are extension, composition or
aggregation.
Inheritance is a procedure to transmit the behavior and attributes of a class to
another one.
The class that inherits has the behavior and the attributes of the inherited class.
Inherited class = superclass,
The class that inherits is subclass.
subclassing . class hierarchy.
The member of a class declared private are not inherited by their subclasses.
The members of a class declared protected or public are inherited by the classes included
into packages that dont contain the superclass.
If a class inherits an abstract method, it has to implement it, or the class must be
declared abstract.
- abstract method = method without implementation
Tiberiu Leia: Software Engineering Object Oriented Programming
47
Example
class Class1
{
int p1=1;
public void met1()
{
System.out.println("Metoda met1()");
}
}
Class1
MainClass
+p1: int = 1
+met1(): void
+main(String[]): void
Class2
+p2: int = 2
+met2(): void
48
49
ClasaA
ClasaB
attributes
attributes
methods
methods
Strong aggregation
(composite
aggregation)
50
An object of type ClasaA (representing the whole) is direct responsible for creating its parts of
type ClasaB. BIDIRECTIONAL NAVIGABILITY?
An object of type ClasaA are in association relation with n objects of type ClasaB. n = * ;
or n=1
Some typical examples of multiplicity bounds:
0 Collection must be empty
1 Exactly one instance
5 Exactly 5 instances
* Zero or more instances
0..1 No instances or one instance
1..1 Exactly one instance
0..* Zero or more instances
1..* At least one instance
m..n At least m but no more than n instances
The solid diamond denotes composition. The strong form of aggregation is characterized by the
fact that components cannot exist without the aggregate. They have the same life span.
Tiberiu Leia: Software Engineering Object Oriented Programming
51
Implementation:
Variant 1)
class ClasaA {
ClasaB cb=new ClasaB();
...........................
}
Variant 2)
class ClasaA {
ClasaB cb;
ClasaA() {
cb=new ClasaB();
.....................
}
}
Variant 3)
class ClasaA {
ClasaB cb;
ClasaA(Type x) {
cb=new ClasaB(x);
Tiberiu Leia: Software Engineering Object Oriented Programming
52
.....................
}
}
How can be implemented for n?
How can be implemented BIDIRECTIONAL NAVIGABILITY and how can be tested the
bidirectional navigation?
53
Target
ClassA
Source
+1..*
ClassB
Light Aggregation
54
55
MainClass
ClassA
+obA: ClassA
+obB: ClassB
+valA: int
+main(): void
+metA(): void
ClassB
+valB: int
+metB(): void
56
MainClass
ClassA
+obA: ClassA
+obB: ClassB
+valA: int
+main(): void
+metA(): void
ClassB
+valB: int
+metB(): void
57
MainClass
ClassA
+obA: ClassA
+obB: ClassB
+valA: int
+main(): void
+metA(): void
ClassB
+valB: int
+metB(): void
58
ClasaA
attributes
methods
Indirect
Aggregation
ClasaB
attributes
methods
An object of type ClasaA (representing the whole) is not necessarily direct responsible of
creating its part of type ClasaB. The objects dont have the same life span.
An object type ClasaA is in relation with n objects of type ClasaB.
n = * ; or n=1
59
Implementation:
class ClasaC {
......................
static ClasaB metoda(Type par) {
......................................
ClasaB b=new ClasaB(par);
......................................
return b;
}
}
class ClasaA {
ClasaB cb;
...................
ClasaA(Type par) {
cb=ClasaC.metoda(par);
..............................
}
...................
}
60
Example:
/* Program*/
public class Class1 {
public static void main(String args[]) {
Class1 ob1=new Class1();// Self instantiation
Class2 ob2=new Class2(); //Instantiation of another class
System.out.println("Intrare main()");
ob1.met1();
ob2.met2();
}
public void met1() {
System.out.println("Mesaj: metoda1");
}
}
class Class2 {
public void met2() {
System.out.println("Mesaj: metoda2");
}
}
Homework: Draw the diagram that describes the relations between classes of the previous
program!
Tiberiu Leia: Software Engineering Object Oriented Programming
61
*
****
***END***
****
*
62