Inheritance Review CSC 171 FALL 2004 LECTURE 19

  • Slides: 56
Download presentation
Inheritance Review CSC 171 FALL 2004 LECTURE 19

Inheritance Review CSC 171 FALL 2004 LECTURE 19

READING l Read Horstmann, Chapter 11 l Look at Chapter 12 – Will not

READING l Read Horstmann, Chapter 11 l Look at Chapter 12 – Will not be on MT or Final – One lab l Begin Reading Chapter 13

Object: The Cosmic Superclass l All classes extend Object l Most useful methods: –

Object: The Cosmic Superclass l All classes extend Object l Most useful methods: – String to. String() – boolean equals(Object other. Object) – Object clone() // Clone copies objects

Abstract Classes l We saw with Interfaces, a way to define a signature for

Abstract Classes l We saw with Interfaces, a way to define a signature for a method, without implementation – requiring later implementation l We can do this in the context of inheritance using sub/superclass hierarchies using abstract methods and classes

Abstract Methods public abstract class Bank. Account { public abstract void deduct. Fees(); }

Abstract Methods public abstract class Bank. Account { public abstract void deduct. Fees(); } l Can’t be instantiated

l __________ is a method for extending existing classes by adding methods and fields.

l __________ is a method for extending existing classes by adding methods and fields.

l __Inheritance_____ is a method for extending existing classes by adding methods and fields.

l __Inheritance_____ is a method for extending existing classes by adding methods and fields.

l The more general class is called a ___________. The more specialized class that

l The more general class is called a ___________. The more specialized class that inherits from it is called the ___________.

l The more general class is called a ___SUPERCLASS___. The more specialized class that

l The more general class is called a ___SUPERCLASS___. The more specialized class that inherits from it is called the ____SUBCLASS_____.

l Every class extends the _______ class either directly or indirectly.

l Every class extends the _______ class either directly or indirectly.

l Every class extends the ___Object______ class either directly or indirectly.

l Every class extends the ___Object______ class either directly or indirectly.

l Inheriting from a class differs from realizing an interface. The subclass inherits ________

l Inheriting from a class differs from realizing an interface. The subclass inherits ________ and _______ from the superclass.

l Inheriting from a class differs from realizing an interface. The subclass inherits _____behavior_______

l Inheriting from a class differs from realizing an interface. The subclass inherits _____behavior_______ and ___state______ from the superclass.

l One advantage of inheritance is ____________.

l One advantage of inheritance is ____________.

l One advantage of inheritance is ___code reuse______.

l One advantage of inheritance is ___code reuse______.

l When defining a subclass, you specify added __________, added ________, and changed or

l When defining a subclass, you specify added __________, added ________, and changed or __________ methods.

l When defining a subclass, you specify added _instance fields____, added _methods______, and changed

l When defining a subclass, you specify added _instance fields____, added _methods______, and changed or _overridden____ methods.

l Sets of classes can form complex inheritance ____________.

l Sets of classes can form complex inheritance ____________.

l Sets of classes can form complex inheritance _____hierarchies______.

l Sets of classes can form complex inheritance _____hierarchies______.

l A subclass has _____ access to private fields fo its superclass.

l A subclass has _____ access to private fields fo its superclass.

l A subclass has _____NO___ access to private fields of its superclass.

l A subclass has _____NO___ access to private fields of its superclass.

l Use the __________ keyword to call a method of the superclass.

l Use the __________ keyword to call a method of the superclass.

l Use the _______super_______ keyword to call a method of the superclass.

l Use the _______super_______ keyword to call a method of the superclass.

l To call the superclass constructor, you use the _______ keyword in the ______

l To call the superclass constructor, you use the _______ keyword in the ______ statement of the subclass constructor.

l To call the superclass constructor, you use the __super______ keyword in the __first______

l To call the superclass constructor, you use the __super______ keyword in the __first______ statement of the subclass constructor.

l A _________ reference can be cast/converted to a ______ reference, but not the

l A _________ reference can be cast/converted to a ______ reference, but not the other way around.

l A ______subclass_____ reference can be cast/converted to a ___superclass____ reference, but not the

l A ______subclass_____ reference can be cast/converted to a ___superclass____ reference, but not the other way around.

l An ________ method is one whose implementation is not specified.

l An ________ method is one whose implementation is not specified.

