Содержание
- 2. Course Contents Part I: Object-Oriented Concepts and Principles Part II: Object-oriented Programming Closer look at the
- 3. Member Variables Variables defined inside a class are called member variables Member variables fall into two
- 4. Instance Variables All objects have their own copies of instance variables System allocates memory for instance
- 5. Class (Static) Variables The runtime system allocates a class variable once per class, regardless of the
- 6. Example of Instance and Class Variables in Java public abstract class Shape { public static int
- 7. Methods The code that describes how to perform an operation on a specific object type is
- 8. Instance Methods Both instance and class members can be referenced from an instance method If no
- 9. Class (Static) Methods A class method is a static method in some class. It may not
- 10. Method Overloading Method overloading means having two methods with the same name in the same class
- 11. Example of Method Overloading public class Rectangle extends Shape { private int width; private int height;
- 12. Coercion Coercion and overloading often go hand in hand Coercion occurs when an argument of one
- 13. Exercise Look through the Java API. Find an example of overloading and explain it. OOP, Rovaniemi
- 14. Variable Shadowing public class Circle extends Shape { private int radius; // Parameter radius shadows member
- 15. Naming the Member Variables A common way to avoid variable shadowing is to follow a naming
- 16. Variable Shadowing Example Fixed Rename member variable radius to a_radius Parameter radius no longer shadows the
- 17. Object Self-reference In OOP languages, a special keyword is used to refer to the current instance
- 18. Constructor A specialized method used to instantiate an object The constructor function has the same name
- 19. Destructor Destructor is a specialized method, which is called when an object is deleted Its purpose
- 20. Constructor Example 1 public class Rectangle extends Shape { private int width; private int height; public
- 21. Constructor Example 2 public class TestShape { public void someMethod() { Color myColor = new Color(100,
- 22. Method overriding Method overriding means providing a replacement method in a new class for a method
- 23. Example of Method Overriding 1 public abstract class Shape { public static int a_numberOfShapes=0; protected Color
- 24. Example of Method Overriding 2 public class Circle extends Shape { private int a_radius; public Circle(int
- 25. Redefinition Occurs when a subclass defines a method using the same name as a method in
- 26. Example of Redefinition class Parent { public void example(int a) {…} } class Child extends Parent
- 27. Variable Hiding Within a class, a member variable that has the same name as a member
- 28. Example of Variable Hiding class Employee { protected int salary; protected int hours; } class PartTimeEmployee
- 29. Exercise Consider the Java API class java.io.Writer What kind of examples of method overriding can you
- 30. Access Specifiers (Visibility Identifiers) There are three parts in a class, which have different protection level:
- 31. Access Specifiers Private members are only visible inside the class itself Public and protected parts define
- 32. Access Specifiers OOP, Rovaniemi University of Applied Sciences
- 33. Private Data – Public Accessor methods Member variables are usually declared as private Public get and
- 34. Checking Parameters for Validity Methods should be made as general as practical Do not set arbitrary
- 35. Example of Using Access Speciers with Set and Get Methods public class Circle extends Shape {
- 36. Exercise: Access Specifiers - Set and Get Methods Modify the class Shape so that the colour
- 37. Exercise: Checking Parameter Values for Validity Add set and get methods for the new int type
- 38. Using Protected Instance Variables Advantages Subclasses can modify values directly Slight increase in performance Avoid set/get
- 39. Abstract Class Abstract methods are methods that are declared but not yet implemented Abstract methods have
- 40. Purpose of Abstract Classes Allow a programmer to define common interface for a group of classes
- 41. Abstract Class vs. Interface Besides abstract methods, an abstract class can contain variable definitions and method
- 42. Polymorphism Generally, the ability to appear in many forms In OOP polymorphism refers to a programming
- 43. Polymorphism in OOP We can refer to a subclass type of an object with a superclass
- 44. Polymorphism Example 1 OOP, Rovaniemi University of Applied Sciences Base class Derived classes
- 45. Polymorphism Example 2 A simple example of polymorphism is the method append in the Java class
- 46. Exercises In your own words, explain polymorphism Consider again the Java API class java.io.Writer. (What kind
- 47. Late Binding 1 Dynamic binding of messages (method calls) to method definitions Polymorphism ensures that the
- 48. Late Binding 2 Actual operations can be bound to messages as soon as the type of
- 49. Virtual Operation An operation which late binding can be applied to is called a virtual operation
- 50. Group Work Write down the names of the Shape, Triangle, Rectangle and Circle classes (see previos
- 51. Group Work Add a new subclass for the class Shape, for instance Parallelogram ( area =
- 52. Other Forms of Polymorphism The type of polymorphism described earlier is called pure polymorphism or inclusion
- 53. Multiple Inheritance Sometimes it is tempting to inherit some attributes and methods from one class and
- 54. Problems with Multiple Inheritance Name ambiguity Inherited, different features can have the same name Same feature
- 55. Reference and Value Semantics Reference semantics: variable values are references to objects (Java) Assignment x=y causes
- 56. Reference and Value Semantics Pure object languages use reference semantics Value semantic type of copying can
- 58. Скачать презентацию