CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see email, see me as soon as possible with questions/concerns Prog2: do not add any public methods.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Topics Inheritance introduction
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
© 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.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
CSC 113 Tutorial QUIZ I.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Advanced Programming Behnam Hatami Fall 2017.
Class Inheritance Concept of Inheritance Java keywords
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

CS 2430 Day 9

Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods to any classes

Agenda for today Finish Date methods Introduction to inheritance Inheritance details

Go to THE THING:

Method lessThan()

Using lessThan() public boolean lessThan(Date rhs) {... }... public static void main(String args[]) { Date fall2012Start = new Date(2012, 9, 4); Date fall2012End = new Date(2012, 12, 21); // How to count the number of days in the semester? Date date = new Date(fall2012Start); int count; for (count = 1; date.lessThan(fall2012End); count++) date = date.tomorrow(); System.out.println("Days in 2012 fall semester: " + count); }

Implementing lessThan() public boolean lessThan(Date rhs) { if (year < rhs.year) return true; if (year > rhs.year) return false; if (month < rhs.month) return true; if (month > rhs.month) return false; return day < rhs.day; }

Method addDays()

Using addDays() public void addDays(int numDays) {... } // modifies the object public static void main(String args[]) { Date prog2Grace = new Date(2012, 10, 1); // Some students want a two-day extension: prog2Grace.addDays(2); }

Implementing addDays() public void addDays(int numDays) { // How to do the add? // Wrong: this.day += numDays Date date = new Date(this); // make a copy of current Date for (int count = 0; count < numDays; count++) date = date.tomorrow(); this.year = d.year; this.month = d.month; this.day = d.day; } Class Date is no longer “immutable”!

Immutable objects Definition: objects whose state (i.e., the object's data) cannot change after construction. For now, we will say that Date, without addDays(), is immutable, although technically, it is not. Example: String s are immutable

Any questions?

Go to THE THING:

Features of OOP Encapsulation Data hiding Abstraction / abstract data type NEW: Inheritance

A new keyword: extends public class A { private int x = 0; public void doIt(int y) { x += y; } public void print() { System.out.println(x); } } public class B extends A { // empty! } public static void main(String args[]) { B b = new B(); // The method is inherited: b.doIt(10); // x is printed out! b.print(); }

Inheritance examle UML: Class B inherits from class A AB Inheritance represents an “is a” relationship public class A {... } public class B extends A {... }

Is-a relationship public class Student extends Person {... } public class Square extends Rectangle {... } public class Civic extends Car {... }

An example project // Assume Class A and B exist and B a sub-class of A public class C { public static void main(String args[]) { A a = new A(); B b = new B(); a.doIt(5); a.print(); b.doIt(10); b.print(); } }

UML for example project Class C uses classes A and B Class B inherits from class A AB C

Any questions?

Why use inheritance? Basic idea: when you want to create a new class and there is already a class that includes some of the code you want Simply extend the existing class! Can reuse the fields and methods in the existing class Less typing!

Some inheritance details

Subclass and superclass A class that is derived from another class is called a subclass (a.k.a., child class) The class from which a class is derived is called a superclass (a.k.a., parent class)

Subclasses and constructors A subclass inherits all members of its parent Constructors are (technically) NOT members, so they are not inherited by subclasses The constructor of the superclass can (and MUST) be invoked from the subclass using super() –Must be invoked on the first line of the constructor!

Any questions?

Go to THE THING: