OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Object Oriented Programming with Java
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstract Classes and Interfaces
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Object Oriented Programming
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Chapter 8 Class Inheritance and Interfaces
CIS 110: Introduction to computer programming
Presentation transcript:

OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides for her/his children l children add to a parent’s knowledge §Polymorphism l Java figures out family relationships: when the parent knows best or when the child knows more than the parent

Inheritance in Java usuper class or parent, subclass or child uadds functionality to an existing class  class Child extends Parent uC++ multiple inheritance … NOT! u single parent families only in Java  use of the keyword super  not this object’s constructor/variable/method, but the one from the super class.

The Object Class u “The Ultimate Superclass” - all classes in Java extend the Object class u Example (an array of generic objects): ObjectDemo.java u "is a" test for inheritance/super class u a String is an Object (true) u an Object is a String (false)

How to override a parent’s method in the child class? uuse the same … u method name u signature (parameter list) u return type udifferent code in the child’s method overrides parent's logic ue.g. equals(), toString() uaccess level can be increased (e.g. private to public) ua different signature: overloads the method ua different return type: compiler error uExample: Inherit.java, ShapeTest & docs

Polymorphism uthe same method call on two objects in the same “family” (inheritance branch) can give different results uWhich method is actually called at run time?  method(signature) in this object’s class  else look up the super (parent) class tree urun-time/dynamic binding determines which one ucompile time: the declared type of an object reference Object x = new BigDecimal("123.45"); urun-time: the actual type of the object referred to determines behaviour. It can be the declared class type or one of its sub classes (inheritance). x.toString() calls BigDecimal class method, not Object's but it compiles because Object x has toString().

Polymorphism  a final method: dynamic binding is turned off  sub (child) classes can inherit but not override any final super (parent) method ulike when your parent said, “…and that’s final!”. ua method of limiting inheritance

Casting and Inheritance ucasting up is automatic: Object object; String string; object = string; // because string IS AN object ucasting down is up to the programmer: string = (String) object; u promises compiler that, at run time, object will refer to a String type. ucompile-time error: subclass = superclass;

Abstract Class: An Example public abstract class Aclass { private int m;// instance variable to be inherited // implemented and to be inherited public void set( int t ) { m = t; } // abstract methods declared, not implemented // must be defined by extending class public abstract void show(); public abstract void change(); }

The Subclasses of an Abstract Class public class Bclass extends Aclass { …// inherit Class Aclass’s set method public void show ( ) { … } // implement details public void change ( ) { … } // implement details } public class Cclass extends Aclass { …// inherit Class Aclass’s set method public void show( ) { … }// class C version public void change ( ) { … }// class C version }

What is an abstract class? uan abstraction(i.e. generalization) of subclasses - a class hierarchy is enforced uabstract methods: the developers of the subclasses MUST customize the code for these methods ucannot be used to create an object instance Aclass aClassRef; // object reference is OK aClassRef = new Aclass(); // but cannot construct uin practice: an abstract class has a mix of instance variables, implemented and abstract methods

Interfaces in Java uWhat is an interface? uHow to declare an interface? uHow to implement an interface? uHow to use an interface?

What is an interface? usyntax: a declaration of methods (that are not implemented yet) usoftware development viewpoint - an agreement on how a “specific” object can be used by other objects - a promise to other software developers that all the methods will be implemented by a class ucannot be used to create an object ustatic final variables may be included (e.g. the Adjustable interface)

How is an interface used?  one or more interfaces can be implemented by a class class MyClass implements Comparable  other classes can tell if your class has implemented an interface if (myClass instanceof Comparable){  then other classes know they can call interface methods int compare=myClass.compareTo(otherMyClass)} uonly your class knows how to implement those methods usee API for Comparable interface uevent handling(GUI programming) - Example: the ActionListener interface uan interface can extend one or more interfaces

Inner Classes ua class that is defined inside another class, called the outer or enclosing class. uaccess privilege to members of the outer class: fields, methods, other inner classes ucan access outer class’s private instance members umostly used for event-driven programs i.e. GUI programming uExample: Outer.java & OuterInnerTest.java

Anonymous Inner Classes uanonymous inner class definition uan inner class that does not have a name unotational convenience: event handling code can be put close to where GUI objects are created unot recommended because u code is messy and dense u can only implement behaviour, i.e. override methods u must use default constructor, i.e. cannot have state