CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs
Object-Oriented Thinking Chapters 1-8 introduced fundamental programming techniques for problem solving using loops, methods, and arrays. Classes provide more flexibility and modularity for building reusable software, and data encapsulation. 2
Encapsulation This means to encase information and behavior within an object o Aka data/information hiding - structure and implementation are hidden to other objects that interact with it Accessing data directly is bad software engineering o o Student.noOfStudents++; No protection: any code could set an instance/static variable to something inappropriate UC. Colorado Springs
Encapsulation First, add private modifier to important variables Second, create getters and setters to retrieve and set private variables o o getter - method that returns the value of an instance variable setter - method that sets the value of an instance variable StudentApp7-8 UC. Colorado Springs
Inheritance Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer is to use inheritance. 5
Superclasses and Subclasses -color: String -filled: boolean GeometricObject -datecreated: java.util.date +GeometricObject() +GeometricObject(color: String, filled: boolean) +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Creates a GeometricObject with the specified color and filled values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. -radius: double +Circle() Circle +Circle(radius: double) +Circle(radius: double, color: String, filled: boolean) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double +printcircle(): void -width: double -height: double +Rectangle() Rectangle +Rectangle(width: double, height: double) +Rectangle(width: double, height: double color: String, filled: boolean) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double 6
Inheritance When the definition of a class is based on an existing class (called the superclass) The class that is inheriting (subclass) can use accessible date fields and methods from superclass UC. Colorado Springs
Superclasses and Subclasses Example: StudentApp9.java StudentI -lastname/firstname: String -age: int -academiclevel: int +StudentI() +StudentI(last: String, first: String) +tostring(): String Properties and methods are inherited -campus: String CCStudentI CTechStudentI -campus: String UCCSStudentI -campus: String -UCCSID: int -lastuccsid: int +UCCSStudentI() +UCCSStudentI (last:string,first:string) +tostring(): String +getuccsid(): int +getcampus(): String 8
Are superclass s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. But superclass constructor can be invoked explicitly or implicitly. Explicitly: Invoked from the subclasses' constructors, using super(). Implicitly (automatically): If super is not explicitly used, the superclass's no-arg constructor is automatically invoked. 9
Superclass s Constructor Is Always Invoked A constructor may invoke an overloaded constructor, or its superclass s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, public A() { is equivalent to public A() { super(); public A(double d) { // some statements is equivalent to public A(double d) { super(); // some statements When a constructor in a subclass (child) is called it actually invokes the constructors of all the super classes (parents) in its inheritance chain 10
CAUTION You must use super() to call the superclass constructor. Invoking a superclass constructor s name in a subclass (e.g., calling Student() in UCCSStudent) causes a syntax error. 11
Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor: super() To call a superclass method: super.method() 12
Defining a Subclass A subclass inherits from a superclass. You can also: Add new properties Add new methods Override the methods of the superclass 13
Overriding Methods in the Superclass Student class: public String tostring (){ return String.format ("%-12s%-10s\t%d\t%d", this.lastname, this.firstname, this.age, this.academiclevel); UCCSStudent class: public String tostring (){ return String.format ("%-12s%-10s\t%d\t%d\t%d\t%s", this.lastname, this.firstname, this.age, this.academiclevel, this.uccssid, campus); 14
Overriding Methods in the Superclass A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. class UCCSStudent extends Student{ // Other methods are omitted /** Override the tostring method defined in Student */ public String tostring() { return super.tostring() + String.format( \t%d\t%s, this.uccssid, campus); Note: The method defined in a subclass (child) matches a method defined in superclass (parent) The subclass (child) method must match signature and return type of method in superclass (parent) 15
NOTE An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. 16
NOTE Like an instance method, a static method can be inherited. However, a static method cannot be overridden (belong to the class). If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. 17
Overriding vs. Overloading Overloading o o o We discussed overloading in methods chapter Two or more methods with the same name but different formal parameters The methods could be in the same class or in different classes related by inheritance Overriding o o Occurs when dealing with inheritance A method defined in the subclass that matches the signature and return type of the method defined in superclass UC. Colorado Springs
Overriding vs. Overloading public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); class B { public void p(double i) { System.out.println(i * 2); class A extends B { // This method overrides the method in B public void p(double i) { System.out.println(i); A inherits from B and method "p" in A matches in signature and return type method "p" in B public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); class B { public void p(double i) { System.out.println(i * 2); class A extends B { // This method overloads the method in B public void p(int i) { System.out.println(i); Method "p" in A has different formal parameters than method "p" in B: Class A has 2 methods called "p" 19
Summary Build your own classes and objects Constructors Static variables and methods Encapsulation, inheritance UC. Colorado Springs