Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
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.
Polymorphism Method overriding Method overloading Dynamic binding 1.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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.
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.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships 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.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Lecture 26: Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Last Time Images as 2D arrays color representation.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Chapter 15 Abstract Classes and Interfaces
Lecture 12 Inheritance.
Object-oriented Programming in Java
CMPUT 301: Lecture 06 Interfaces, Abstract classes and Inheritance
12 Data abstraction Packages and encapsulation
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Abstract classes and interfaces
null, true, and false are also reserved.
Implementing Non-Static Features
Announcements & Review
Chapter 11 Inheritance and Polymorphism
More About Inheritance & Interfaces
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson –Chapter 9 Regis & Stepp Additional Reading: va/concepts/inheritance.html Last Time Review Inheritance Dynamic dispatch Today Dynamic dispatch Abstract Classes

Lecture 28: Abstract Classes & Inheritance Inheritance in Java... Object TransformationGraphRectangle ColoredRectangle Object provides these methods: toString() equals() // more on these clone() // later... instanceof() All classes inherit from Object Rectangle inherits from Object ColoredRectangle inherits from Rectangle

Lecture 28: Abstract Classes & Inheritance Super class: Rectangle public class Rectangle { protected int width; // the instance variables protected int height; public Rectangle() { width = 0; height = 0; } public Rectangle(int w, int h) { width = w; height = h; } public void draw(...) {}

Lecture 28: Abstract Classes & Inheritance Syntax of Inheritance Example: ColoredRectangle public class ColoredRectangle extends Rectangle { private Color myColor; // additional instance variables public ColoredRectangle() { super(); // optional myColor = Color.white; } /* Is the same as: public ColoredRectangle() { implicitly first calls super, because the signatures match myColor = Color.white; } */ public ColoredRectangle(int w, int h, Color newColor) { super(w,h); // required: why do we need this one? myColor = newColor; }

Lecture 28: Abstract Classes & Inheritance Dynamic Dispatch Declare variables of supertype and they can be the supertype or the subtype Rectangle r = new ColoredRectangle(); Declare variables of a subtype and they can only be of type subtype or lower. Test type with instanceof returns a boolean if (r instanceof Rectangle) {...} if (r instanceof ColoredRectangle) {...} BlueJ

Lecture 28: Abstract Classes & Inheritance Abstract Classes Want to partially declare a class Force an implementation of a method but do not offer a default implementation it –require a draw method, but have not default draw

Lecture 28: Abstract Classes & Inheritance Syntax for Abstract Classes public abstract class Shapes { protected static String name; // e.g., Rectangle, Triangle,... // Subclass must implement draw public abstract void draw(DrawingBox box); public String getName() { // Subclass need not implement getName return name; } public class Rectangle extends Shapes { protected static String name = “Rectangle”;.... public void draw(DrawingBox box) { box.drawRect(...) }

Lecture 28: Abstract Classes & Inheritance BlueJ examples...

Lecture 28: Abstract Classes & Inheritance Interfaces Want to partially specify a behavior that can apply to lots of classes –For example, adding Color Enforces a consistant application of a “feature” across lots of classes

Lecture 28: Abstract Classes & Inheritance Interfaces Interface Colorable { public void setColor(Color c); public Color getColor(); } public class ColorRectangle extends Shapes implements Colorable{ Color myColor; public void setColor(Color c) { myColor = c; } public Color getColor() { return myColor; }