Академический Документы
Профессиональный Документы
Культура Документы
using Java
Simon Kroly
simon.karoly@codespring.ro
Classes and Objects
//methods
public void talk() {} Instantiation:
public void learn() {} Person p1 = new Person (Jancsi, 18);
Person p2 = new Person(Juliska, 18);
}
Student *
Town Country Capital
1..* 1 1 1
0..1 0..1
Person
+name: String
+age: int
+talk(): void
+learn(): void
Student Professor
Constructors
Access modifiers, visibility settings for attributes and methods:
public (+) can be accessed from the class and any other classes
private (-) can only be accessed within the class
protected (#) can only be accessed within the class and its subclasses
package-private: - can be accessed from the class and from other classes
within the same package. This is the default visibility (if there are no access
modifier specified)
this reference: reference to the current object
For classes:
final can not be used as a superclass
abstract can not be instantiated, can only be used as a superclass. If there
is an abstract method within a class, then the class itself must be declared
abstract.
For methods:
static class methods, there is no need for instantiation to call these
methods. They can be called using the class name: Classname.method() (e.g.
Integer.toString()). Only static attributes can be used and static methods can
be called within these methods.
abstract can be used only in abstract classes. There is no implementation
provided for these methods in the abstract class. Implementations must be
provided by the derived classes (subclasses) (or they have to be also declared
abstract).
final these methods cannot be overridden in the subclasses
native implementation is done using a native programming language
synchronized synchronized access to critical resources (see: threads)
7/11/2016 OOP in Java 10
Type modifiers
For fields:
static class attribute, the value is shared between instances (the same
value is used).
final there is only one possibility for using assignment operator (only one
value can be assigned to these attributes). The modifier is used for declaring
constants. Attention: a final reference can not be "redirected", but the object
state can be modified through the reference.
transient it is not a part of the persistent state of the object, it will be
excluded during serialization.
volatile it can be modified by multiple threads. It will be never cached
thread-locally (reads and writes are done directly to/from memory).
("synchronized" access)
The type used in the reference declaration vs. the type of the
referenced object.
What happens when a reference declared as type A references an
object of type B and a method is called through this reference,
which is overridden in class B? (which "version" will be actually
called?)
Probably, if the method is overridden, we want to use the implementation corresponding to the
concrete object type. This behavior is not automatically guaranteed by all programming
languages. E.g. C++ virtual methods.
In Java always the implementation corresponding to the concrete
object is called (dynamic binding). We can say: in Java all methods
are virtual.
Implementing class:
public class Circle implements Resizable {
public void resize(Dimension d) {
//source code for resizing the circle
}
}
Main:
public static void main(String[] args) {
Circle c = new Circle();
c.resize(new Dimension(100,100));
Resizable s = new Circle();
s.resize(new Dimension(100,100));
}
class A {
A(){}
class B { declaring classes within other
B() {} classes
public void doIt() { non-static nested classes
System.out.println("Hello");
}
cannot contain static methods
}; the instantiation of non-static
} nested classes is always
preceded by the instantiation of
public class Example { the outer class
public static void main(String[] args) {
A a = new A();
A.B b = a. new B();
b.doIt();
}
}
...
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
...
getClass(): returns the runtime class of this object (a Class object will
be returned).
toString(): returns a string representation for this object. By default a
string is returned based on the name of the class and the hash code of
the object. It is recommended for all subclasses to override this method.
finalize(): called by the garbage collector on an object when garbage
collection determines that there are no more references to the object. A
subclass overrides the finalize method to dispose of system resources or
to perform other cleanup.
equals(): indicates whether some other object is "equal to" this one. For any
non-null reference values the default implementation returns true if these refer
to the same object. This method is usually overridden within subclasses to
implement an equivalence relation based on object states. E.g. two strings are
equals if they have the same content. Obs.: it is generally necessary to override
the hashCode method whenever the equals method is overridden (see the
general contract of the hashCode method).
hashCode(): returns an integer, the hash code value for the object. This
method is supported for the benefit of hash tables. The default implementation
returns distinct integers for distinct objects (typically implemented by using the
internal address of the object). If two objects are equal according to the equals
method, then calling the hashCode method the same integer result has to be
produced for both objects. (Generally, all the fields used for equality check
within the equals method will be used for generating the hash code.)
clone(): creates and returns a copy of this object. The signature of the
method:
protected Object clone() throws CloneNotSupportedException
A copy can be created only if the Cloneable interface is implemented by the class
(otherwise runtime exception is thrown). This interface is not implemented by the
Object class, so not all Java objects are cloneable.
The clone method creates a new instance of the class of this object and initializes
all its fields with the contents of the corresponding fields of this object. By default,
the contents of the fields are not themselves cloned. Thus, by default the method
performs a shallow copy. It should be properly overridden to perform a deep copy.
Using packages:
java.awt.Button b;
MyPackage.MyBaseClass ob;
MyPackage.MyDerivedClass od;
or:
import java.awt.Button;
import MyPackage.MyBaseClass;
import MyPackage.MyDerivedClass
or:
import java.awt.*;
import MyPackage.*;
core collection
Person
StudentIterator
#name: String
#age: int +hasMoreElements(): boolean
+nextElement(): Student
+Person(String, int)
+getName(): String
+setName(String): void <<inner class>>
+getAge(): int StudentIteratorImpl
+setAge(int): void
-index: int
+StudentIteratorImpl()
Student +hasMoreElements(): boolean
+nextElement(): Student
-faculty: String
+StudentList(int size)
+addStudent(Student): void
TestStudentList
+getIterator(): StudentIterator