Encapsulation Inheritance Interfaces CSE 115 Spring 2006 February

Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006

Three Pillars of Object-Oriented Programming p Encapsulation p Inheritance p Polymorphism

Encapsulation Keeping data and operations that work on that data in one computational unit p “public face” versus “private implementation” p

Inheritance (from before exam) p Ability to extend another class and inherit all its public capabilities.

Inheritance Every class in Java uses inheritance. p Either specify a specific superclass by using the keyword extends when defining the class. p Use default superclass, which is java. lang. Object p

One superclass p In Java, a class can extend one class (ie – only one class name can appear after the keyword extends in a class definition) p 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 p Defining a method in a subclass that has the same signature as one in the superclass. p Can: n n n Call the superclass’ method Add on to the superclass’ method Completely redefine the method.

Interface Contract (of methods) p 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. p

Interfaces Are NOT classes p But they are types (just like classes are). So, they can be the type of p n n n Variables (local or instance) Return types Formal Parameters

Defining an Interface No instance variables allowed. p Everything is public. p No implementation of methods, just method signatures followed by ; p n 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 interface

Java Code: Interfaces public interface Interface. Name { public abstract void method 1(); abstract public Object method 2(); void method 3(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. p The formal name for this relationship is realization and the informal name is “implements” or “implements an interface” p Inside the code, the class must create methods with the same signatures as those in the interface and give each method a method body. p

Java Code: Implementing an Interface public class name implements Interface. Name { public void method 1() { } public Object method 2() { return new Object(); } public void method 3(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. p We can have a superclass and implement one or more interfaces. p An interface can extend another interface or multiple other interfaces. p You can NEVER create an instance of an interface. p

Inheritance Recap p The next set of slides presents a recap of all the topics we have discussed with inheritance.

Class. A extends Class. B p Class. A now inherits (can access and use) all public and protected elements of Class. B p We can expect the behavior from the subclass that the superclass had.

Class. A extends Class. B p When the constructor from Class. A is called, it automatically calls the noargument constructor for Class. B. If Class. B does not have a no-argument constructor, Class. A must explicitly call one of the other constructors from Class. B using the keyword super.

Class. A extends Class. B p 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.

Class. A extends Class. B p 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 p 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. n Ex) Got a lot of classes that do the same thing? p Create a common superclass!

What’s the difference? Method overloading p Method inheritance p Method overriding p
- Slides: 21