New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 4 Notes

by: Taylor Kahl

Week 4 Notes CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Covers lectures on Feb 1st and 3rd and corresponding textbook material
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 7 page Class Notes was uploaded by Taylor Kahl on Saturday January 30, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 20 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU

Popular in ComputerScienence


Reviews for Week 4 Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 01/30/16
From 02/01 lecture: Continuing the section on Objects & the equals method… o Say we want to compare any type of Object to a Point object to see if they’re equal – we can typecast a generic Object to be a Point Updated method: public boolean equals(Object o) { Point other = (Point) o; //changes o from an Object to a Point named other return x = other.x && y = other.y; } Returns true if the objects’ x and y fields are equal o But, typecasting doesn’t change the object itself, just its reference  you can’t typecast incompatible objects  Example: you can’t typecast a String as a Point. So if you tried to pass a String as a parameter you’ll get an error  But we want to be able to call this method on any type of object! o Fix this with instanceof operator  instanceof – boolean operator – tests whether a variable refers to an object of a given type o Ex: String s = “carrot’;  s instanceof String – true  s instanceof Point – false  “hello” instanceof String – true  Final equals method: public boolean equals(Object o) { if (o instanceof Point) { Point other = (Point o); *** return x == other.x && y == other.y } else { return false; } } Returns false if o is not a Point or if it is a point but its x and y fields do not equal the implicit parameter’s fields ***You’d think we could remove this typecast line, since we’ve already determined that o is an instanceof a Point – but the code won’t compile without it Polymorphism:  Allows the same code to be used with different types of objects o and code will behave differently for different objects  Ex: the + operator adds integers and doubles, but it concatenates Strings  An array of a superclass type can store any subtype as elements o Ex: Employee[] e = { new Lawyer(), new Secretary(), new Marketer() };  Variables are able to refer to multiple kinds of objects o A variable T can refer to an object of any subclass of T  Ex: An employee variable can refer to any of its subclasses Employee frank = new Lawyer();  When we call methods on frank, it will behave like a Lawyer object  Advantage of polymorphism: We can write flexible code o Ex: a method that accepts an Employee as a parameter will accept any object of an Employee subclass  the method will behave differently for different types of objects public static void printSalary(Employee e) { System.out.print(e.getSalary()); } Let’s say lucy is a Lawyer object and stan is a LegalSecretary object. Legal Secretaries make $5000 more than other employees. Calling: printSalary(lucy); printSalary(stan); Produces this output: 50000.0 55000.0 ***If an object is defined as an Employee variable, you may only call methods on it that are part of the Employee class  takeDictation() is a method of the Secretary and LegalSecretary classes. But even if an object is defined as Employee ed = new LegalSecretary(); you can’t call the takeDictation() method on ed because the Employee class doesn’t contain this method  It’s legal to typecast ed as a secretary so we can use Secretary’s methods on ed ((Seretary) ed).takeDictation(); //this will work  You can define an object as any variable equal or higher in hierarchy than the actual object (For reference, in the example above, ed is an Employee variable and a LegalSecretary object) o A Secretary object can be defined as an Employee or Secretary variable  but NOT as a LegalSecretary (lower in hierarchy)  Variables can only be typecast as compatible objects o ed was stored as an Employee variable but is actually a LegalSecretary object o ed can be typecast as a Secretary or a LegalSecretary variable  but NOT as a Lawyer (not compatible) Class Diagrams and Tables: A way to keep track of inherited and overridden methods Consider the following classes: public class A { public void method1() { System.out.println(“A 1”); } public void method2() { System.out.println(“A 2”); } public String toString() { return “A”; } } public class B extends A { public void method2() { System.out.println(“B 2”); } } public class C extends A { public void method1() { System.out.println(“C 1”); } public String toString() { return “C”; } } public class D extends C { public void method2() { System.out.println(“D 2”); } } Class diagrams show the hierarchy relationships between the classes and which methods are overridden in the subclasses A method1 method2 toString B C method2 method1 toString D method2 Method output for each class: A B C D toString A A C C method1 A 1 A 1 C 1 C 1 method2 A 2 B 2 A 2 D 2 This can get tricky when some methods call other methods. Dr. Manandhar and the lecture notes specifically say we’ll have a question involving methods and polymorphism on tests, so practice making tables for some of the classes in the lecture slides From 02/03 lecture: Interfaces:  A supertype between classes without code sharing o classes implement interfaces in an is-a relationship  A list of method declarations  Like a class – but methods only have headers, no body statements o each class that implements an interface has to implement ALL of its methods  Another difference btw superclasses: can’t create objects of an interface type public interface name { public returnType methodName(parameter 1); public returnType methodName(); } Methods are abstract – declared, but have no statements. Different classes can implement the methods differently  Example: a Shape interface o every shape has an area and perimeter, but different shapes calculate them differently public interface Shape { public double getArea(); public double getPerimeter(); }  Implementing an interface: o declare the class/classes that implement it o implement each of the interface’s methods in the class public class name implements interfaceName { o Ex: class Rectangle implementing Shape interface public class Rectangle implements Shape { private double width; //Rectangle’s fields private double height; public Rectangle(double width, double height) { //a constructor this.width=width; this.height=height; } public double getArea() { return width*height; } public double getPerimeter() { return 2*(width+height); } } As you see, we’ve implemented both of Shape’s methods, getArea and getPerimeter. We could write other classes that implement Shape like Circle and Triangle, using different formulas for their methods.  Benefits of interfaces: o client programs can use polymorphism with any object of that interface type:  make an array of Shapes  write a method that accepts a general Shape parameter  return a Shape from a method o While a class can inherit only 1 superclass, it can implement many interfaces  Interface diagrams are constructed the same way as class diagrams but use dashed lines to show relationships btw interface/classes


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Anthony Lee UC Santa Barbara

"I bought an awesome study guide, which helped me get an A in my Math 34B class this quarter!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.