Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 14

2 Note Set 14 Overview Relationships between classes Composition Inheritance

3 You might have noticed… Classes interact in your programs An instance method might receive an object as a parameter (even just at string) Once class might have an object of a different type as an instance variable public class MyClass { private Point x; A class might absorb the functionality of another class and add new attributes and functionality You make copious use of the Java API that contains a vast amount of pre-written, well-tested code

4 Software Reusability No need to “reinvent the wheel” Allows software developers to use Java API or other APIs that are well tested Rapid Application Development RAD Software reusability speeds the development of high-quality software

5 Composition – has-a When a class has references to objects of other classes as members. Great example of software re-use public class CardShoe { private Card [] cards; //other members } A CardShoe Object contains an array of Card objects CardShoe has access to all of the public members of the Card class. CardShoe does not have access to any of the private members of Card CardShoe HAS-A card

6 has-a & composition Car has-a motor motor has-a transmission Student has-a address address has-a street frame has-a panel Other examples?

7 Garbage Collection in Java When you allocate new objects, space is used in RAM as well as other system resources (network, files, etc). What releases these resources? JVM performs Garbage Collection to reclaim memory occupied by objects that are no longer in use Garbage collector does not necessarily release other system resources such as files, etc. System.gc(); //asks for GC to happen can use the finalize method in a class to clean-up allocated or reserved resources. Called by GC if GC attempts to reclaim an object – no guarantees though – GC might not actually run

8 Static class members Two types of members of a class Static variable ONLY one no matter how many objects of a type are allocated on a PER-CLASS basis Instance variables One for EVERY object that is instantiated on a PER-OBJECT basis public class Test { private int x; private int y; private static int count; //methods } //Before the two object are allocated Test obj1; Test obj2; Memory obj1 obj2 x x y y x x y y count Count exists in memory before any objects of type Test are allocated

9 When to Static??? When to Instance??? If every object of a particular type needs access to the same exact piece(s) of data, then make it static. If a division of employees makes produces more than 500K in profit for the company, then they get a 10% bonus based on salary public class Division { //An Employee obj Contains vital info including salary private Employee[] emps; public static double totalProfitForDiv; //other methods }

10 Static Methods Static Methods have access to static members (other static methods or static data) Cannot access instance variables – because they aren’t instance methods – doesn’t make sense Access using name of class and dot (.) public class Division { //An Employee obj Contains vital info including salary private Employee[] emps; public static double totalProfitForDiv; //other methods public static double getTotalProfit(){ //This method cannot access any element of emps return totalProfitForDiv; }

11 Inheritance One of the cornerstones of OOP A type of software re-use A new class is created by absorbing the members of an existing class and adding to or modifying those absorbed members Instead of the has-a relationship, Inheritance involves the is-a relationship. MultiplicationPanel is-a JPanel (has-a doesn’t make sense)

12 Terminology subclass vs superclass subclass inherits the members of the super class public class MultiplicationPanel extends JPanel In an is-a relationship, a subclass object may be treated as an object of its superclass. subclasssuperclass JPanel MultiplicationPanel

13 Class Object Object is the top level class in every inheritance hierarchy Every class extends Object implicitly if it doesn’t extend another class explicitly public class Person{ private String name; private String address; } Object Person public class Employee extends Person{ private double salary; } Object Person Employee

14 Some Inheritance Examples Student UndergradStudent Grad Student Circle Triangle Rectangle Shape BankAccount CheckingAccount SavingsAccount

15 Code public class Person { private String name; public void setName(String s) { name = s; } public String getName() { return name; } public class Student extends Person{ private double gpa; public void setGpa(double g) { gpa = g; } public double getGpa() { return gpa; } extends indicates inheritance Student has everything from person plus whatever new “stuff” it adds

16 Code public class Person { private String name; public void setName(String s){ name = s; } public String getName() { return name; } public class Student extends Person{ private double gpa; public void setGpa(double g) { gpa = g; } public double getGpa() { return gpa; } In Main : Student s = new Student(); name gpa s s has the data from both Person and Student Any public member from Person or Student can be called on object s.

17 Protected access public – can be seen/accessed/called from outside the class private – can only be seen/accessed/called from inside the class protected – can be only be seen/accessed/called from inside the class AND from subclasses ?1 – Does a subclass have access to directly modify a private instance variable of the superclass? _____________ ?2 – Does a subclass have access to directly modify a protected instance variable of the superclass? ____________

18 Code – Private Name public class Person { private String name; public void setName(String s) { name = s; } public String getName() { return name; } public class Student extends Person{ private double gpa; public void setGpa(double g) { gpa = g; } public double getGpa() { return gpa; } public void setData (String s, double g) { name = s; gpa = g; } method setData does not have access to private data member name from Person

19 Code – Protected Name public class Person { protected String name; public void setName(String s) { name = s; } public String getName() { return name; } public class Student extends Person{ private double gpa; public void setGpa(double g) { gpa = g; } public double getGpa() { return gpa; } public void setData (String s, double g) { name = s; gpa = g; } Now this is OK because name has protected visibility – can be directly access by subclasses Inherited members maintain their visibility in subclasses. So – a protected member of the superclass is a protected member of the subclass and a public member of the superclass is a public member of the subclass.


Download ppt "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14."

Similar presentations


Ads by Google