Java Object Oriented Design CSC207 Fall 2014
Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code What language do you plan to use for implementation?
Design Problem What did you decide about: variables you need for each shape methods you need to implement Are you planning to use an Object Oriented language (e.g. Java)?
Principles of Object Orientation 1. Encapsulation 2. Inheritance 3. Polymorphism
Class vs Object Class is more like a blueprint An object is an instance of a class
Instance and Static Variables, Constants public class Circle { private int x,y; private int radius; private static int numcircles = 0; public static final double Pi = 3.1415926535; // Methods } radius is an instance variable. Each object/instance of the Circle class has its own radius variable. numcircles is a static variable. Also called a class variable. There is one numcircles variable shared by all objects of the class. Pi is a constant (an immutable variable). Since it is declared final, its value cannot be reassigned. We made it public, since it is static and final.
Encapsulation Convention: make all non-final instance variables private, which makes them accessible only within the class. When desired, give outside access using getter and setter methods. Why make instance variables private? We aim to hide implementation details and allow access to variables via a well-defined interface (the public methods for that class).
Access Modifiers Classes can be declared public or package-private. Members of classes can be declared public, private, protected, or package-private. Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier (aka packageprivate) Y Y N N private Y N N N
Constructors A constructor has: the same name as the class no return type (not even void) A class can have multiple constructors, as long as their signatures are different. Signature is the method name + its parameters If you do not define a constructor, the compiler supplies one with no parameters and no body. Once you define a constructor for a class, the compiler will no longer supply the default constructor.
this this is an instance variable that you get without declaring it. It is like self in Python. Its value is the address of the object whose method has been called.
Method return type Defining Methods A method must have a return type declared. Use void if nothing is returned. The form of a return statement: return expression If the expression is omitted or if the end of the method is reached without executing a return statement, nothing is returned.
For access modifiers: public - callable from anywhere protected - callable from same class, package, or subclasses no modifier - callable from same class or package private - callable only from this class Variables declared in a method are local to that method.
Parameters When passing an argument to a function, you pass what is in the variable s box: For class types, you are passing a reference. (Like Python) For primitive types, you are passing a value. This has important implications: aliasing! (we will cover this later)
Inheritance hierarchy All classes form a tree called the inheritance hierarchy, with Object at the root. Class Object does not have a parent. All other Java classes have exactly one parent. If a class has no parent declared, it is a child of class Object. A parent class can have multiple child classes. Class Object guarantees that every class inherits methods tostring, equals, and others. For class Circle to inherit from class Shape we use the keyword extends public class Circle extends Shape {
Inheritance Inheritance allows one class to inherit the non-private data/methods of another class. In a subclass, super refers to the part of the object defined by the parent class. To call a parent s constructor: super(arguments); A subclass can add new data and methods. A subclass can also reuse a name already used in its superclass (i.e. inherited data member or method). More to come on the implications of this...
Shadowing and Overriding Suppose class A and its subclass AChild each have an instance variable x an instance method m A s x is shadowed by Achild s x. This is confusing and rarely a good idea. A s m is overridden by Achild s m. This is often a good idea. We often want to specialize behaviour in a subclass. If a method must not be overridden in a descendant, declare it final.
Polymorphism (Dynamic Binding) A variable can be assigned objects of its type or any subtype. But which method gets called depend not on the variable type, but on the type of the object it currently refers to. Overriding and dynamic binding allow a superclass to define some things that it and all of its descendants share, and yet the correct specialized method to be called.
Casting Down Object o = new String( hello ); We cannot do the following, because Object does not have a charat method: char c = o.charat(1); Instead, we need to the object that o refers to as a String: char c = ((String) o).charat(1);