370 likes | 384 Views
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;
E N D
COMPUTER 2430Object Oriented Programming andData 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; valid = true; } return valid; } public String getName() { return name; } ... }
// 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) ... }
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()); } }
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()); }
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()); }
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()); }
Superclass and Subclasses Person Superclass Student Professor Subclasses UML Diagram
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?
// 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); } ... }
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.
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); } }
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! }
Are Constructors Inherited by the Subclasses? Constructors are Special Class Methods.
// 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; . . . }
// 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; . . . }
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; } }
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; } }
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); } }
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()); } }
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.
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.
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; } }
Superclass and Subclasses Person Superclass Student Professor Subclasses An instance of a subclass is an instance of the superclass.
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.
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() }
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); } }
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); } }
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 } }
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 } }
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 ) . . . }
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 } }
Lab 2 • Five (5) points • Due 10 pm, Friday, September 21 • Class Golfer • Lab Assistant
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; }
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; } }
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);
Quiz 1 Data Hiding Data Encapsulation Abstract Data Type Object Method Reference ___F____ ___E____ ___I____ ___H____ ___J____ ___K____