Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
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 Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
1 COMP 110 Inheritance Tabitha Peck M.S. April 14, 2008 MWF 3-3:50 pm Philips 367.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
COMP Inheritance Basics Yi Hong June 09, 2015.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Polymorphism.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Chapter 7- Inheritance.
Inheritance and Polymorphism
Java Inheritance.
Advanced Programming in Java
Road Map Inheritance Class hierarchy Overriding methods Constructors
COMP 110 More about inheritance
ATS Application Programming: Java Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CSC 143 Inheritance.
Inheritance Chapter 7 Chapter 7.
CSC 113 Tutorial QUIZ I.
Class Inheritance (Cont.)
Inheritance Basics Programming with Inheritance
COMP 110 Information hiding and encapsulation
Inheritance I Class Reuse with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
CS1316: Representing Structure and Behavior
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Chapter 10: Method Overriding and method Overloading
Object-Oriented Programming
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011 COMP 110 Inheritance Catie Welsh April 11, 2011

Michele Weigle - COMP 14 - Spr 04 Announcements Program 4 early code check due Friday, April 15th in class Bring a print out of your code with you – whatever you have so far Also include a copy of any pseudocode you’ve written out To earn the 25 points, I’d like to see a significant amount of code written that does 1 of the following Game board code written – numbers randomly assigned, 2 of each # Mouse clicking code written so that your shapes are centered in the correct square of the grid

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Go over Array Review Worksheet Solutions are posted online Inheritance

Inheritance We have discussed before how classes of objects can have relationships Person Transportation Student Employee Car Airplane Animal Undergrad Grad Faculty Staff Horse Elephant Camel Masters Doctoral Nondegree

Inheritance Define a general class Later, define specialized classes based on the general class These specialized classes inherit properties from the general class

Inheritance What are some properties of a Person? How about a Student? Name, height, weight, age How about a Student? ID, major Does a Student have a name, height, weight, and age? Student inherits these properties from Person

The is-a relationship This inheritance relationship is known as an is-a relationship A Doctoral student is a Grad student A Grad student is a Student A Student is a Person Is a Person a Student? Not necessarily!

Base class Our general class is called a base class Examples: Also called a parent class or a superclass Examples: Person, Transportation

Derived class A specialized class that inherits properties from a base class is called a derived class Also called a child class or a subclass Examples: Student is-a Person Employee is-a Person Car is-a form of Transportation Animal is-a form of Transportation Person Student Employee Undergrad Grad Faculty Staff Masters Doctoral Nondegree

Child (derived) classes can be parent (base) classes Student is a child class of Person Student is also the parent class of Undergrad and Grad Person Student Employee Undergrad Grad Faculty Staff Masters Doctoral Nondegree

Why is inheritance useful? Enables you to define shared properties and actions once Derived classes can perform the same actions as base classes without having to redefine the actions If desired, the actions can be redefined – more on this later

How does this work in Java? public class Person { private String name; public Person() name = “No name yet”; } public void setName(String newName) name = newName; public String getName() return name; Person - name + setName(String newName) : void + getName() : String

How does this work in Java? public class Student extends Person { private int id; public Student() super(); id = 0; } public Student(String stdName, int idNumber) setName(stdName); setID(idNumber); public void setID(int idNumber) id = idNumber; public int getID() return id; Person - name + setName(String newName) : void + getName() : String Student - id + setID(int idNumber) : void + getID() : int

The extends keyword public class Derived_Class_Name extends Base_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods } public class Student extends Person // stuff goes here A derived (child) class inherits the public instance variables and public methods of its base (parent) class

We have seen extends before public class SmileyMain extends JApplet The SmileyMain class inherits the public instance variables and public methods of the JApplet class

private vs. public private instance variables and private methods in the base class are NOT inherited by derived classes This would not work: public Student(String stdName, int idNumber) { name = stdName; // ERROR! name is private to Person setID(idNumber); }

private vs. public private instance variables of the base class CAN be accessed by derived classes using the base class’ public methods This works: public Student(String stdName, int idNumber) { setName(stdName); // OK! setName is a public method in Person setID(idNumber); }

The super keyword Used to call a constructor of the base class (remember, a base class is also known as a superclass) More details later

Overriding methods What if the class Person had a method called printInfo? public class Person { // a bunch of other stuff // ... public void printInfo() System.out.println(name); }

Overriding methods What if the class Student also had a method called printInfo? public class Student extends Person { // a bunch of other stuff // ... public void printInfo() System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); }

Overriding methods If Student inherits the printInfo() method and defines its own printInfo() method, it would seem that Student has two methods with the same signature We saw before that this is illegal, so what’s the deal?

Overriding methods Java handles this situation as follows: If a derived class defines a method with the same name, number and types of parameters, and return type as a method in the base class, the derived class’ method overrides the base class’ method The method definition in the derived class is the one that is used for objects of the derived class

Overriding methods: example Both Person and Student have a printInfo() method Student std = new Student("John Smith", 37183); std.printInfo(); // calls Student’s printInfo method, // not Person’s Output would be: Name: John Smith ID: 37183

Overriding vs. overloading If a derived class defines a method of the same name, same number and types of parameters, and same return type as a base class method, this is overriding You can still have another method of the same name in the same class, as long as its number or types of parameters are different: overloading

Wednesday Discuss Lab 8 More about inheritance