190 likes | 300 Views
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
E N D
CSE 1341 - HonorsPrinciples 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 • 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
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
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
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?
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
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
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
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 (.)
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)
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.
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
Some Inheritance Examples Student UndergradStudent Grad Student Shape Circle Triangle Rectangle BankAccount CheckingAccount SavingsAccount
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; } }
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.
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
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
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.