l An __abstract____ method is one whose implementation is not specified.

l An __abstract____ method is one whose implementation is not specified.

l An __________ class cannot be instantiated.

l An __________ class cannot be instantiated.

l An _____abstract____ class cannot be instantiated.

l An _____abstract____ class cannot be instantiated.

l A field or method that is not declared as public, private, or protected

l A field or method that is not declared as public, private, or protected can be accessed by all classes in the same ______, which is usually not desirable.

l A field or method that is not declared as public, private, or protected

l A field or method that is not declared as public, private, or protected can be accessed by all classes in the same ____package_______, which is usually not desirable.

l A field or method that is not declared as public, private, or protected

l A field or method that is not declared as public, private, or protected can be accessed by all classes in the same _______.

l A field or method that is not declared as public, private, or protected

l A field or method that is not declared as public, private, or protected can be accessed by all classes in the same _package____.

l __________ features can be accessed by all subclasses and all classes in the

l __________ features can be accessed by all subclasses and all classes in the same package.

l _Protected_______ features can be accessed by all subclasses and all classes in the

l _Protected_______ features can be accessed by all subclasses and all classes in the same package.

l You should define the _________ method to yield a string that describes the

l You should define the _________ method to yield a string that describes the object state.

l You should define the _to. String____ method to yield a string that describes

l You should define the _to. String____ method to yield a string that describes the object state.

l You should define the __________ method to test whether two objects have equal

l You should define the __________ method to test whether two objects have equal state.

l You should define the _____equals______ method to test whether two objects have equal

l You should define the _____equals______ method to test whether two objects have equal state.

l The ______________method makes a new object with the same state as an existing

l The ______________method makes a new object with the same state as an existing object.

l The ______clone_______method makes a new object with the same state as an existing

l The ______clone_______method makes a new object with the same state as an existing object.

Excercise l l l Vehicle Car Truck Sedan Coupe Pickup. Truck SUV l Minivan

Excercise l l l Vehicle Car Truck Sedan Coupe Pickup. Truck SUV l Minivan l Bicycle l Motorcycle l

Excercise l l l l Student Professor TA Employee Secretary Department. Chair Janitor l

Excercise l l l l Student Professor TA Employee Secretary Department. Chair Janitor l l l Seminar. Speaker Person Course Seminar Lecture

CHAPTER 12 – MORE GUI l Applications

CHAPTER 12 – MORE GUI l Applications

FRAME BASED APPLICATION public static void main(String[] args) { JFrame app. Frame = new

FRAME BASED APPLICATION public static void main(String[] args) { JFrame app. Frame = new JFrame(); app. Frame. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE); app. Frame. set. Content. Pane(rect. Panel); app. Frame. pack(); app. Frame. show(); }

Layout Management l Container arranges its components l By default, JPanel places components from

Layout Management l Container arranges its components l By default, JPanel places components from left to right, then starts a new row if needed l Panel layout carried out by Flow. Layout layout manager l Can set other layout managers panel. set. Layout(new Border. Layout());

Border Layout l Border layout places components into positions (center, north, west, south, east)

Border Layout l Border layout places components into positions (center, north, west, south, east) panel. add(text. Field, Border. Layout. SOUTH );

Border Layout l Content pane of frame has border layout by default frame. get.

Border Layout l Content pane of frame has border layout by default frame. get. Content. Pane(). add(text. Field, Border. Layout. SOUTH); l Border layout grows components to fit area l To avoid growth, place component into panel (with flow layout), then add panel to content pane

Grid Layout l l l Lays out components in rows and columns All components

Grid Layout l l l Lays out components in rows and columns All components have the same size Add components left to right (top row first, then second row, etc. ) l panel. set. Layout(new Grid. Layout(4, 3)); panel. add(button 7); panel. add(button 8); panel. add(button 9); panel. add(button 6);

Grid Layout

Grid Layout

A Combo Box, Check Boxes, and Radio Buttons

A Combo Box, Check Boxes, and Radio Buttons

Menu Items Pull-Down Menus l Add menu items to the menu JMenu. Item file.

Menu Items Pull-Down Menus l Add menu items to the menu JMenu. Item file. New = new JMenu. Item("New"); l Add action listener to the menu item Action. Listener l =. . . ; file. New. add. Action. Listener(l);

Putting it all together

Putting it all together

Menus

Menus