Lecture 8 D&D Chapter 9 Inheritance Date.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Written by: Dr. JJ Shepherd
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 12: Adding Functionality to Your Classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Topics Inheritance introduction
Inheritance ndex.html ndex.htmland “Java.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Data Structures and Algorithms in JAVA Chapter 2.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Written by: Dr. JJ Shepherd
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Lecture 14 Throwing Custom Exceptions
Objects as a programming concept
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
MIT AITI 2003 Lecture14 Exceptions
Interfaces.
Inheritance and Polymorphism
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Week 8 Lecture -3 Inheritance and Polymorphism
Showing Relationships in UML
CSC 143 Inheritance.
Object Oriented Analysis and Design
CSC 113 Tutorial QUIZ I.
More inheritance, Abstract Classes and Interfaces
CS1316: Representing Structure and Behavior
Domain Class Diagram Chapter 4 Part 2 pp
Extending Classes.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
CS1316: Representing Structure and Behavior
Fundamental Error Handling
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Inheritance.
Chapter 13 Exception Handling: A Deeper Look
Overview of C++ Polymorphism
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Lecture 8 D&D Chapter 9 Inheritance Date

Goals By the end of this lesson, you should Understand the notation used in class diagrams Know how to implement and use super and sub classes Understand how to handle built-in exceptions with try-catch

Tennis Tournament Class diagrams Superclass Subclasses Try-catch Summary

Class Diagram Basics Class diagrams Superclass Subclasses Try-catch Summary http://creately.com/blog/diagrams/class-diagram-relationships/

Person - Superclass Class diagrams Superclass Subclasses Try-catch // Person.java public class Person { protected String name; // constructors public Person (){} public Person (String name){ this.name = name; } // gets and sets public void setName(String name){ public String getName(){ return name; //general methods … The Person superclass looks like any standard class except the variable name has been declared using the keyword protected rather than private. This means its private to this class and its subclasses. Class diagrams Superclass Subclasses Try-catch Summary

Player - SubClass Class diagrams Superclass Subclasses Try-catch //Player.java public class Player extends Person { private int seeding; // constructors public Player(String name) { super(name); } public Player() { super(); public Player(String name, int seeding){ this.seeding = seeding; // this class's gets and sets public void setSeeding(int seeding){ public int getSeeding(){ return seeding; //methods public int ChanceOfWin(){ return (int) (Math.random() *100)+1; Class diagrams Superclass Subclasses Try-catch Summary Player extends the Person class The constructors must first call the superclass constructor (to ensure we have the Person-specific part of the object constructed). This constructor is referenced by super() Player has the superclass fields and methods and its own

Official - SubClass Official also extends Person Class diagrams public class Official extends Person { enum Roles {UMPIRE,LINEJUDGE,BALLKID,UNDEFINED}; private Roles role; // constructors public Official(String name) { super(name); } public Official() { super(); public Official(String name, String role){ super( name); setRole(role); // this class's gets and sets public void setRole(String role){ try { this.role = Roles.valueOf(role.toUpperCase()); catch (IllegalArgumentException e){ this.role = Roles.UNDEFINED; public String getRole(){ return role.toString().substring(0,1).toUpperCase() + role.toString().substring(1).toLowerCase(); Class diagrams Superclass Subclasses Try-catch Summary Official also extends Person

Official - SubClass Class diagrams Superclass Subclasses Try-catch public class Official extends Person { enum Roles {UMPIRE,LINEJUDGE,BALLKID,UNDEFINED}; private Roles role; // constructors … // this class's gets and sets public void setRole(String role){ try { this.role = Roles.valueOf(role.toUpperCase()); } catch (IllegalArgumentException e) { this.role = Roles.UNDEFINED; Class diagrams Superclass Subclasses Try-catch Summary Note the use of an enum for the roles. We also use exception handling here in the form of a try-catch block. This is because the line in the try block will throw an exception (error) if the String role does not contain a valid value. The catch block allows us to take corrective action rather than letting the program crash.

Try-catch Class diagrams Superclass Subclasses Try-catch Summary public void setRole(String role){ try { this.role = Roles.valueOf(role.toUpperCase()); } catch (IllegalArgumentException e) { this.role = Roles.UNDEFINED; } } Many things can throw an exception: from a USB drive being removed in the middle of a save, to a divide-by-zero. Trying to assign an invalid enum causes an exception. Putting a try {} around code that may cause an exception will mean you can catch the exception and deal with it so that the program does not crash. (Obviously in a real program, you would have to get a valid role from the user.) More about Exceptions later!

Inheritance Test Class diagrams Superclass Subclasses Try-catch public class InheritanceTest { public static void main(String[] args) { Person superWoman = new Person(); superWoman.name = "Test Super Name"; System.out.println("name: " + superWoman.getName()); Player player1 = new Player(); player1.name = "Andy Murray"; player1.setSeeding(1); System.out.printf("name: %s, seeding: %d chance of win %d %%\n", player1.getName(), player1.getSeeding(), player1.ChanceOfWin()); Player player2 = new Player("Angelique Kerber",1); System.out.printf("name: %s, seeding: %d\n", player2.getName(), player2.getSeeding()); Official official1 = new Official("Kader Nouni","umpire"); System.out.printf("name: %s, role: %s\n", official1.getName(), official1.getRole()); Official official2 = new Official("Jilly BallGirl", "BallGirl"); System.out.printf("name: %s, role: %s\n", official2.getName(), official2.getRole()); } Class diagrams Superclass Subclasses Try-catch Summary Putting main() into a test class that runs a number of tests is a good way to try out these techniques and check they are working correctly

Debug Class diagrams Superclass Subclasses Try-catch Summary

What do we know Class diagrams Superclass Subclasses Try-catch Summary Inheritance is when a class is used as the parent of another class A super class is one which is used as a parent. It can be exactly the same as any other class The sub-class extends the super class Add the keyword extends and the superclass name after the subclass class name. E.g.: public class Tomato extends Vegetable{ ..} Subclass constructors must call the super class constructor as the first line of code. The subclass has all the attributes, methods, types of the super class plus any new ones it declares Sub-classes can also be super classes. There is no practical limit to the depth of the inheritance hierarchy. A class can only extend one other class. See next lecture for how to achieve polymorphism (which lets a class inherit from multiple sources).

Resources Class diagrams Superclass Subclasses Try-catch Summary Composite classes D&D chapter 8 https://docs.oracle.com/javase/tutorial/java/javaOO/ enums D&D sections 5.8 and 8.9 https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Next Lecture D&D Chapters 10 Objects (4) Polymorphism