Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 8 Recap CSE 115 Spring 2007. Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.

Similar presentations


Presentation on theme: "Week 8 Recap CSE 115 Spring 2007. Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite."— Presentation transcript:

1 Week 8 Recap CSE 115 Spring 2007

2 Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite object, it can itself refer to composites. Therefore, we have a structure which can grow infinitely large depending on what we add to it. Therefore, we have a structure which can grow infinitely large depending on what we add to it.

3 Inheritance Between Interfaces Between Interfaces Between Classes Between Classes

4 Interface -> Interface Formal name for this relationship is generalization Formal name for this relationship is generalization Informal name “extends” Informal name “extends” Code: Code: public interface InterfaceA extends InterfaceB { …}

5 What does extends do? All public methods declared in InterfaceB become part of InterfaceA and remain public. All public methods declared in InterfaceB become part of InterfaceA and remain public.

6 Important to classes that implement InterfaceA Now those classes must provide implementations for all methods from InterfaceB as well as those methods declared in InterfaceA. Now those classes must provide implementations for all methods from InterfaceB as well as those methods declared in InterfaceA.

7 New type: Abstract Classes Classes that have an abstract method Classes that have an abstract method Abstract method: A method that is undefined Abstract method: A method that is undefined Provides partial implementation (of an interface perhaps) Provides partial implementation (of an interface perhaps)

8 Relationships with Abstract Classes Abstract Classes can implement interfaces. Abstract Classes can implement interfaces. Abstract Classes can extend other Abstract Classes. Abstract Classes can extend other Abstract Classes. Concrete Classes can extend Abstract Classes. Concrete Classes can extend Abstract Classes.

9 Abstract Class implements Interface Can provide some or all of the implementation for the methods declared in the interface. Can provide some or all of the implementation for the methods declared in the interface. At least one method in the abstract class still remains undefined. This can be an interface method or a completely new method. At least one method in the abstract class still remains undefined. This can be an interface method or a completely new method.

10 Abstract Class extends Abstract Class The subclass inherits all public methods of the superclass and they remain public in the subclass. The subclass inherits all public methods of the superclass and they remain public in the subclass. The subclass can implement some or all of the abstract methods from the superclass, however, once again at least one method in the subclass remains abstract. The subclass can implement some or all of the abstract methods from the superclass, however, once again at least one method in the subclass remains abstract.

11 Restriction on extends Only one class name can appear after the keyword extends in a Java file. Only one class name can appear after the keyword extends in a Java file. Therefore, a class can only extend one other class, whether that class is abstract or concrete. Therefore, a class can only extend one other class, whether that class is abstract or concrete.

12 Concrete Class extends Abstract Class Concrete subclass inherits all public methods from abstract class and they remain public in the subclass. Concrete subclass inherits all public methods from abstract class and they remain public in the subclass. Concrete class MUST provide implementation for all abstract methods from the superclass. Concrete class MUST provide implementation for all abstract methods from the superclass.

13 One more use of extends Concrete classes can also extend other concrete classes. Concrete classes can also extend other concrete classes. The subclass once again inherits all public methods of the superclass and they remain public in the subclass. The subclass once again inherits all public methods of the superclass and they remain public in the subclass.

14 Constructors and Inheritance When you create an instance of a class that extends another class, the constructor of the subclass must make a call to the constructor of the superclass. When you create an instance of a class that extends another class, the constructor of the subclass must make a call to the constructor of the superclass. By default, the no argument constructor of the superclass is called. By default, the no argument constructor of the superclass is called.

15 Constructors and Inheritance If no no-argument constructor exists in the superclass, the subclass must explicitly call another constructor from the superclass using the keyword super and passing the appropriate parameters. If no no-argument constructor exists in the superclass, the subclass must explicitly call another constructor from the superclass using the keyword super and passing the appropriate parameters.

16 Method Overriding When we use inheritance, we inherit all the public methods from the superclass. When we use inheritance, we inherit all the public methods from the superclass. What if we don’t like the way a particular method is defined in the superclass and want to change it. What if we don’t like the way a particular method is defined in the superclass and want to change it. We can use method overriding. We can use method overriding.

17 Method Overriding Defining a method in the subclass that has the same method header as one in the superclass. Defining a method in the subclass that has the same method header as one in the superclass. Two types of overriding Two types of overriding –Partial: Calls the superclass’ method and adds on functionality. –Total: Completely ignores superclass functionality and provides totally new definition for the method.


Download ppt "Week 8 Recap CSE 115 Spring 2007. Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite."

Similar presentations


Ads by Google