1 / 19

CSE 1341 - Honors Principles of Computer Science I

CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 13. Note Set 13 Overview. Relationships between classes Composition Inheritance. You might have noticed…. Classes interact in your programs

questa
Télécharger la présentation

CSE 1341 - Honors Principles of Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu Note Set 13

  2. Note Set 13 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 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 When a class has references to objects of other classes as members. Great example of software re-use

  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 //Before the two object are allocated Test obj1; Test obj2; public class Test { private int x; private int y; private static int count; //methods } obj1 x y obj2 Count exists in memory before any objects of type Test are allocated Memory x count y • 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

  9. When to Static??? When to Instance??? public class Division { //An Employee obj Contains vital info including salary private Employee[] emps; public static double totalProfitForDiv; //other methods } 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

  10. Static Methods 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; } } 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 (.)

  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. • MultiplicationPanelis-aJPanel (has-a doesn’t make sense)

  12. Terminology subclass superclass JPanel MultiplicationPanel • subclassvssuperclass • 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.

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

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

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

  16. Code public class Person { private String name; public void setName(String s){ name = s; } public String getName() { return name; } } In Main : Student s = new Student(); name s gpa public class Student extends Person{ private double gpa; public void setGpa(double g) { gpa = g; } public double getGpa() { return gpa; } } 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 ?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? ____________ 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

  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.

More Related