1 / 37

COMPUTER 2430 Object Oriented Programming and Data Structures I

COMPUTER 2430 Object Oriented Programming and Data Structures I. public class Person { private String name, address, phone, email; private Date dob; public boolean setName (String s) { boolean valid = false; if (s.length() > 1) { name = s;

binghama
Download Presentation

COMPUTER 2430 Object Oriented Programming and Data Structures 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. COMPUTER 2430Object Oriented Programming andData Structures I

  2. public class Person { private String name, address, phone, email; private Date dob; public boolean setName (String s) { boolean valid = false; if (s.length() > 1) { name = s; valid = true; } return valid; } public String getName() { return name; } ... }

  3. // Subclasses can have additional members public class Student extends Person { private float gpa; public boolean setGPA (float value) public float getGPA() . . . } public class Professor extends Person { private boolean tenured; public boolean applyGrant (String grantName) public void teachCourse (String courseName) ... }

  4. public class CS2430Sx { public static void main(String[] args) { Student student = new Student(); student.setName("Frank"); student.setGPA(3.5f); System.out.println("Student: " + student.getName() + ", GPA: " + student.getGPA()); Professor prof = new Professor(); prof.setName("Qi"); System.out.println("Professor: " + prof.getName()); } }

  5. import java.util.Scanner; public static void main(String[] args) { Scanner sc = new Scanner( System.in ); String str; Student student = new Student(); str = sc.next(); // next string student.setName(str); System.out.println("Student: " + student.getName()); }

  6. import java.util.Scanner; public static void main(String[] args) { Scanner sc = new Scanner( System.in ); String str; Student student = new Student(); // str = sc.next(); // student.setName(str); System.out.println("Enter a name: "); student.setName( sc.next() ); System.out.println("Student: " + student.getName()); }

  7. import java.util.Scanner; import java.io.File; public static void main(String[] args) { Scanner sc; try { sc = new Scanner( new File("Lab2_1.in") ); } catch (Exception ex) { sc = new Scanner( System.in ); } String str; Student student = new Student(); student.setName( sc.next() ); System.out.println("Student: " + student.getName()); }

  8. Superclass and Subclasses Person Superclass Student Professor Subclasses UML Diagram

  9. Inheritance • Do subclasses inherit public members from the superclass? • Do subclasses inherit private members from the superclass? • Do subclasses inherit methods from the superclass? • Do subclasses inherit data members from the superclass?

  10. // Subclasses can have additional members public class Student extends Person { private float gpa; public boolean setGPA (float value) public float getGPA() /** Subclass Person inherits private data member name, but CANNOT access name in the code. */ public void print () { System.out.println(name); } ... }

  11. Inheritance • Subclasses CANNOT access directly private members defined in the superclass. • Private members can be accessed directly only within the class where they are defined. • Protected members can be accessed directly from subclasses.

  12. public class Person { protected String name, address, phone, email; private Date dob; . . . } public class Student extends Person { public void print () { // Works with protected members System.out.println(name); } }

  13. import java.util.Scanner; public static void main(String[] args) { Scanner sc = new Scanner( System.in ); String str; Student student = new Student(); System.out.println("Enter a name: "); student.setName( sc.next() ); student.print(); // Works! }

  14. Are Constructors Inherited by the Subclasses? Constructors are Special Class Methods.

  15. // Java provides the default constructor public class Person { protected String name, address, phone, email; . . . } // Java provides the default constructor public class Student extends Person { private float gpa; . . . }

  16. // Add a constructor public class Person { . . . public Person( String inName ) { name = inName; } . . . } // Does class Student inherit the constructor? // NO! public class Student extends Person { private float gpa; . . . }

  17. public class Person { protected String name, address, phone, email; . . . public Person( String inName ) } // Add a constructor public class Student extends Person { . . . public Student( String inName ) { // Still not working! name = inName; gpa = 3.0f; } }

  18. public class Person { protected String name, address, phone, email; . . . public Person( String inName ) } public class Student extends Person { . . . public Student( String inName ) { // Have to call super to inherit! super(inName); gpa = 3.0f; } }

  19. public class Person { protected String name, address, phone, email; . . . public Person( String inName ) } public class Student extends Person { . . . public Student( String inName ) { gpa = 3.0f; // supper() must be the first statement! super(inName); } }

  20. public class CS2430Sx { public static void main(String[] args) { Person person = new Person("Rocky"); System.out.println("Person: " + person.getName()); Student student = new Student("Frank"); System.out.println("Student: " + student.getName() + ", GPA: " + student.getGPA()); } }

  21. Inheritance • Subclasses inherit all members from superclass, except constructors. • Inside each constructor of a subclass, a constructor of the superclass must be called at the very beginning to inherit from the superclass. • The constructor of the superclass is called using super() with required parameters if any.

  22. Default Constructor • If a class does not provide any constructors, Java will add the default constructor. • If a superclass has the default constructor and super() is not called at the beginning of a constructor of a subclass, Java will call the default constructor of the superclass.

  23. public class Person { public Person() public Person( String inName ) . . . } public class Student extends Person { . . . public Student( String inName ) { // Java calls super() here for you gpa = 3.0f; name = inName; } }

  24. Superclass and Subclasses Person Superclass Student Professor Subclasses An instance of a subclass is an instance of the superclass.

  25. Superclass and Subclass • A variable of a superclass can point to instances of its subclasses, but a variable of a subclass can not point to an instance of its superclass (unless it’s an instance of the subclass). • On a variable of a superclass, any methods of the superclass can be invoked, but additional methods of its subclass cannot be invoked, even the instance pointed by the variable is an instance of the subclass. • This is determined at the compiling time.

  26. public class Person { protected String; public Person( String inName ) public boolean setName( String inName ) public String getName() } public class Student extends Person { private float gpa; public Student( String inName ) public void setGPA( float inGPA ) public float getGPA() }

  27. public class CS2430Sx { public static void main(String[] args) { Person person; // pointer to instances of Person Student student; // pointer to instances of Student // Valid? person = new Person("Rocky"); // Valid? person.setName("Mike"); // Valid? person.setGPA(3.1f); } }

  28. public class CS2430Sx { public static void main(String[] args) { Person person; // pointer to instances of Person Student student; // pointer to instances of Student // Valid? student = new Student("Frank") ; // Valid? student.setName("Mike"); // Valid? student.setGPA(3.1f); } }

  29. public class CS2430Sx { public static void main(String[] args) { Person person; // pointer to instances of Person Student student; // pointer to instances of Student // Valid? person = new Student("Frank"); // Yes // Valid? student = new Person("Mike"); // No } }

  30. public class CS2430Sx { public static void main(String[] args) { Person person; // pointer to instances of Person Student student; // pointer to instances of Student // Valid person = new Student("Frank"); // Valid? person.setName("Mike"); // Yes: method of superclass // Valid? person.setGPA(3.1f); // No: method of subclass } }

  31. public class Person { protected String name; public Person( String inName ) /** Copy constructor */ public Person( Person p ) { // this.name = p.name; name = p.name; } public boolean setName( String inName ) . . . }

  32. public class CS2430Sx { public static void main(String[] args) { Person person = new Person("Rocky"); Student student = new Student("Frank"); // Valid? Person p = new Person(person); // Yes: copy constructor // Valid? p = new Person(student); // Yes: the student instance is an instance of Person // only name is copied, not gpa } }

  33. Lab 2 • Five (5) points • Due 10 pm, Friday, September 21 • Class Golfer • Lab Assistant

  34. Quiz 1 public class IntList { private final int MAX = 100; private int[] myList = new int[MAX]; private int num = 0; public boolean addInt (int intVal) { if (num == MAX) // >= is also good return false; myList[num ++] = intValue; return true; }

  35. Quiz 1 public class IntList { . . . public int find (int intValue) { for (int i = num – 1; i >= 0; i --) if (myList[i] == intValue) return i; return -1; } }

  36. Quiz 1 // Write Java code as a user // to create an IntList object called theList IntList theList = new IntList(); // Write a segment of Java code as a user that // uses a loop to add 10 values 10, 20, 30, … 100 // to theList. You can use magic number here, but // you must use a for loop to add the values. for (int i = 10; i <= 100; i += 10) theList.addInt(i);

  37. Quiz 1 Data Hiding Data Encapsulation Abstract Data Type Object Method Reference ___F____ ___E____ ___I____ ___H____ ___J____ ___K____

More Related