Class Notes (838,814)
Canada (511,109)
CSC207H1 (40)
Lecture

oct03c.docx

2 Pages
114 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
OCTOBER 03  Types, now that we know inheritance and interfaces o A class Child that extends class Parent and implements interface Person...  If of type child  Can be used anywhere that Parent is legal  Can be used anywhere that Interface is legal  But cannot be replaced by an object of type Parent  Children replace parents, not the other way around o Why use implements?  A class can only extend one parent, but can implement many interfaces  Implements is a way to guarantee a capability  A set of methods that implement some functionality  Casting o An instance of a class Child has  a Child part, with all the data members and methods of Child  a Parent part, with all the data members and methods of Parent  a Grandparent part, ... etc., all the way up to Object o Casting can be used to access the different parts o Need to cast down but not up  When Java looks up a name, it automatically will look up through the parts of an object if necessary  When java looks for a method, it automatically look at the lowest level, even if the object has been casted up  So we don’t need to cast “up”  But it won’t automatically look down.  If the data member or method we want is down in a more specific part of the object, we do need to cast  Of course, you also need to know where it begins to look  Where Java looks for a name, this depends on o whether it’s an instance or static member of the class o whether it’s a variable or method o if it’s a variable, what its type is and whether it has been cast to a new type  Shadowing and Overriding o Suppose class A and its subclass B each have  An instance variable x  An instance method m o A’s x is shadowed (hidden) by B’s x  this is confusing and rarely a good idea  When b’s x is called in an object b, b’s x is used  A’s x can still be called using super.x o A’s m is overridden by B’s m  This is often a good idea.  We often want to specialize behavior in a subclass  Example o Car.java 1. package names; 2. public class Porsche extends Car { 3. 4. String name = "Fast car"; 5. 6. public void makeSound() { 7. System.out.println(this.name + " vroooom!!!!"); 8. } 9. 10. } o Porsche.java 1. package names; 2. public class Car { 3. 4. String name = "Car"; 5. int gas = 10; 6. 7. public void makeSound() { 8. System.out.println(this.name + " beep
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit