1Using Polymorphism11/26/20072Opening Discussion■Let's look at some solutions to the interclass problem.■Minute essay responsesArrays and bracketsThe add method can add to a list at any point.The differences between arrays and lists are syntax and the array is a fixed size with a fixed number of slots.Which you use depends on application, but most people use arrays by default because of the simpler syntax.Multidimensional arrays.3Inheritance and Subtyping■Last class we talked about inheritance, what it provides to us, and how we do it in Java.Code reuseSubtyping■We use the extends keyword when a class inherits from a class or implements when a class inherits from an interface.■Abstract classes can't be instantiated. Abstract methods aren't implemented in that class.■Interfaces are like completely abstract classes.■All classes in Java inherit from the Object class.4Polymorphism■The term polymorphism comes from Greek and it literally means many shapes. In programming it means many types.■A polymorphic type is a type that can represent many other types. A polymorphic operation/method is one that can work with many different types.■Polymorphism gives us a lot of power in our programming and can drastically reduce the amount of code we have to write because one method can be used with many different types.■Subtyping from inheritance gives us polymorphism.5GUI Libraries in Java■The GUI libraries in Java probably use inheritance more than any other set of libraries.■There are two main packages that you use when doing GUIs in Java.java.awt – the Abstract Window Toolkit was the original GUI library. It still has important helper classes.javax.swing – this is a “pure” Java library built on top of AWT that we will be using.■Let's look in that API at the Swing classes.6Components■Everything that goes into a GUI in Java inherits from Component. (In Swing they inherit from JComponent.)■This is where polymorphism comes into play because the code can be written to work with a Component and it will work with a button, check box, drop box, or any other component.■The Swing library has all the basic components you would expect to be able to put into GUIs as well as many others.7JFrame■For windows in the Swing library you use JFrames.■Let's look at the JFrame class and write some code to open up a window.■Now let's add one component to that frame.8Layout Managers■What we just wrote only has one limitation, we only got to add one component to the frame.■For adding multiple elements we use Layout Managers. These are classes that control how things go into a GUI container.■There are many layout types, but we will only deal with a few.FlowLayoutGridLayoutBorderLayout■Some Swing components have their own layouts.9Minute Essay■Have you done any of the reading recently? What have you read? Has it helped?■Interclass Problem – Write a GUI that brings up a frame and puts one button and one text box in the frame. Experiment with how to lay it out to make it look
View Full Document