Object-Oriented Programming Concepts in Java

cs18000 problem solving and object oriented n.w
1 / 48
Embed
Share

Explore the concept of inheritance in Java programming, where classes share member definitions hierarchically. Learn how to create classes like Person, Student, and Professor, and understand the relationship between superclass and subclass.

  • Java Programming
  • Inheritance
  • Object-Oriented
  • Classes
  • Hierarchical Relationship

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS18000: Problem Solving and Object-Oriented Programming

  2. Inheritance

  3. Problem Sometimes classes have related or overlapping functionality Consider a program for keeping track of personnel at the university Need a Person class to keep information But also might want special classes for Student: to include grades or classes taken Professor: to include salary and rank 3

  4. Person Class public class Person { private String name; private String address; public Person(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } 4

  5. Student Class (1) public class Student { private String name; private String address; private String[] classes; private String[] grades; public Student(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } Very redundant with Person class return address; } // continued public String getAddress() { 5

  6. Student Class (2) // continued public void setAddress(String address) { this.address = address; } public String[] getClasses() { return classes; } public void setClasses(String[] classes) { this.classes = classes; } Unique to Student class // and more } 6

  7. Inheritance Rather than duplicating members (fields and methods) among these classes, Java allows classes to share member definitions in a hierarchical fashion One class can extend another inheriting fields and methods from it Terminology: the subclass inherits from the superclass 7

  8. Example Class Person has fields name, address, as well as accessors and mutators Class Student extends Person Inherits the fields and methods from Person Adds classes and grades (and more accessors and mutators) Class Professor extends Person Inherits the fields and methods from Person Adds rank and salary (and more accessors and mutators) Common fields and methods go in Person, and are inherited by its subclasses Class-specific fields and methods go in their respective class 8

  9. Student Subclass Subclass only contains the differences public class Student extends Person { private String[] classes; private String[] grades; public Student(String name, String address, String[] classes, super(name, address); this.classes = classes; this.grades = grades; } String[] grades) { public String[] getClasses() { return classes; } public void setClasses(String[] classes) { this.classes = classes; } } 9

  10. Object Class One designated class in Java Object is the root of all classes Any class that doesn t extend another class implicitly extends the Object class A class can only extend one other class (but can implement multiple interfaces) Java is a single inheritance system C++ is a multiple inheritance system 10

  11. Subclass Object Contains its fields as well as all the fields defined in its superclasses Student object Fields defined in Object name Fields defined in Person address classes Fields defined in Student grades 11

  12. Object Class Methods The Object class has a small number of public methods. Samples clone() makes a copy of the object equals(Object e) compares for equality toString() returns a String representation The toString() method is very handy: It is called by printf and similar methods when a String is needed (e.g., for printing) You can override it in your classes to get something more descriptive 12

  13. Constructor Chaining When constructing an object of a class, it is important that all the constructors up the inheritance chain have an opportunity to initialize the object under construction Called constructor chaining Java enforces constructor chaining by inserting implicit calls to superclass constructors You can override this behavior by inserting your own calls 13

  14. Constructor Rules Every class must have at least one constructor The first line of every constructor must be a call to another constructor. 14

  15. Default Constructors If you don t provide any constructors in a class, Java provides one for you: public ClassName() { super(); } The statement super(); calls the 0-argument constructor in the superclass 15

  16. Default Chaining If you doprovide a constructor by default Java inserts the statement super(); at the beginning to enforce chaining 16

  17. Explicit Chaining You can explicitly call a superclass constructor yourself Useful for passing arguments up the line to initialize the object using superclass constructors See the Student example earlier Calls super(name, address) Invokes constructor in Person to initialize these fields 17

  18. Explicit Chaining The first step in each constructor is to either Call another constructor in the current class, or Call a superclass constructor To call another constructor, use this( ) To call a superclass constructor, use super( ) You can do one or the other but not both In either case, the argument types are matched with the class constructors to find a match If no explicit this( ) or super( ) is provided in a constructor, Java automatically calls super() (the superclass constructor with no arguments) 18

  19. Constructor Complications If the base class does not have a parameterless constructor, the derived class constructor must make an explicit call, with super( ), to an available constructor in the base class 19

  20. super() and this() Recall that this( ) can be used to call another constructor in the current class If you call this( ), Java does not call super() OK, since, the constructor you call must either call this( ) or super( ), so super( ) will eventually be called If specified explicitly, calls to super( ) or this( ) must be the first statement in a constructor ensures proper initialization by superclass constructors before subclass constructors continue 20

  21. Wheel Example (1) No extends , so implicitly extends Object class public class Wheel { private double radius; public Wheel(double radius) { this.radius = radius; } } Since constructor provided, no default (0 argument) constructor provided or available. Since no call to super( ) or this( ), Java inserts call to super(), Object constructor public class Tire extends Wheel { private double width; public Tire(double radius, double width) { // super(radius); this.width = width; } } If no call to super( ), Java inserts call to super(), which doesn t exist. Result -> syntax error 21

  22. Wheel Example (1) public class Wheel { private double radius; public Wheel(double radius) { this.radius = radius; } } public class Tire extends Wheel { private double width; public Tire(double radius, double width) { super(radius); this.width = width; } } With call to super( ), superclass constructor called with specified argument 22

  23. Terminology Student extends Person Student is a subclass of Person Person is a superclass of Student Person is the parent class, Student is the child class Person is the base class, Student is the derived class Superclass/subclass may be counterintuitive since the subclass has more stuff than the superclass Instead, think superset/subset . Objects in class Student are a subset of objects in class Person 23

  24. More Inheritance Access Restrictions and Visibility Overriding and Hiding instanceof

  25. Reminder: Java Access Modifiers Can apply to members: fields and methods Modifiers control access to members from methods in other classes This list is from least to most restrictive: Keyword Restriction None (any other method can access) public Only methods in the class, subclasses, or in classes in the same package can access protected [none] Only methods in the class or in classes in the same package can access (called package private ) Only methods in the class can access private 25

  26. Subclass Access Subclasses cannot access private fields in their superclasses Two options: Leave as is; provide accessors and/or mutators Change private to protected Protected allows subclass access to superclass fields (even if the subclass is in a different package) General advice: use accessors and mutators 26

  27. Overloading vs Overriding Overloading In the same class, two methods with the same name, but different signatures Overriding In a superclass and subclass, two methods with the same name, same signature 27

  28. Overriding Methods A subclass method with the same signature as a superclass method overrides the superclass method The subclass method is executed instead of the superclass method Useful to change the behavior of a method when applied to a subclass object A method that is not overridden is inherited by (available to) the subclass 28

  29. Accessing Overridden Methods Overridden methods can also be accessed using super: super.method( ) 29

  30. Overriding Methods public class Person { public void display() { System.out.println(name,address); } } public class Student extends Person { public void display() { System.out.println(name,address,classes,grades); } } public class Student extends Person { public void display() { super.display(); System.out.println(classes,grades); } } 30

  31. The instanceof Operator It is possible to determine if an object is of a particular class (or subclass) The expression (objectA instanceof ClassB) evaluates true if the object referenced by objectA is an instance of the class ClassB (student1 instanceof Student) is true (student1 instanceof Person) is true 31

  32. Exceptions try-catch throw

  33. Handling Error Situations public class Summer { public static void main(String[] args) { Scanner in = new Scanner(System.in); int number; // number that is input int sum = 0; // sum of values int c = 0; // how many values read double average; // average value while (in.hasNextInt()) { number = in.nextInt(); c = c + 1; sum = sum + number; } if (c > 0) { average = sum / c; System.out.printf("%d values, sum %d, average %f", c, sum, average); } else System.out.printf( no values, no sum, no average"); } } 33

  34. What to do when an error occurs? Old style: return an error code Caller must check on each call Did the method return an error? Requires a special value to indicate error Example: open( file.txt ) in C returns a file descriptor that allows reading from the file Returns -1 (an invalid file descriptor) if open fails Programmer must remember to check for -1 34

  35. Java Approach: Exceptions Write code without worrying about checking for errors When an error is detected, an exception is thrown Java system stops execution of the current method Searches for an exception handler to deal with the problem Search begins in the current method and continues to caller -> caller s caller -> caller s caller s caller -> -> main -> 35

  36. The Call Stack Currently executing method method4 method3 Each method frame on the stack contains storage for the parameters and local variables of the method Methods waiting for called method to complete method2 method1 main 36

  37. Searching for a Handler Exception occurs here method4 method3 Java searches for an exception handler starting with the current method, then caller, caller s caller, etc. Each method can either catch the exception or throw it on to its caller method2 method1 main 37

  38. Catching an Exception: Basic Syntax Basic syntax of the try-catch statement try clause try { } catch (Exception e) { statements-to-recover-from-exception; } statements-that-might-throw-exception; catch clause Note: Exception is a class name, not a reserved word; e is an object reference. 38

  39. Passing the Buck: Throws A method can declare that it throws an exception without catching it New syntax public void doit(int x) throws Exception { statements-that-might-throw-an-exception; } No try-catch needed! Note: throws is a keyword, Exception is a class name 39

  40. Exception Class Exceptions are objects The exception object is an instance of class Exception, or a subclass of Exception Created using new (just like any object) Two useful methods e.getMessage() get the associated text message e.printStackTrace() prints the current call stack 40

  41. Exception Class Hierarchy Exception IOException YourException FileNotFoundException RuntimeException NullPointerException ArithmeticException IndexOutOfBoundsException 41

  42. Checked vs. Unchecked Exceptions The RuntimeException class and its subclasses are unchecked exceptions: Generally indicate program or JVM error (null pointer, arithmetic, invalid array index, etc.) Typically: no recovery is possible; program crashes All other Exceptions are checked Generally indicate user error (e.g., file not found) Must check for them (try-catch or throws) Typically: recoverable (e.g., prompt user again) 42

  43. EOF: Unchecked Exception import java.util.Scanner; public class EOF { public static void main(String[] args) { Scanner s = new Scanner(System.in); while (true) { String word = s.next(); System.out.println(word); } } } Throws NoSuchElementException at end of file 43

  44. EOF: Catching NoSuchElement import java.util.Scanner; import java.util.NoSuchElementException; public class EOF { public static void main(String[] args) { Scanner s = new Scanner(System.in); while (true) { try { String word = s.next(); System.out.println(word); } catch (NoSuchElementException e) { System.out.printf("NoSuchElementException: %s\n ,e.getMessage()); break; } } } } 44

  45. Scanner: Catching FileNotFound import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class LineCounter { public static void main(String[] args) { File f = new File(args[0]); try { Scanner s = new Scanner(f); int c = 0; while (s.hasNextLine()) { s.nextLine(); c++; } System.out.printf("read %d lines from file %s\n", c, f); } catch (FileNotFoundException e) { System.out.printf("Exception: %s\n", e.getMessage()); } } } 45

  46. Making Your Own Exception Class public class StudentNotFoundException extends Exception { public StudentNotFoundException (String message) { super (message); } } public class FindStudent { public Student search (int student) throws StudentNotFoundException { if (...) { throw new StudentNotFoundException (Integer.toString(student)); } } } 46

  47. Catching Multiple Exceptions It is possible to catch multiple exceptions from one try Catches must be ordered from lowest subclass to highest superclass try { } catch (StudentNotFoundException e) { // code to handle student not found } catch (NullPointerException e) { // code to handle null pointer } catch (Exception e) { // code to handle all other exceptions } statements-that-may-throw-exceptions; 47

  48. Finally Clause Finally, if present, a finally clause is executed after all other try/catch statements The finally clause is guaranteed to execute, even if earlier clause returns try { statements-that-may-throw-exceptions; } catch (StudentNotFoundException e) { // code to handle student not found } finally { // code to clean things up } 48

Related


More Related Content