Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Inheritance, part 2: Subclassing COMP 401, Fall 2014 Lecture 8 9/11/2014.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
The Java Programming Language
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Programming in Java CSCI-2220 Object Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Polymorphism, Virtual Methods and Interfaces Version 1.1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
2.7 Inheritance Types of inheritance
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.
Conditional Statements
Object Oriented Programming
Final and Abstract Classes
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015

Outline for Today 1.Enums 2.Class inheritance 3.“Factoring out” code through inheritance 4.Typing using IS-A relationships 5.Access modifiers

First, let’s talk about Enums Motivating example: enums.v1

First, let’s talk about Enums When a type can only take on a small, finite set of values we just want to call by name... Java provides a special kind of class called an enum. Basically just a bunch of int -type fields – but the actual values are not accessible to the programmer. Can be compared using the == operator.

Enum Syntax and Type Rules enums.v2

Nested Enums and Switch Statements enums.v3

Class Inheritance The main reason we have inheritance is so we don’t have to repeat code we’ve already written. This is analogous to defining methods for common groups of statements.

class A public void baz() {...} class B public void doh() {...} int x; int y; int x; int z; public int foo() {...} public void bar() {...} public int foo() {...} public void bar() {...} Say we have these two class definitions...

class A public int foo() {...} public void bar() {...} public void baz() {...} class B public int foo() {...} public void bar() {...} public void doh() {...} int x; int y; int x; int z; And these methods are defined the same way...

class A extends S public void baz() {...} class B extends S class S int x; public int foo() {...} public void bar() {...} public void doh() {...} int y; int z; Use class inheritance to “factor out!”

class A public int foo() {...} public void bar() {...} public void baz() {...} class B public int foo() {...} public void bar() {...} public void doh() {...} int x; int y; int x; int z; Now what if these are declared with the same header but not defined the same way??

class A implements I public int foo() {...} public void bar() {...} public void baz() {...} class B implements I interface I int foo(); void bar(); public int foo() {...} public void bar() {...} public void baz() {...} int x; int y; int x; int z; Use an interface! (Sorry, the instance variable has to stay)

class Athlete public void run() {...} class Refrigerator interface Runner void run(); public void run() {...} Although this isn’t always applicable... class Nose public void run() {...}

class Athlete public void run() {...} class Refrigerator interface Runner void run(); public void run() {...} Although this isn’t always applicable... class Nose public void run() {...}

Typing with IS-A Relationships If we have a variable of a certain type, we can put any object that IS that type in that variable. DNAStrand head; // Can be StringDNAStrand, JoinedDNAStrand, etc. Works with both classes and interfaces.

BUT We may call only those methods that are defined in that type!!! class SuperDNAStrand extends StringDNAStrand { public void amazingMethod() {...} } DNAStrand head = new SuperDNAStrand(); head.amazingMethod();

BUT We may call only those methods that are defined in that type!!! class SuperDNAStrand extends StringDNAStrand { public void amazingMethod() {...} } DNAStrand head = new SuperDNAStrand(); head.amazingMethod(); // compilation error

BUT We may call only those methods that are defined in that type!!! class SuperDNAStrand extends StringDNAStrand { public void amazingMethod() {...} } R EMEMBER : IS-A relationships are transitive!!! SuperDNAStrand is automatically “implementing” the DNAStrand interface

Null Any reference-typed variable can take a null value: ◦“empty” or pointing to no object. “Referencing” a null-valued variable (e.g., calling a method on it) will result in the dreaded NullPointerException class BadDNAStrand implements DNAStrand { DNAStrand head; // defaults to null public char getBaseAt(int idx) { return head.getBaseAt(idx); // null pointer exception } }

Null Value vs. Null String String s1 = null; String s2 = “”; // s1 is not the same as s2!

The Object Class Every Java class inherits from Object – even if you don’t say so!! Therefore, and Object -type variable can hold any reference type. BUT, you won’t be able to call any methods you have defined. You won’t need the Object class very often, but sometimes you will see it come up as a workaround for certain type-checking issues.

Access Modifiers The public and private keywords are called access modifiers. They determine which other classes may access your fields or methods – critical for encapsulation. There are two other levels of access: protected and default access (no keyword).

Access Modifiers KeywordSame classSame packageSubclassesErrrbody public Yes protected Yes NO (none / default)Yes NO private YesNO

Access Modifiers If you’re not sure what to use... ◦ public for methods ◦ private for fields Also, methods declared in an interface are always public

Four Pillars of Object Orientation I.Encapsulation access modifiers II.Modularity classes III.Inheritance IV.Polymorphism interfaces overriding overloading