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

21
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    2

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

Encapsulation, Inheritance &

Interfaces

CSE 115 Spring 2006

February 27, March 1 & 3, 2006

Three Pillars of Object-Oriented ProgrammingEncapsulationInheritancePolymorphism

Encapsulation 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.Object

One 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 Parameters

Defining 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 interface

Java Code: Interfaces

public 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 overriding