Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006."— Presentation transcript:

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

2 Three Pillars of Object-Oriented Programming  Encapsulation  Inheritance  Polymorphism

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

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

5 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

6 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.

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

8 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.

9 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

10 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

11 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. }

12 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.

13 Java Code: Implementing an Interface public 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. }

14 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.

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

16 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.

17 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.

18 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.

19 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.

20 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!

21 What’s the difference?  Method overloading  Method inheritance  Method overriding

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

Similar presentations

Ads by Google