Sie sind auf Seite 1von 41

Java

SCJP 6 exam
prepara.on course
by Arian CELINA, B.Sc., SCJP

Chapter 7:
Object Orienta,on

Contents

Encapsula.on
Inheritance
Polymorphism
Overriding/Overloading
Reference variable cas.ng
Implemen.ng an interface
Legal return types
Constructors and instan.a.on
Sta.cs
Coupling and Cohesion

Jan, 2009

Java trainig for SCJP 6

Encapsula.on
The benet of OO, exibility and maintainability
Keep your variables/proper.es private
Enforce access to proper.es with geSer and seSer methods
Keep your API safe for modica.on without breaking everyone
elses work
API Example

Jan, 2009

Java trainig for SCJP 6

Inheritance

Inheritance is everywhere in java


Every class in java inherits from Object class (except Object itself)
Code reuse and polymorphism are expecta.ons behind
inheritance
instanceof operator is used to test for inheritance

Jan, 2009

Java trainig for SCJP 6

Inheritance - example
public class Human extends Moveable {

public class Moveable {

public void think() {}

public void move() {

System.out.println(A);
}

public class TestMoveable {

public sta.c void main(String[] ar) {

public class Dog extends Moveable{

new Human().move();

new Dog().move();
}
Jan, 2009

Java trainig for SCJP 6

Is A rela.onship
Expressed through extends and implements

instanceof operator is used to test Is-A rela.onship


Applies to the whole inheritance tree

Jan, 2009

Java trainig for SCJP 6

Is A rela.onship - example
class Human{}

Boy is a Male

class Male extends Human{}

Boy is a Human

class Boy extends Male {}

Male is a Human
Boy is a subclass of Male
Male is a superclass of Boy
Human is a superclass of Boy

Jan, 2009

Java trainig for SCJP 6

Has A rela.onship
Object A is said to have Object B when A has a reference to B
public class Time {

public class Hour {

Hour h;

}
Time Has A Hour
Jan, 2009

Java trainig for SCJP 6

Polymorphism

Every object that can pass more than one Is-A test is polymorphic
Except objects of type Object, every object in Java is polymorphic

Jan, 2009

Java trainig for SCJP 6

Polymorphism (2)
A reference variable can be of one type, and that type cannot
change aier the declara.on
A reference is a variable and it can be re-assigned
Reference variables type denes which methods can be invoked
on that object
A reference variable can refer to any object of declared type or its
subtype
A reference type can be declared as class type or interface type
Jan, 2009

Java trainig for SCJP 6

10

Polymorphism (3)
class Human{}

Human h = new Human();

class Male extends Human{}

Human h2 = new Male();

class Boy extends Male {}

Boy b = new Boy();


h2 = b;
h = h2;

On run.me, the method of overriding object will run


Jan, 2009

Java trainig for SCJP 6

11

Overriding
Overriding means re-implemen.ng an inherited method
At run.me, regardless of the variable type, overridden method will run
public class Car {
public void accelerate() {
System.out.println(Generic accelerate);
}
}
public class BMW extends Car {
public void accelerate() {
System.out.println(20 km\h 100 km\h);
}
}
Jan, 2009

public class TestCar {


public sta.c void main(String[] args) {
Car c1 = new Car();
Car c2 = new BMW();

c1.accelerate();
c2.accelerate();
}
}

Java trainig for SCJP 6

12

Overriding (2)
The argument list must be same with the overridden methods
The return type must be the same, or a subtype
The access level can not be more restric.ve can be less restric.ve
A method can be overridden only if it is inherited
Overriding method can throw any unchecked excep.on
Overriding method can throw a narrower excep.on, but not wider
Overriding method can not throw new excep.ons
Jan, 2009

Java trainig for SCJP 6

13

Overriding (3)
Overriding method can omit declaring an excep.on which it never
throws even overridden does declare it
A method marked nal can not be overridden
Sta.c methods can not be overridden

Jan, 2009

Java trainig for SCJP 6

14

Overloading
The same method name with dierent argument list
Overloading methods MUST change the arguments
Overloading methods CAN change the return type
Overloading methods CAN throw new excep.ons
Overloading methods CAN change the access level
Overloading can be in the same class or in a subclass if overloaded
method is inherited
Jan, 2009

Java trainig for SCJP 6

15

Overloading (2)

public class Overloading {


public int add(int n1, int n2) { return n1 + n2;}
public long add(long n1, long n2) { return n1 + n2;}
public int add(short n1, short n2) {return n1 + n2;}
}

Jan, 2009

Java trainig for SCJP 6

16

Overloading (3)
class Calculator {
int add(int x, int y) { return x + y;}
double add(double x, double y) { return x + y;}
}

add(4, 6) - which method is invoked?
add(2.2, 5.1) which method is invoked?

Jan, 2009

Java trainig for SCJP 6

17

Overloading (4)
class Gree.ng{
void gree.ng(Human h) { System.out.println(Hello human);}
void gree.ng(Boy b) { System.out.println(Hello boy);}
}
Human h = new Human(); Boy b = new Boy(); Human hb = new Boy();

Calling:
gree.ng(h);
gree.ng(b); what will be print in each of these?
gree.ng (hb);
Jan, 2009

Java trainig for SCJP 6

18

Overloading (4)
class Gree.ng{
void gree.ng(Human h) { System.out.println(Hello human);}
void gree.ng(Boy b) { System.out.println(Hello boy);}
}
Human h = new Human(); Boy b = new Boy(); Human hb = new Boy();

Calling:
gree.ng(h); ------> Hello human
gree.ng(b); ------> Hello boy
gree.ng (hb); ------> Hello human
Jan, 2009

Java trainig for SCJP 6

19

Reference variable cas.ng


public class TestCas.ng {
public class Human {

public sta.c void main(String[] args) {

void talk() {

Human[] humans = { new Human(),

System.out.println(Human talk);

new Human(),

new Boy()}

for (Human h : humans) {

public class Boy extends Human {

h.talk();

void talk() { System.out.println(Boy talking);}

if (h instanceof Boy) {

void cry() {System.out.println(Boy crying);}

((Boy)h).cry();

}
}
Jan, 2009

Java trainig for SCJP 6

20

Reference variable cas.ng (2)


Compiler will allow cas.ng if the

public class TestCas.ng {

classes are in the same inheritance

public sta.c void main(String[] args) {

tree

Human h = new Human();

The example compiles but fails at


run.me because a human object

Boy b = (Boy) h; //fails on run.me


}
}

can not be casted to Boy

Jan, 2009

Java trainig for SCJP 6

21

Reference variable cas.ng (3)


This example does not compile

public class TestCas.ng {

because String is not in the same

public sta.c void main(String[] args) {

inheritance tree with Boy

Human h = new Human();


Boy b = (String) h; //does not compile
}
}

Jan, 2009

Java trainig for SCJP 6

22

Reference variable cas.ng (4)


In upcas.ng, specifying cast is

public class TestCas.ng {

op.onal. The example compiles.

public sta.c void main(String[] args) {


Boy b = new Boy();
Human h = (Human) b;
Human h2 = b;
}
}

Jan, 2009

Java trainig for SCJP 6

23

Implemen.ng an interface
Implemen.ng an interface is complying with the contract
The concrete class which implements an interface must provide
implementa.on to all interface methods
Follow overriding rules
Maintain the correct signature of interface methods
If a superclass declares it implements an interface, the subclass
can declare it again but not provide an implementa.on.
Jan, 2009

Java trainig for SCJP 6

24

Implemen.ng an interface (2)


A class can implement more than one interfaces
An interface can extend an interface
An interface can extend more than one interfaces
An abstract class does not need to provide implementa.on to
interface methods of the interface it implements

Jan, 2009

Java trainig for SCJP 6

25

Legal return types


For an overloaded method, there is no restric.on in return type
In overrides, as of Java 5, a returned value can be a subtype of
declared type
Example: public Human getHuman() { return new Human();}
public Human getHuman() { return new Boy();} //overriden method

Jan, 2009

Java trainig for SCJP 6

26

Legal return types (2)


A null value can be returned when a type of Object is expected
Array is a perfectly acceptable return type
When a primi.ve value is expected, any value that implicitly can
be converted to that type can be returned, e.g. byte for int
When primi.ve value is expected, any value that can be casted
to that type can be returned, e.g. public int calcSomething() {
double a = 2.5;
return (int) a; }
Jan, 2009

Java trainig for SCJP 6

27

Legal return types (3)


A method with void return type must not return anything
When Object reference return type is expected, any value that
implicitly can be casted to that object type can be returned
e.g. public Human calcSomething() {
return new Boy();
}

Jan, 2009

Java trainig for SCJP 6

28

Constructors and Instan.a.on


Every class must have a constructor (Abstract & Concrete)
If it is not typed, the compiler adds one
Constructors have the same name with the class
Constructors do not have return type
Constructors can use any access modier (including private)
Are typically used to instan.ate instance variables

Jan, 2009

Java trainig for SCJP 6

29

Constructors and Instan.a.on (2)


Constructors are invoked at run.me when we say new
When a constructor is invoked, all constructors up in the
hierarchy are invoked

Jan, 2009

Java trainig for SCJP 6

30

Constructors and Instan.a.on (3)


Every constructor, as rst statement has either call to this or super
You cannot call a method or access an instance variable un.l the
super constructor runs
The only place where a constructor can be called is from another
constructor

Jan, 2009

Java trainig for SCJP 6

31

Constructors and Instan.a.on (4)


The compiler will create a default constructor only when no
constructor is declared
Default constructor is a no-argument constructor
Default constructor has the same access modier as the class
Default constructor makes a call to super with no arguments

Jan, 2009

Java trainig for SCJP 6

32

Constructors and Instan.a.on (5)


Will compiler create a default constructor?

class Hello { Hello(){}} . No
class Hello2 {} Yes

Jan, 2009

Java trainig for SCJP 6

33

Constructors and Instan.a.on (6)


Constructors are not inherited
Constructors can be overloaded

E.g.

class Test {

Test() {}
Test(int i) {}
}
Jan, 2009

Java trainig for SCJP 6

34

Sta.cs
Methods are marked sta.c if its implementa.on is not dependant
to instance
A variable is marked sta.c if it should keep informa.on related to
class, not the instance e.g. count the number of instances

Jan, 2009

Java trainig for SCJP 6

35

Sta.cs (2)
Sta.c methods cannot be overridden
Sta.c methods can be overloaded
Sta.c methods cannot access instance variables
Sta.c methods can use sta.c variables and other sta.c methods

Jan, 2009

Java trainig for SCJP 6

36

Sta.cs (3)
A variable is marked sta.c if it should keep informa.on related to
class, not the instance e.g. count the number of instances
A method is marked sta.c if it does not used instance variables
and non sta.c methods of the class

Jan, 2009

Java trainig for SCJP 6

37

Sta.cs (4)
Accessing sta.c methods and variables syntax

MyClassName.method()

Jan, 2009

OR

MyClassName.variable

Java trainig for SCJP 6

38

Coupling and Cohesion

Coupling is about how much one class knows about the other
Loose coupling is good Tight coupling is bad
Cohesion is about how specialised a class is
High cohesion facilitates maintainability of the code

Jan, 2009

Java trainig for SCJP 6

39

References
SCJP Sun Cer.ed Programmer for Java 6 Exam 310-065
By Katherine Sierra and Bert Bates

Chapter 2

Jan, 2009

Java trainig for SCJP 6

40

Contact and lectures

Lectures

www.arian-celina.com

e-mail

arian.celina@gmail.com

Jan, 2009

Java trainig for SCJP 6

41

Das könnte Ihnen auch gefallen