Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT AITI 2003 – Lecture 12 Inheritance. What is Inheritance?  Real World Example: We inherit traits from our mother and father. We also inherit traits.

Similar presentations


Presentation on theme: "MIT AITI 2003 – Lecture 12 Inheritance. What is Inheritance?  Real World Example: We inherit traits from our mother and father. We also inherit traits."— Presentation transcript:

1 MIT AITI 2003 – Lecture 12 Inheritance

2 What is Inheritance?  Real World Example: We inherit traits from our mother and father. We also inherit traits from our grandmother, grandfather, and ancestors. We might have similar eyes, the same smile, a different height….but we are essentially “derived” from our parents.  In Software, object inheritance is more well defined! Object types that are derived from other object types “resemble” their parent classes by inheriting/copying both state/variables and behavior/methods.

3 Picture Representation FOOD FRUIT APPLE String name void eat() String name void eat() int juiceLevel String name int juiceLevel void eat() void slice() Both variables and methods are inherited inherited

4 Inheritance All Java classes inherit implicitly from Object Classes that inherit from other classes can call methods from those classes, and can access their data members and methods (except for those that are private) Inheritance is useful because it allows you to reuse functionality and to manage complexity

5 Inheritance Example class A { int x; } class B extends A { int y; } class C extends B { int z; } A Object B C

6 Inheritance Example x y x z y x Instance of A has just A's fields. Its type is A (and Object). Instance of B has fields from A and B. Its type is A and B (and Object). Instance of C has fields from A, B and C. Its type is A, B and C (and Object).

