features of oo programming topic 4
play

Features of OO Programming Topic 4 Encapsulation Inheritance - PowerPoint PPT Presentation

Features of OO Programming Topic 4 Encapsulation Inheritance abstraction information hiding breaking problem up based on data types "Question: What is the object oriented way of Inheritance getting rich? code reuse specialization


  1. Features of OO Programming Topic 4 Encapsulation Inheritance abstraction information hiding breaking problem up based on data types "Question: What is the object oriented way of Inheritance getting rich? code reuse specialization "New code using old code." 1 CS 314 Inheritance 2 Encapsulation Inheritance Create a program to allow people to play the Another kind of relationship exists between game Monopoly data types Create classes for money, dice, players, the There are properties in Monopoly bank, the board, chance cards, community chest a street is a kind of property cards, pieces, etc. a railroad is a kind of property Some classes use other classes: a utility is a kind of property the board consists of spaces a player has money a piece has a position also referred to as composition CS 314 Inheritance 3 CS 314 Inheritance 4

  2. Inheritance What to Do? In Monopoly there is the concept of a If we have a separate class for Street, Property Railroad, and Utility there is going to be a lot All properties have some common traits of code copied they have a name hard to maintain they have a position on the board an anti-pattern they can be owned by players Inheritance is a programming feature to they have a price to buy allow data types to build on pre-existing data But some things are different for each of the types without repeating code three kinds of property How to determine rent when another player lands on the Property CS 314 Inheritance 5 CS 314 Inheritance 6 Inheritance in Java Mechanics of Inheritance Java is designed to encourage object 1. extends keyword oriented programming 2. inheritance of instance methods all classes, except one, must inherit from 3. inheritance of instance variables exactly one other class 4. object initialization and constructors The Object class is the cosmic super class 5. calling a parent constructor with super() The Object class does not inherit from any other class The Object class has several important methods: 6. overriding methods toString , equals , hashCode , clone , getClass 7. partial overriding, super.parentMethod() implications: 8. inheritance requirement in Java all classes are descendants of Object 9. the Object class all classes and thus all objects have a toString , equals , hashCode , clone , and getClass method 10. inheritance hierarchies toString , equals , hashCode , clone normally overridden CS 314 Inheritance 7 CS 314 Inheritance 8

  3. Nomenclature of Inheritance Clicker Question 1 In Java the extends keyword is used in the What is the primary reason for using class header to specify which preexisting class inheritance when programming? a new class is inheriting from public class Student extends Person A. To make a program more complicated Person is said to be the parent class of Student B. To copy and paste code between classes the super class of Student the base class of Student C. To reuse pre-existing code an ancestor of Student Student is said to be D. To hide implementation details of a class a child class of Person E. To ensure pre conditions of methods are met. a sub class of Person a derived class of Person a descendant of Person CS 314 Inheritance 9 CS 314 Inheritance 10 Clicker Question 2 Simple Code Example Create a class named Shape What is output when the main method is run? public class Foo { what class does Shape inherit from public static void main(String[] args) { what methods can we call on Shape objects? Foo f1 = new Foo(); add instance variables for a position System.out.println(f1.toString()); } override the toString method } Create a Circle class that extends Shape A. 0 add instance variable for radius B. null debug and look at contents try to access instance var from Shape C. Unknown until code is actually run. constructor calls D. No output due to a syntax error. use of key word super E. No output due to a runtime error. CS 314 Inheritance 11 CS 314 Inheritance 12

  4. Constructors Overriding methods Constructors handle initialization of objects any method that is not final may be When creating an object with one or more ancestors (every overridden by a descendant class type except Object) a chain of constructor calls takes place The reserved word super may be used in a constructor to same signature as method in ancestor call a one of the parent's constructors may not reduce visibility must be first line of constructor if no parent constructor is explicitly called the default, 0 may use the original method if simply want to parameter constructor of the parent is called add more behavior to existing if no default constructor exists a syntax error results If a parent constructor is called another constructor in the same class may no be called no super();this(); allowed. One or the other, not both good place for an initialization method CS 314 Inheritance 13 CS 314 Inheritance 14 The Keyword super super is used to access something (any protected or public field or method) from the super class that has been overridden Rectangle's toString makes use of the toString in ClosedShape my calling super.toString() Creating a SortedIntList without the super calling toString would result in infinite recursive calls Java does not allow nested supers super.super.toString() results in a syntax error even though technically this refers to a valid method, Object 's toString Rectangle partially overrides ClosedShapes toString CS 314 Inheritance 15

  5. A New Class Implementing SortedIntList Assume we want to have a list of ints, but Do we have to write a whole new class? that the ints must always be maintained in Assume we have an IntList class. ascending order Clicker 3 - Which of the following methods [-7, 12, 37, 212, 212, 313, 313, 500] would have to be changed? sortedList.get(0) returns the min add(int value) sortedList.get( list.size() 1 ) int get(int location) returns the max String toString() int size() int remove(int location) CS 314 Inheritance 17 CS 314 Inheritance 18 Overriding the add Method Clicker 4 public class IntList { First attempt private int size Problem? private int[] con } solving with insert method public class SortedIntList extends IntList { double edged sort public SortedIntList() { System.out.println(size); // Output? solving with protected } What protected really means } A. 0 B. null C. unknown until code is run D. no output due to a compile error E. no output due to a runtime error CS 314 Inheritance 19 20

  6. Problems What about this method? void insert(int location, int val) What about this method? More Example Code void insertAll(int location, IntList otherList) SortedIntList is not a good application ClosedShape and Rectangle classes of inheritance given the IntList we developed CS 314 Inheritance 21 CS 314 Inheritance 22 A ClosedShape class Shape Classes public class ClosedShape Declare a class called ClosedShape { private double myX; private double myY; assume all shapes have x and y coordinates public ClosedShape() override Object 's version of toString { this(0,0); } Possible sub classes of ClosedShape public ClosedShape (double x, double y) { myX = x; Rectangle myY = y; } Circle public String toString() Ellipse { return "x: " + getX() + " y: " + getY(); } Square public double getX(){ return myX; } public double getY(){ return myY; } Possible hierarchy } // Other methods not shown ClosedShape <- Rectangle <- Square CS 314 Inheritance 23 CS 314 Inheritance 24

  7. A Rectangle Class A Rectangle Constructor public class Rectangle extends ClosedShape { private double myWidth; public class Rectangle extends ClosedShape private double myHeight; { private double myWidth; public Rectangle() private double myHeight; { this(0, 0); } public Rectangle( double x, double y, public Rectangle(double width, double height) double width, double height ) { myWidth = width; myHeight = height; { super(x,y); } // calls the 2 double constructor in public Rectangle(double x, double y, // ClosedShape double width, double height) myWidth = width; { super(x, y); myWidth = width; myHeight = height; myHeight = height; } } public String toString() { return super.toString() + " width " + myWidth + " height " + myHeight; // other methods not shown } } } CS 314 Inheritance 25 CS 314 Inheritance 26 Initialization method Result of Inheritance public class Rectangle extends ClosedShape Do any of these cause a syntax error? { private double myWidth; private double myHeight; What is the output? public Rectangle() Rectangle r = new Rectangle(1, 2, 3, { init(0, 0); 4); } ClosedShape s = new CloseShape(2, 3); public Rectangle(double width, double height) System.out.println( s.getX() ); { init(width, height); } System.out.println( s.getY() ); System.out.println( s.toString() ); public Rectangle(double x, double y, double width, double height) System.out.println( r.getX() ); { super(x, y); init(width, height); System.out.println( r.getY() ); } System.out.println( r.toString() ); private void init(double width, double height) System.out.println( r.getWidth() ); { myWidth = width; myHeight = height; } CS 314 Inheritance 27 CS 314 Inheritance 28

Recommend


More recommend