Algorithmics 3 Algorithmics 3 Inheritance September 27, 2006 Algorithmics 3 Program Program Classes About classes Object fields and methods Class methods and fields Constructors Inheritance Polymorphism Summary Algorithmics 3 Program What we’ve seen so far I I A specification is a description in English (or in French). Object-oriented programming is all about I I I I I objects the state of these objects (i.e. their properties) what the objects can do what messages the objects can answer to the contracts objects enter. Algorithmics 3 Program What we’ve seen so far I I A specification is a description in English (or in French). Object-oriented programming is all about I I I I I I objects the state of these objects (i.e. their properties) what the objects can do what messages the objects can answer to the contracts objects enter. we’re still short of concrete relations between objects ! Algorithmics 3 Program Today I Classes. I Constructors. I Object methods and fields I Class methods and fields. I Inheritance. I Polymorphism. Algorithmics 3 Program Today I Classes. I Constructors. I Object methods and fields I Class methods and fields. I Inheritance. I Polymorphism. Most of these things you know already. Don’t worry, there’ll be new things, too. I Algorithmics 3 Classes About classes Program Classes About classes Object fields and methods Class methods and fields Constructors Inheritance Polymorphism Summary Algorithmics 3 Classes About classes What’s a class ? Q What’s a class ? Algorithmics 3 Classes About classes What’s a class ? Q What’s a class ? A A class describes a set of objects. Algorithmics 3 Classes About classes What’s a class ? Q What’s a class ? A A class describes a set of objects. A A class is a manner of building objects sharing some traits (properties, messages, etc.). Algorithmics 3 Classes About classes What’s a class ? Q What’s a class ? A A class describes a set of objects. A A class is a manner of building objects sharing some traits (properties, messages, etc.). A In Java, a class is the only manner of building objects. Not in OCaml or JavaScript. Algorithmics 3 Classes About classes What’s a class ? Q What’s a class ? A A class describes a set of objects. A A class is a manner of building objects sharing some traits (properties, messages, etc.). A In Java, a class is the only manner of building objects. Not in OCaml or JavaScript. Two objects of the same class will share all the traits defined by that class. Algorithmics 3 Classes About classes Defining a class Each class must be defined in its own file. // I n f i l e MyClass . j a v a p u b l i c c l a s s MyClass { // T h i s i s an empty c l a s s } Algorithmics 3 Classes Object fields and methods Object fields and object methods Classes can contain object fields and object methods. // I n f i l e M y S e c o n d C l a s s . j a v a p u b l i c c l a s s MySecondClass { p r i v a t e i n t someField = 0; p r i v a t e i n t s o m e O t h e r F i e l d ; // S h o u l d be i n i t i a l i z e d ! p u b l i c i n t getSomeValue ( ) { r e t u r n t h i s . someField ∗2; } p u b l i c i n t getOtherField () { r e t u r n t h i s . s o m e O t h e r F i e l d +1; } p u b l i c boolean areValuesEqual () { r e t u r n t h i s . g e t S o m e V a l u e ()== t h i s . g e t O t h e r F i e l d ( ) ; } } Algorithmics 3 Classes Object fields and methods What’s this ? Algorithmics 3 Classes Object fields and methods What’s this ? The special variable this represents the current object. Algorithmics 3 Classes Object fields and methods What’s this ? The special variable this represents the current object. An object field is part of all the objects of the corresponding class. An object method is part of all the objects of the corresponding class. Both fields and methods can be accessed with this.. Algorithmics 3 Classes Object fields and methods What’s this ? The special variable this represents the current object. An object field is part of all the objects of the corresponding class. An object method is part of all the objects of the corresponding class. Both fields and methods can be accessed with this.. That’s the only kind of field you have seen so far. That’s not the only kind of method you have seen so far. Algorithmics 3 Classes Object fields and methods What’s this ? The special variable this represents the current object. An object field is part of all the objects of the corresponding class. An object method is part of all the objects of the corresponding class. Both fields and methods can be accessed with this.. That’s the only kind of field you have seen so far. That’s not the only kind of method you have seen so far. Until now, you probably haven’t used this. to access fields and methods. It’s not compulsory but it often makes your code much more readable. Algorithmics 3 Classes Class methods and fields Applications A class can define method public static void main(String[] arguments). This defines a potential entry point to the program. // I n f i l e M y A p p l i c a t i o n . j a v a public class MyApplication { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g u m e n t s ) { // . . . } } Algorithmics 3 Classes Class methods and fields Applications A class can define method public static void main(String[] arguments). This defines a potential entry point to the program. // I n f i l e M y A p p l i c a t i o n . j a v a public class MyApplication { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g u m e n t s ) { // . . . } } Note that there can be several entry points to the program (usually for testing purposes). Parameter arguments defines command-line arguments you can pass to the program. Algorithmics 3 Classes Class methods and fields Staticity ? // I n f i l e M y A p p l i c a t i o n . j a v a public class MyApplication { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g u m e n t s ) { // . . . } } Keyword static specifies that the method is a class method. In other words, the method belongs to the class, not to its instances. Algorithmics 3 Classes Class methods and fields Staticity ? // I n f i l e M y A p p l i c a t i o n . j a v a public class MyApplication { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g u m e n t s ) { // . . . } } Keyword static specifies that the method is a class method. In other words, the method belongs to the class, not to its instances. main is a class method of class MyApplication, which means that you can use it without having to instanciate MyApplication. System.out is a class field of class System, which means that you can use it without having to instanciate System. Algorithmics 3 Classes Class methods and fields Uses and limitations // I n f i l e M y S t a t i c C l a s s . j a v a public class MyStaticClass { public static f i n a l String blue = " blue " ; public static f i n a l red = " red " ; public s t a t i c void t e s t ( ) { System . o u t . p r i n t l n ( " T h i s ␣ i s ␣ a ␣ t e s t " ) ; } } Class fields act as global variables, while class methods act as global procedures and functions. Algorithmics 3 Classes Class methods and fields Uses and limitations // I n f i l e M y S t a t i c C l a s s . j a v a public class MyStaticClass { public static f i n a l String blue = " blue " ; public static f i n a l red = " red " ; public s t a t i c void t e s t ( ) { System . o u t . p r i n t l n ( " T h i s ␣ i s ␣ a ␣ t e s t " ) ; } } Class fields act as global variables, while class methods act as global procedures and functions. In other words, MyStaticClass.red = "yellow"; changes the value of MyStaticClass.red for the whole universe. Algorithmics 3 Classes Class methods and fields Uses and limitations // I n f i l e M y S t a t i c C l a s s . j a v a public class MyStaticClass { public static f i n a l String blue = " blue " ; public static f i n a l red = " red " ; public s t a t i c void t e s t ( ) { System . o u t . p r i n t l n ( " T h i s ␣ i s ␣ a ␣ t e s t " ) ; } } Class fields act as global variables, while class methods act as global procedures and functions. In other words, MyStaticClass.red = "yellow"; changes the value of MyStaticClass.red for the whole universe. Usually, if you don’t specifically know that you need to define a class field/method, don’t. Algorithmics 3 Classes Constructors Program Classes About classes Object fields and methods Class methods and fields Constructors Inheritance Polymorphism Summary Algorithmics 3 Classes Constructors Class construction Object o = new Object() Algorithmics 3 Classes Constructors Class construction Object o = new Object() Construction I is what happens when you call new initializes the memory I creates the actual object (this) I must initalize final fields. I Algorithmics 3 Classes Constructors Class construction Object o = new Object() Construction I is what happens when you call new initializes the memory I creates the actual object (this) I must initalize final fields. I Until construction is complete, you cannot reliably call methods, access fields, or use this. Algorithmics 3 Classes Constructors Class construction Object o = new Object() Construction I is what happens when you call new initializes the memory I creates the actual object (this) I must initalize final fields. I Until construction is complete, you cannot reliably call methods, access fields, or use this. Java will not check that ! Algorithmics 3 Classes Constructors Declaring a constructor In Java, a constructor is declared almost like a method – but it’s not a method ! Algorithmics 3 Classes Constructors Declaring a constructor In Java, a constructor is declared almost like a method – but it’s not a method ! I you can think of the constructor as of a function returning the object Algorithmics 3 Classes Constructors Declaring a constructor In Java, a constructor is declared almost like a method – but it’s not a method ! I you can think of the constructor as of a function returning the object – indeed, that’s what it is in OCaml or JavaScript I a constructor can be public, private, protected or package-protected a constructor doesn’t have a return value I I a constructor cannot return the name of the constructor must be exactly the name of the class I the constructor accepts parameters just like a method I you can have several different constructors, provided they accept different parameter types I at the end of the constructor, all object fields should be initialized I at the end of the constructor, all final object fields must be initialized. I Algorithmics 3 Classes Constructors Syntactically public { class MyClassWithConstructor f i n a l i n t number ; p u b l i c MyClassWithConstructor () { number = 0 ; } p u b l i c MyClassWithConstructor ( f i n a l { number = s o m e V a l u e ; } } int someValue ) Algorithmics 3 Classes Constructors What about destruction ? Just like constructors, you can write destructors (actually, finalizers). Algorithmics 3 Classes Constructors What about destruction ? Just like constructors, you can write destructors (actually, finalizers). We’re just not going to detail them today. Algorithmics 3 Classes Inheritance And the meek... You already know that every class inherits from class Object. When class Child inherits from class Parent, I every public, protected or package-protected object field of Parent automatically appears as an object field of Child – private fields cannot be manipulated I every public, protected or package-protected object method of Parent automatically appears as an object method of Child – private methods cannot be manipulated I every instance of Child can be used as if it were a Parent. Algorithmics 3 Classes Inheritance And the meek... You already know that every class inherits from class Object. When class Child inherits from class Parent, I every public, protected or package-protected object field of Parent automatically appears as an object field of Child – private fields cannot be manipulated I every public, protected or package-protected object method of Parent automatically appears as an object method of Child – private methods cannot be manipulated I every instance of Child can be used as if it were a Parent. In other words, class Child specializes class Parent. Algorithmics 3 Classes Inheritance Declaring inheritance public { class Parent // D e f i n e // D e f i n e methods fields } public { class Child extends Parent // E v e r y p u b l i c / p r o t e c t e d / p a c k a g e−p r o t e c t e d method and f i e l d // o f P a r e n t c a n be u s e d h e r e , a s i f i t had b e e n d e f i n e d i n // C h i l d } Inheritance is declared with extends. Algorithmics 3 Classes Inheritance Inheritance and construction public { class Rectangle p r i v a t e f i n a l i n t width ; p r i v a t e f i n a l i n t height ; p u b l i c Rectangle ( i n t width , i n t h e i g h t ) { t h i s . width = width ; t h i s . h e i g h t= h e i g h t ; } p u b l i c i n t getWidth ( ) { r e t u r n width ; } p u b l i c i n t getHeight () { return height ; } p u b l i c i n t getArea () { r e t u r n getWidth ()∗ g e t H e i g h t ( ) ; } } public { class Square public { inherits Square ( i n t Rectangle side ) super ( side , side ); } } Descendants must invoke one of the constructors of their parent, using keyword super. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. 6. this is now a fully-fledged Object. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. 6. this is now a fully-fledged Object. 7. The constructor of Rectangle proceeds and terminates. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. 6. this is now a fully-fledged Object. 7. The constructor of Rectangle proceeds and terminates. 8. this is now also a fully-fledged Rectangle. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. 6. this is now a fully-fledged Object. 7. The constructor of Rectangle proceeds and terminates. 8. this is now also a fully-fledged Rectangle. 9. The constructor of Square proceeds and terminates. Algorithmics 3 Classes Inheritance More about construction Let’s instanciate Square with new Square(10). 1. A constructor of Square is called. 2. A constructor of Rectangle is called. 3. A constructor of Object is called. 4. Some magic creates the object. 5. The constructor of Object proceeds and terminates. 6. this is now a fully-fledged Object. 7. The constructor of Rectangle proceeds and terminates. 8. this is now also a fully-fledged Rectangle. 9. The constructor of Square proceeds and terminates. 10. this is now also a fully-fledged Square. Algorithmics 3 Classes Polymorphism Towards polymorphism Let’s refine our definition of squares public { class OtherSquare public { extends OtherSquare ( i n t Square size ) super ( size ); } public { int getWidth ( ) System . out . p r i n t l n ( " C a l l i n g ␣ OtherSquare . getWidth ( ) " ) ; s u p e r . getWidth ( ) ; } } Algorithmics 3 Classes Polymorphism What have we done ? O t h e r S q u a r e a = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n a . getArea ( ) ; I Algorithmics 3 Classes Polymorphism What have we done ? O t h e r S q u a r e a = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n a . getArea ( ) ; I "Calling OtherSquare.getWidth()" S q u a r e b = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n b . getArea ( ) ; I Algorithmics 3 Classes Polymorphism What have we done ? O t h e r S q u a r e a = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n a . getArea ( ) ; I "Calling OtherSquare.getWidth()" S q u a r e b = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n b . getArea ( ) ; I "Calling OtherSquare.getWidth()" R e c t a n g l e c = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n c . getArea ( ) ; I Algorithmics 3 Classes Polymorphism What have we done ? O t h e r S q u a r e a = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n a . getArea ( ) ; I "Calling OtherSquare.getWidth()" S q u a r e b = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n b . getArea ( ) ; I "Calling OtherSquare.getWidth()" R e c t a n g l e c = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n c . getArea ( ) ; I "Calling OtherSquare.getWidth()" O b j e c t d = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n d . getArea ( ) ; I Algorithmics 3 Classes Polymorphism What have we done ? O t h e r S q u a r e a = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n a . getArea ( ) ; I "Calling OtherSquare.getWidth()" S q u a r e b = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n b . getArea ( ) ; I "Calling OtherSquare.getWidth()" R e c t a n g l e c = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n c . getArea ( ) ; I "Calling OtherSquare.getWidth()" O b j e c t d = new O t h e r S q u a r e ( 1 0 ) ; r e t u r n d . getArea ( ) ; I Doesn’t compile Algorithmics 3 Classes Polymorphism That’s polymorphism Any object method can be redefined (or “overloaded”) in its subclasses. This is called polymorphism. This is a powerful mechanism but it also makes object-oriented programs more fragile. Algorithmics 3 Classes Polymorphism That’s polymorphism Any object method can be redefined (or “overloaded”) in its subclasses. This is called polymorphism. This is a powerful mechanism but it also makes object-oriented programs more fragile. That’s the reason why we define actions as protected: so that they can be overloaded by subclasses. Algorithmics 3 Classes Polymorphism That’s polymorphism Any object method can be redefined (or “overloaded”) in its subclasses. This is called polymorphism. This is a powerful mechanism but it also makes object-oriented programs more fragile. That’s the reason why we define actions as protected: so that they can be overloaded by subclasses. The alternative would be to declare actions as I either private – hence unusable and unmodifiable by subclasses I or final – hence unmodifiable by subclasses. Algorithmics 3 Summary Summary I I A specification is a description in English (or in French). Object-oriented programming is all about I I I I I I objects the state of these objects (i.e. their properties) what the objects can do what messages the objects can answer to the contracts objects enter building specialized objects from more generic objects. Algorithmics 3 Summary Next week Swing !