Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Lecture 5: Interfaces.
Abstract Class and Interface
Object Oriented Programming
Java Review Interface, Casting, Generics, Iterator.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Abstract Classes and Interfaces
Inheritance using Java
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Java Implementation: Part 3 Software Construction Lecture 8.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Object Oriented Programming
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Inheritance and Polymorphism
Interface.
Lecture 8: Polymorphism & Class Design
Java Programming Language
Interfaces.
Advanced Java Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values on both sides of equals sign  Not limited to what is possible in Java

Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values on both sides of equals sign  Not limited to what is possible in Java = 3 2

Announcement

Inheritance Issues  Multiple inheritance causes many problems  What if there are multiple superclasses declare field?  Which constructor will super() chain to?  Since classes extend only 1 class, Java avoids problem  Often mix multiple ideas within class, however

Inheritance Issues  Multiple inheritance causes many problems  What if there are multiple superclasses declare field?  Which constructor will super() chain to?  Since classes extend only 1 class, Java avoids problem  Often mix multiple ideas within class, however public class Mammal { … } public class Bird { … } public class Platypus extends

Inheritance Issues  Multiple inheritance causes many problems  What if there are multiple superclasses declare field?  Which constructor will super() chain to?  Since classes extend only 1 class, Java avoids problem  Often mix multiple ideas within class, however public class Mammal { … } public class Bird { … } public class Platypus extends

abstract  Can declare methods as abstract  Methods cannot be defined; promise functionality  Methods can have parameters, return type, etc.  Bodies are illegal; declaration only lists signature public abstract void foo(int i); public abstract Beer bar(String o);

abstract class  If we have a class containing abstract methods  Class considered abstract no matter how declared  abstract  abstract classes similar to regular classes…  static & non- static fields allowed in class  Class will always extend exactly 1 other class  Mix of abstract & normal methods can be included  May use default one or may define a constructor  Can be extended by other classes

abstract class

Interfaces in Real World

Interfaces  Can only declare important constant fields  public static final must be used for fields  Interface declares public abstract methods  Methods callable anywhere by any class  But method’s body cannot be defined in interface

Adding to an Interface  Interfaces have sub-interfaces  Sub-interface extends super-interface  Super-interface’s methods inherited by sub-interface  Methods from super-super-interface also inherited  Sub-interface can extend multiple interfaces  Methods not defined so does not cause problems

Declaring Interfaces public interface Drawable { public void setColor(Color c); public Color getColor(); public void draw(Graphics g); } public interface TranDraw extends Drawable{ public void setTransparency(int tLevel); } public interface MoveDraw extends Drawable{ public void setPosition(int x, int y); }

Interfaces implement  Classes implement interfaces  Implementing classes define interface’s methods  Any number of interfaces may be implemented  Multiple inheritance issues ignored -- methods empty  Unrelated to superclass chosen or used by a class  Classes must implement all interface’s methods  Includes methods inherited from super-interface

Implementing Interfaces public class Square implements Drawable { private Color c; private int x, y, side; public Square(Color col, int len) { c = col; side = len; } public void setColor(Color col){ c=col; } public Color getColor() { return c; } public void draw(Graphics g) { g.drawRect(x, y, side, side, c); } }

Implementing Interfaces public class Oval implements Drawable { private Color c; private int x, y, majRad, minRad; public Oval(Color co, int maj, int min){ c = co; majRad = maj; minRad = min; } public void setColor(Color col){ c=col; } public Color getColor() { return c; } public void draw(Graphics g) { g.drawOval(x, y, majRad, minRad, c); } }

Using Interfaces

Interface vs. Abstract Class  Both concepts serve similar purposes  Cannot instantiate either of these types  But can be used for variable, field, & parameter types  Used in other classes to identify important features  But very important differences define when each used

Interface vs. Abstract Class  Can extend classes & implement interfaces instances  Both used to mark instances have methods defined  Fields, params, & locals can use either as their type  Use abstract class when…  …classes should use common method or private field  …place in object hierarchy as subclass of existing class  Otherwise use interface for abstract methods  More flexible: class can implement many interfaces  Can mark classes; do not have to declare methods

Typecasting int i = 13; Square s = ((Square)i);  Only exist to “assist” compiler with code  Changes variable’s type so compilation continues  Not in *.class file – does not affect instance at all  Only when KNOW instance & variables types differ  Errors at runtime instead of during compilation  Illegal code will compile, but still illegal

Typecasting int i = 13; Square s = ((Square)i);  Only exist to “assist” compiler with code  Changes variable’s type so compilation continues  Not in *.class file – does not affect instance at all  Only when KNOW instance & variables types differ  Errors at runtime instead of during compilation  Illegal code will compile, but still illegal

Narrowing Conversions  Java cannot compile narrowing conversions  Assigns superclass/interface to lower variable  Compiler will not allow it, but could be legal  Typecasting required for these assignments Object obj = new String(“bye”);

Narrowing Conversions  Java cannot compile narrowing conversions  Assigns superclass/interface to lower variable  Compiler will not allow it, but could be legal  Typecasting required for these assignments Object obj = new String(“bye”); String sad = obj; //  Does not work

Narrowing Conversions  Java cannot compile narrowing conversions  Assigns superclass/interface to lower variable  Compiler will not allow it, but could be legal  Typecasting required for these assignments Object obj = new String(“bye”); String sad = obj; //  Does not work String glad = (String)obj; // works!

Your Turn  Get into your groups and complete activity

For Next Lecture  Read GT2.5 for Mon. and be ready to discuss  What is a generic type?  Why are they going to save our wrists this year?  How can they be used correctly?  As usual, there is weekly assignment on Angel Tuesday  Due by 5PM Tuesday via Assignment Submitter  Each problem graded using provided JUnit tests