Class Inheritance Concept of Inheritance Java keywords

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

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.
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
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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,
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
Topics Inheritance introduction
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
BY:- TOPS Technologies
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Advanced Java Topics Chapter 9
Lecture 12 Inheritance.
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
An Introduction to Inheritance
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
OOP’S Concepts in C#.Net
CSC 205 Java Programming II
One class is an extension of another.
Inheritance Chapter 5.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Interface.
METHOD OVERRIDING in JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
OO Design with Inheritance
Java Programming, Second Edition
Inheritance.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Chapter 9 Carrano Chapter 10 Small Java
Object-Oriented Programming
Inheritance and Polymorphism
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Class Inheritance Concept of Inheritance Java keywords Practice extending a base class of a subclass

Inheritance An important OOP mechanism. Implies a parent child relationship. Enables one class to acquire data attributes and functionality of another class. Specifically, use inheritance to pass along setters, getters and other methods and data members of an existing class.

Example with John John is a Loan Shark He is often threatened with bodily harm. He doesn’t have money to pay for a 24/7 bodyguard. Decides to get a dog for protection.

Dog Options for John

Why did John choose a Doberman Pinscher? Intelligent Vigilant Alert Fearless Aggressive Loyal Obedient

Real-world inheritance Attributes Traits Behaviors … All passed down through a lineage of generations

OOP inheritance OOP provides a mechanism that enables one class to acquire all aspects of another class

Flexibility of Inheritance? Programmers can create a subclass class by simply indicating the ways in which it differs from a super class that has already been developed and tested.

Inherited Traits Acquired or Overridden

Example We expect to see shared traits in all Doberman Pinschers. Individual Doberman Pinschers have different personal characteristics. We dependably count on many inherited attributes and behaviors.

OOP Inheritance We use inheritance to pass along functionality and attributes of an existing class. We can use override to identify different traits and functionality.

Inheritance Relationship Instrument is a super class, a general class. Wind, Percussion and Stringed are sub classes of Instrument. A sub class is-a specific type of super class. Example: Percussion is-a(n) Instrument.

What does a subclass inherit? A sub class automatically inherits the data fields and methods of the super class.

Subclass A subclass can declare additional methods or change the implementation of inherited methods. When you supply a new implementation for an inherited method, you automatically override the super class method.

Polymorphism allows us to create a new implementation for an inherited method. Example: Instrument is the base class. Wind has its own implementation of play(). Percussion has its own implementation of adjust(). Stringed has its own implementation of play() and adjust().

Java Keywords for Inheritance extends Use extends to create a subclass. When a class B extends class A, B automatically inherits all variables and methods defined in class A. super Use super to access methods from the superclass (base class). Used inside a subclass method to call a method defined in the superclass. Only public methods can be called by the super keyword. super is also used by class constructors to invoke constructors of its parent class.

Access Modifiers There are 3 types of access modifiers: private members are accessible only within a class. protected members can be accessed only by the subclasses. public members have the widest scope among all other modifiers. They are accessible everywhere.

Practice using the Quiz App Two kinds of questions: Fill-in-blank (a generic question) Multiple Choice What is the base class (super class)? What is the sub class?  

Question class and subclass

Multiple Choice Example 1 Question: What year was Java born? (a) 2004 (b) 1995 (c) 1991 (d) 1989 Answer: 1991 NOTE: There are four choices.

Multiple Choice Example 2 Question: How many hamburgers per cow? (a) 4500 (b) 25 (c) 4 NOTE: There are three choices.

Quiz App classes Question base class MultipleChoice class extends Question QuizEngine class QuizMain class

public class Question { // Data Members private String question; private String answer; public Question(String q, String a) { question = q; answer = a; } // Setters and Getters public String getAnswer() { return answer; public void setAnswer(String a) { public String getQuestion() { return question; public void setQuestion(String q) { // Extra Functionality public boolean checkAnswer(String response) { return answer.toLowerCase().equals(response.toLowerCase());

public class QuizEngine { private ArrayList<Question> questionList; private int correctAnswers; public QuizEngine() { questionList = new ArrayList<Question>(); correctAnswers = 0; } public void addQuestion(Question q){ questionList.add(q); public void shuffleQuestions(){ Collections.shuffle(questionList); public void start(){ Scanner in = new Scanner(System.in); for (int i = 0; i < questionList.size(); i++){ System.out.println("Question " + (i+1)+ ": " + questionList.get(i).getQuestion()); System.out.print("Answer: "); String response = in.nextLine(); if (questionList.get(i).checkAnswer(response)){ System.out.println("Correct"); correctAnswers++; else{ System.out.println("Incorrect"); public void getResults(){ System.out.println("Score: " + (int)((double)correctAnswers/questionList.size()*100) + "%");

public class QuizMain { public static void main(String[] args) { // TASK 1: INSTANTIATE TWO QUESTIONS, ONE FOR EACH TYPE. // TYPE: GENERIC QUESTION Question q1 = new Question("Who invented Java?", "James Gosling"); // TYPE: MULTIPLE CHOICE QUESTION MultipleChoice q2 = new MultipleChoice("Year was Java born?",""); q2.addChoice("1994", false); q2.addChoice("1993", false); q2.addChoice("1992", false); q2.addChoice("1991", true); // TASK 2: INSTANTIATE A QUIZ ENGINE AND POPULATE IT WITH QUESTIONS QuizEngine quizEngine = new QuizEngine(); quizEngine.addQuestion(q1); quizEngine.addQuestion(q2); //TASK 3: SHUFFLE THE QUESTIONS AND START THE QUIZ quizEngine.shuffleQuestions(); quizEngine.start(); quizEngine.getResults(); }

Further Practice Use inheritance to add a True and False question to the Quiz App. True and False questions are Multiple Choice questions, with a restriction of two choices.