Encapsulation, Inheritance & InterfacesThree Pillars of Object-Oriented ProgrammingEncapsulationInheritance (from before exam)InheritanceOne superclassMethod OverridingInterfaceInterfacesDefining an InterfaceJava Code: InterfacesImplementing an InterfaceJava Code: Implementing an InterfaceInterface FactsInheritance RecapClassA extends ClassBSlide 17Slide 18Slide 19Slide 20What’s the difference?Encapsulation, Inheritance & InterfacesCSE 115 Spring 2006February 27, March 1 & 3, 2006Three Pillars of Object-Oriented ProgrammingEncapsulationInheritancePolymorphismEncapsulationKeeping data and operations that work on that data in one computational unit“public face” versus “private implementation”Inheritance (from before exam)Ability to extend another class and inherit all its public capabilities.InheritanceEvery class in Java uses inheritance.Either specify a specific superclass by using the keyword extends when defining the class.Use default superclass, which is java.lang.ObjectOne superclassIn Java, a class can extend one class (ie – only one class name can appear after the keyword extends in a class definition)However, if the class that is extended has a superclass, the new subclass inherits from both its direct superclass as well as that class’ superclass.Method OverridingDefining a method in a subclass that has the same signature as one in the superclass.Can:Call the superclass’ methodAdd on to the superclass’ methodCompletely redefine the method.InterfaceContract (of methods)If a class chooses to enter into the agreement with the interface, the class promises to have methods with the same signatures as those in the interface.InterfacesAre NOT classesBut they are types (just like classes are). So, they can be the type ofVariables (local or instance)Return typesFormal ParametersDefining an InterfaceNo instance variables allowed.Everything is public.No implementation of methods, just method signatures followed by ;These types of methods are called abstract methods because they have no method bodies. The keyword abstract might be seen in the method signatures of methods in an interfaceJava Code: Interfacespublic interface InterfaceName {public abstract void method1();abstract public Object method2();void method3(Object obj);//Note that all three method signatures are valid for an //interface and which you use is a matter of style.}Implementing an InterfaceA class realizes an interface using the keyword implements.The formal name for this relationship is realization and the informal name is “implements” or “implements an interface”Inside the code, the class must create methods with the same signatures as those in the interface and give each method a method body.Java Code: Implementing an Interfacepublic class name implements InterfaceName {public void method1() { }public Object method2() { return new Object(); }public void method3(Object obj) { }//Note that each method must have a method body, but //that method body could be empty. }Interface FactsWe can implement more than one interface.We can have a superclass and implement one or more interfaces.An interface can extend another interface or multiple other interfaces.You can NEVER create an instance of an interface.Inheritance RecapThe next set of slides presents a recap of all the topics we have discussed with inheritance.ClassA extends ClassBClassA now inherits (can access and use) all public and protected elements of ClassBWe can expect the behavior from the subclass that the superclass had.ClassA extends ClassBWhen the constructor from ClassA is called, it automatically calls the no-argument constructor for ClassB. If ClassB does not have a no-argument constructor, ClassA must explicitly call one of the other constructors from ClassB using the keyword super.ClassA extends ClassBA subclass is the specialization of the superclass, so we can add instance variables, add methods, change parameters on methods, or change the way methods behave.ClassA extends ClassBWhen we have a method that has the same signature in a subclass as one in the superclass, we have method overriding. Method overriding is how we change the behaviors of methods in a subclass.InheritanceLooking at inheritance in reverse – if we have a lot of commonality amongst some classes, we can create one superclass to generalize amongst the subclasses. Ex) Got a lot of classes that do the same thing? Create a common superclass!What’s the difference?Method overloadingMethod inheritanceMethod
View Full Document