I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES.

Slides:



Advertisements
Similar presentations
Abstract Class and Interface
Advertisements

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.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
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.
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Abstract Classes and Interfaces
Games and Simulations O-O Programming in Java The Walker School
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
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.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Week 4 Object-Oriented Programming (1): Inheritance
Using local variable without initialization is an error.
CS2102: Lecture on Abstract Classes and Inheritance
Object Oriented Programming (OOP) LAB # 8
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Java Inheritance.
Fundaments of Game Design
Inheritance and Polymorphism
Presentation transcript:

I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

I NTERFACES Let you share methods across multiple (otherwise unrelated) clases Specify new types (like “ IResult ” and “ IContestant ” in your homework assignments) Only have method headers (name of method and its return type), not actual method bodies When a class “ implements ” an interface, that’s a promise to use all of the methods from that interface Are not used to share common method bodies – that’s done with abstract classes

A BSTRACT C LASSES Let you share common variables and code (method bodies, not just method names), and to help avoid code duplication. If you have an “Animal” abstract class with a weight and name, and if “Fish” extends that class, Fish then gets a weight and name as well. Classes that “extend” an abstract class will inheret/implement all methods in its superclass. From the example above, if “Animal” has any methods in it, “Fish” gets those methods too. You CANNOT declare an instance of an abstract class If “Animal” is an abstract class, I cannot do: Animal animal = new Animal(5, “animalName”); If “Fish” is NOT an abstract class and is concrete, I can do: Fish fish = new Fish(1, “fishName”); They don’t need to have all methods in an interface that they implement. Abstract classes are exempt from this.

H OW MANY CLASSES CAN YOU EXTEND ? Only one. A class can implement multiple interfaces, but it can only extend one class.

W HAT IS “ SUPER ”? “super” refers to the class that the current class extends From before, if Fish calls to “super,” it’s calling to the abstract class Animal Let’s say fish have a weight and a name (like all animals do), but then they also have a variable called “scalesColor.” The class for fish would look like: public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; }

L ET ’ S EXAMINE THAT MORE CLOSELY. public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; } The text in blue refers to the extended abstract class Animal. This means we will go to the constructor for Animal and give it this weight and this name. The abstract class Animal knows what to do with these two pieces of information. The text in red refers to the variable scalesColor, which is within the Fish class and doesn’t exist in the Animal class. The Animal abstract class has no scalesColor, so we can’t pass it into the call to super. Animal just wouldn’t know what to do with it, and you would get an error. The Fish class is what handles the scalesColor.

Abstract classes let you share some pieces of code while being able to add in other different pieces of code that are unique to a specific class. In the example below, the text in blue is code that the concrete classes acquire from their abstract class, Animal. Text in red is code that is unique to that specific class. abstract class Animal has: int weight String name class Fish has: int weight String name String scalesColor class Lion has: int weight String name String maneColor int roarScarinessLevel class Goat has: int weight String name String bleatDescription public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; } public class Lion extends Animal { String maneColor; int roarScarinessLevel; Lion(weight, String name, String maneColor, int roarScarinessLevel){ super(weight, name); this.maneColor = maneColor; this.roarScarinessLevel = roarScarinessLevel; } public class Goat extends Animal { String bleatDescription; Fish(int weight, String name, String bleatDescription){ super(weight, name); this.bleatDescription = bleatDescription; }

B INARY S EARCH T REES Each node has two children (left and right) Left’s key is smaller than parent’s key Right’s key is larger than parent’s key

E XAMPLE BST Let’s add to this tree and remove from this tree 10 / \ 4 12 / \ 2 7 / \ / \ / 5

E XAMPLE BST: ADD E LT addElt( 9 ) 10 / \ 4 12 / \ 2 7 / \ / \ / 5

E XAMPLE BST: ADD E LT 9 has been added 10 / \ 4 12 / \ 2 7 / \ / \ / \ 5 9

E XAMPLE BST: REM E LT remElt( 6 ) 10 / \ 4 12 / \ 2 7 / \ / \ / \ 5 9

E XAMPLE BST: REM E LT 6 has been removed 10 / \ 4 12 / \ 2 7 / \ / \ \ 9

E XAMPLE BST: REM E LT remElt( 3 ) 10 / \ 4 12 / \ 2 7 / \ / \ \ 9

E XAMPLE BST: REM E LT 3 has been removed 10 / \ 4 12 / \ 2 7 / / \ \ 9

E XAMPLE BST: REM E LT remElt( 4 ) 10 / \ 4 12 / \ 2 7 / / \ \ 9

E XAMPLE BST: REM E LT 4 has been removed… Now what? 10 / \ ? 12 / \ 2 7 / / \ \ 9

E XAMPLE BST: REM E LT We chose the smallest item to the right of where 4 was to be 4’s replacement. 10 / \ 5 12 / \ 2 7 / \ 1 8 \ 9