DOC PREVIEW
UB CSE 115 - Inheritance Interfaces

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 ProgrammingEncapsulationInheritancePolymorphismEncapsulationKeeping 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.InheritanceEvery 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 superclassIn 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 OverridingDefining a method in a subclass that has the same signature as one in the superclass.Can:Call the superclass’ methodAdd on to the superclass’ methodCompletely redefine the method.InterfaceContract (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.InterfacesAre NOT classesBut they are types (just like classes are). So, they can be the type ofVariables (local or instance)Return typesFormal ParametersDefining an InterfaceNo 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 InterfaceA 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 FactsWe 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 RecapThe next set of slides presents a recap of all the topics we have discussed with inheritance.ClassA extends ClassBClassA now inherits (can access and use) all public and protected elements of ClassBWe can expect the behavior from the subclass that the superclass had.ClassA extends ClassBWhen 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 ClassBA 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 ClassBWhen 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.InheritanceLooking 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 overloadingMethod inheritanceMethod


View Full Document

UB CSE 115 - Inheritance Interfaces

Download Inheritance Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?