7 Inheritance Example static public void main(String[] args) { A aa = new A(); A ab = new B(); /* a B is also an A */ A ac = new C(); /* a C is also an A */ B bb = new B(); B bc = new C(); C cc = new C(); /* not legal -> an A is not a B! */ // B ba = new A(); /* compile error */ }

8 Advantages of Inheritance  Complex classes and data types are already provided in java packages / libraries. We want to reuse these classes, and modify to our requirements: java.io input/ouput operations java.swing GUI components java.awt.geom 2D geometric objects java.applet Applets embedded in web pages java.util Data Structures – collection classes (Set, List,Tree, Vector) java.sql Supports Database access  Also – we want to write very abstract classes and then specialize (abstract classes). Example: We write an abstract shape class, and then keep on adding new shapes (next lecture).

9 Type Hierarchy Object-oriented systems allow classes to be defined in terms of other classes. For example, a square, triangle, and circle are all shapes. In object-oriented terminology, a square/triangle/circle are subclasses of the shape class. Similarly, the shape class is the superclass of all shape objects. Shape objects are also Objects. Object Shape CircleTriangleSquare Class/Type Hierarchy Inherit State and Behavior Specialize Point Pixel

10 Reusing Classes  We wrote a Point class. Now we want a ColorPoint class or a GraphicsPixelPoint class. We can just use existing variables/methods in the Point class (inherit these) and then write more code to add new functionality (extend the base class). We simply add a color field to our Point, or other graphics information for a pixel point.  We can use 2D geometry package classes already in java.awt.geom, and extend these classes to have specialized behavior. (Don’t need to reinvent Line, Circle, Rectangle classes.)

11 Less Elegant Way of “reuse” public class ColorPoint { private Point point; private Color color; // We wrap a color field around a Point object. public ColorPoint(Point p, Color color) { point = p; this.color = color; } } DELEGATION

12 Extending the Point Class/Type public class ColorPoint extends Point { private Color color; public ColorPoint(double x, double y, Color color) { super(x, y); // Make into a Point first. this.color = color; // Add info for new ColorPoint. } INHERITANCE

13 Constructors… Example I public class Shape { Shape() { System.out.println(“I am a Shape”); } } public class RoundShape extends Shape { RoundShape() { System.out.println(“I am a Round Shape”); } } public class Circle extends RoundShape { Circle() { System.out.println(“I am a Circle”); } public static void main(String[] args) { Circle x = new Circle(); }

14 Output of Program I am a Shape I am a Round Shape I am a Circle Default Constructors for superclasses called. When we construct a Circle, we are first making it a Shape, then a RoundShape, and finally a Circle.

15 Constructor Example II class Plant { private String kingdom; private String genus; private String species; private boolean poisonous; public Plant(String g, String s, boolean p) { kingdom= "Plantae"; genus= g; species= s; poisonous= p; } We write a base Plant class. All Plants are kingdom Plantae. Specific objects of the Plant type have a genus and species.

16 continued… class Tree extends Plant { private double crownSize; private double trunkSize; public Tree(String g, String s, double cs, double ts) { super(g, s, false); crownSize= cs; trunkSize= ts; } An object of Type Tree is also a Plant. Hence, it is of kingdom Plantae. We first call the super class constructor to make our object a Plant. Then we add crownSize and trunkSize modifications to make our object a Tree.

17 Hiding fields class SuperClass { int inheritData; double data; } class SubClass extends SuperClass { float data; } SubClass inherits inheritData. However, data is defined twice. In SubClass, we now have two data fields (one hidden) float data is newly defined. super.data can also be accessed in subclass.

18 Overriding Methods class SuperClass { … boolean isTrue() { } int totalSum(int x, int y) { } } class SubClass extends SuperClass { … // OVERRIDE totalSum method above, and add something. int totalSum(int x, int y) { super.totalSum(x, y); // Can Call Superclass method. // Do something more now, MODIFY System.out.println(“I can now add modifications.”); }

19 Access: Private,Public,Protected, no access specified (package) Super Class may contain members (methods or data) of type: –Private: Know About This: Access only internally in class. –Protected (a little unsafe, tricky, so avoided) Access: –To Methods of all Subclasses. –To all Classes in same package –Package (no access specified) Access by methods of classes in same package –Public: Know about this: Access to all classes everywhere.

20 Final and Static In SuperClass, can define a variable or method as final and static: Static: If subclass has access, then it will inherit the static variable or static method and it will be shared between all objects of a derived class AND base class. Final: If a variable, method, or class is defined as FINAL, then it cannot be overriden in a subclass.

21 Casting Objects From the inheritance concept, we now also can CAST objects into different types. OBJECT  POINT  COLORPOINT // Don’t need Cast if assigning a superclass type. Point p = new Point(4.4, 5.6); Object pHolder = (Object)p; Object pHolder = p; // Could just write this statement. // Need Casting if assigning a subclass type. Object x = new Point(3.0, 4.4); Point xPoint = (Point) x;

22 When Casting is Needed OBJECT  POINT  COLORPOINT In general, don’t need to cast unless we need methods of the subclass type. Object o = new ColorPoint(4.4, 3.4, new Color(“red”)); Can use: o.toString() and will get appropriate method. Can’t use: o.getX() since o has object type, not point. So cast to use getX() method.

23 Continued… OBJECT  POINT  COLORPOINT Object o = new ColorPoint(4.4, 3.4, new Color(“red”)); Can’t use: o.getX(); Using Casting to Point: Now Can Use: ( ( Point ) o ).getX(); OR can use ColorPoint cast to use very specific methods: ((ColorPoint) o).getColor();

24 Casting Example Vector v = new Vector(); v.addElement(new Rectangle(…..)); v.addElement(new Square(……)); v.addElement(new Circle(……)); for (int k = 0; k < v.size(); k++) { v.elementAt(k).toString(); ((v.elementAt(k)) Shape).draw(); // Cast needed }

25 Casting - Summary In general casting is not encouraged – becomes too confusing. A programmer should design superclasses properly so we don’t need to call specific methods all the time. But, many advantages (previous Vector exercise): We can draw all types of different shapes just by using a general draw method. We don’t have to call each rectangle, circle, square draw method separately.

26 Programming Example A Company has a list of Employees. It asks you to provide a payroll sheet for all employees. Has extensive data (name, department, pay amount, …) for all employees. Different types of employees – manager, engineer, software engineer. You have an old Employee class but need to add very different data and methods for managers and engineers. Suppose someone wrote a name system, and already provided a legacy Employee class. The old Employee class had a printData() method for each Employee that only printed the name. We want to reuse it, and print pay info. Borrowed with permission from Course 1.00 Notes

27 public … Main(…){ Employee e1…(“Mary”,“Wang”);... e1.printData(); // Prints Employee names.... } Employee e1 lastName firstName printData Encapsulation Message passing“Main event loop” private: REVIEW PICTURE

28 Employee class class Employee { // Data private String firstName, lastName; // Constructor public Employee(String fName, String lName) { firstName= fName; lastName= lName; } // Method public void printData() { System.out.println(firstName + " " + lastName);} } This is a simple super or base class.

29 Inheritance Class Employee firstName lastName printData() Class Manager salary firstName lastName Class Engineer hoursWorked wages firstName lastName printData() getPay() is-a printData() getPay() Already written: is-a You next write:

30 Engineer class Class Engineer extends Employee { private double wage; private double hoursWorked; public Employee(String fName, String lName, double rate, double hours) { super(fName, lName); wage = rate; hoursWorked = hours; } public double getPay() { return wage * hoursWorked; } public void printData() { super.printData(); // PRINT NAME System.out.println("Weekly pay: $" + Wage* hoursWorked); } } Subclass or (directly) derived class

31 Manager class class Manager extends Employee { private double salary; public Manager(String fName, String lName, double sal){ super(fName, lName); salary = sal; } public double getPay() { return salary; } public void printData() { super.printData(); System.out.println("Monthly salary: $“ + salary);} } Subclass or (directly) derived class

32 Inheritance… Class Manager Salary firstName lastName printData getPay Class SalesManager firstName lastName printData getPay Salary salesBonus is-a

33 SalesManager Class class SalesManager extends Manager { private double bonus; // Bonus Possible as commission. // A SalesManager gets a constant salary of $1250.0 public SalesManager(String fName, String lName, double b) { super(fName, lName, 1250.0); bonus = b; } public double getPay() { return 1250.0; } public void printData() { super.printData(); System.out.println(“Bonus Pay: $" + bonus; } } Derived class from derived class

34 Main method public class PayRoll { public static void main(String[] args) { // Could get Data from tables in a Database. Engineer fred = new Engineer(“Fred", "Smith", 12.0, 8.0); Manager ann = new Manager("Ann", "Brown", 1500.0); SalesManager mary= new SalesManager("Mary", “Kate", 2000.0); // Polymorphism, or late binding Employee[] employees = new Employee[3]; employees[0]= fred; employees[1]= ann; employees[2]= mary; for (int i=0; i < 3; i++) employees[i].printData(); } Java knows the object type and chooses the appropriate method at run time

35 Output from main method Fred Smith Weekly pay: $96.0 Ann Brown Monthly salary: $1500.0 Mary Barrett Monthly salary: $1250.0 Bonus: $2000.0 Note that we could not write: employees[i].getPay(); because getPay() is not a method of the superclass Employee. In contrast, printData() is a method of Student, so Java can find the appropriate version.


Download ppt "MIT AITI 2003 – Lecture 12 Inheritance. What is Inheritance?  Real World Example: We inherit traits from our mother and father. We also inherit traits."

Similar presentations


Ads by Google