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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Java Keywords CSE 115 Spring Purpose This set of slides contains a running list of the keywords that we have talked about so far this semester and.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
An Introduction to Inheritance
Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
One class is an extension of another.
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

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

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

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

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