Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 Generalization, inheritance, class hierarchy and graphics.

Similar presentations


Presentation on theme: "Week 9 Generalization, inheritance, class hierarchy and graphics."— Presentation transcript:

1 Week 9 Generalization, inheritance, class hierarchy and graphics

2 Review We have seen interfaces that specify capabilities (I.e., methods) but do not define them. Any class that implements an interface must define those methods. The big take-home point is that an interface doesn’t care how a particular class implements a method; only that it indeed has that method defined.

3 Interfaces An interface can extend one or more interfaces. This is the last of the five relationships. Can you name, draw a UML and show the code for each? Formal name is Generalization. Informal name is “extends” or “is a”. Two types of generalization: one from interface to interface and one from class to class.

4 Interfaces What does it mean to have an interface extend another interface? The method specifications in the super- interface(s) are carried down the chain. Any class that implements an interface, must define all the methods in its immediate parent interface(s) and all methods specified in any interfaces that its parent(s) extends from

5 Interfaces See UML Why? Gets complicated. However, the big idea is decoupling, generalization and therefore specialization. We want our code to be strong. Not muscle bound and rigid, but flexible and adaptable. This is as in Life. Remember who told you that.

6 Inheritance Classes can extend other classes as well. We say that classes inherit from other classes. What do they inherit? Anything that is public or protected. Unlike interfaces, though, a class can extend from only one class. A class can have only only one direct superclass. Multiple classes can extend from it, however; i.e., a class can have multiple subclasses.

7 Inheritance public class A extends B{ } We say:A is a B A inherits from B A extends B A is the subclass or child B is the superclass or parent B generalizes A

8 Class Hierarchy There is a hierarchy to all classes in Java. All classes have parents or superclasses. Except one. The top class is java.lang.Object. Learn it, love it, know it. Every class in Java is an Object, either directly or indirectly.

9 Why reinvent the wheel? We extend a class already written so we can specialize that class and save valuable time for ourselves. We get all the parent’s capabilities and parent’s parent’s capabilities and so on. Humans are lazy. This is doubly true for programmers. Remember who told you that.

10 Graphics javax.swing We give you the Classlibs with our modified javax.swing classes that help get you started in the fun, tedious, frustrating and ridiculously time consuming world of Java graphics.

11 Graphics We have containers, components and graphics Components are javax.swing mostly. Some components are JButton, JSlider and JLabel. We give you some specific containers like containers.Row and containers.Column. These act as layout managers for anything that gets added to them. Rows add items left to right and columns add items top to bottom. There is also the javax.swing.JFrame container. This is usually the top level or outermost container. Always remember to setVisible(true), pack(), and setDefaultCloseOperation(EXIT_ON_CLOSE).

12 Graphics Everything, except the outermost Frame is added to something else. Everything is nested in another container. We often extend these classes so we can specialize them. Some graphics are Ellipse, Rectangle, Image and Color. All graphics are added to a special container, which is called graphics.DrawingCanvas. Your fellow students and teaching staff will shun and ridicule you if you ever try to add a graphics.Rectangle3D to a containers.Row Always remember to setDimension(), setColor(), setLocation() of your graphics. Default dimension is (0,0), default color is gray, and default location is (0,0)  that is upper left corner of screen. The ONLY way to learn graphics is try it out and make lots of mistakes until it looks and behaves like you want it to.


Download ppt "Week 9 Generalization, inheritance, class hierarchy and graphics."

Similar presentations


Ads by Google