20180220.pdf - Object-Oriented Programming Concepts CSE 219...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 219: Computer Science III Stony Brook University Object-Oriented Programming Concepts
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Subclasses The concept of a subclass is probably the most characteristic (and powerful) feature of object- oriented programming. class Vehicle {     int weight;     int toll() { return(3); }   }   class Car extends Vehicle {     int number_of_passengers;   }   class Truck extends Vehicle {     int number_of_axles;     int toll() {       return(super.toll() + weight/1000 + number_of_axles);     }   }
Image of page 2
Extension and Overriding The classes Car and Truck both extend Vehicle with additional fields and methods. Car and Truck are subclasses of Vehicle . Vehicle is a superclass of Car and Truck . Car and Truck both inherit the field weight and the method toll from the superclass Vehicle . Class Truck overrides the inherited method toll with a specialized version.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
UML Class Diagram inheritance edge
Image of page 4
The Power of Inheritance Inheritance makes it possible for a subclass to include only what is different from its superclass. Subclasses help avoid code repetition . Eases maintenance. Subclasses facilitate code re-use . Reduces work required in the future.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The “Is-a” Relationship Each instance of a subclass “ is a ” instance of its superclass. A subclass instance understands all “messages” (methods) that a superclass instance does (and possibly more). An instance of a subclass is usable everywhere an instance of the superclass is usable. This is the so-called substitution principle . Whenever you create a subclass, ask yourself whether the “is- a” relationship makes sense for it. If not, a subclass is probably not the right thing to be using.
Image of page 6
Class Hierarchies Inheritance can often extend for multiple levels: javafx.scene.control Class DialogPane java.lang.Object javafx.scene.Node javafx.scene.Parent javafx.scene.layout.Region javafx.scene.layout.Pane javafx.scene.control.DialogPane
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Java Class Hierarchy In Java, the class hierarchy forms a tree, with the primordial class Object at the root. Every class ultimately inherits from class Object . Every class (except Object ) has exactly one superclass. Why is that?
Image of page 8
Multiple Inheritance: The “Diamond Problem” Which version of toll should SUV inherit?
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Avoiding the Diamond Multiple inheritance situations lead to ambiguities which are hard to resolve, in general. The single-superclass restriction avoids the diamond. The Java designers chose this simpler approach rather than to try to solve the diamond problem.
Image of page 10
Inheritance and Initialization In Java, initialization of objects is performed by so- called constructors . More properly, they should have been called initializers , because they don’t actually construct anything (that is done by new ).
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern