Inheritance and Encapsulation. Amit Gupta

Similar documents
Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Polymorphism

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS-202 Introduction to Object Oriented Programming

Inheritance, and Polymorphism.

PROGRAMMING LANGUAGE 2

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Exception Handling Generics. Amit Gupta

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Comp 249 Programming Methodology

CS1150 Principles of Computer Science Objects and Classes

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Mobile Application Programming. Swift Classes

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Inheritance and Polymorphism

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Advanced Placement Computer Science. Inheritance and Polymorphism

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

ECE 122. Engineering Problem Solving with Java

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Programming in C# Inheritance and Polymorphism

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Chapter 5 Object-Oriented Programming

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

What is Inheritance?

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Chapter 14 Abstract Classes and Interfaces

Inheritance. COMP Week 12

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Programming II (CS300)

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Lecture 18 CSE11 Fall 2013 Inheritance

Programming II (CS300)

2.4 Structuring programs

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Object Orientated Analysis and Design. Benjamin Kenwright

Inheritance -- Introduction

Object Orientated Programming Details COMP360

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Microsoft Visual Basic 2005: Reloaded

INHERITANCE. Spring 2019

Data Structures (list, dictionary, tuples, sets, strings)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Instance Members and Static Members

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Mobile Application Programming. Swift Classes

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Inheritance and Polymorphism. CS180 Fall 2007

CMSC 132: Object-Oriented Programming II

C++ Important Questions with Answers

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance, Polymorphism, and Interfaces

Building custom components IAT351

CS111: PROGRAMMING LANGUAGE II

8. Polymorphism and Inheritance

Chapter 6: Inheritance

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Polymorphism

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

QUESTIONS FOR AVERAGE BLOOMERS

Lecture 10 OOP and VB.Net

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Lecture 5: Inheritance

Reusing Classes. Hendrik Speleers

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

JAVA MOCK TEST JAVA MOCK TEST II

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Polymorphism and Inheritance

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance. Transitivity

CS Programming I: Inheritance

C++ Inheritance and Encapsulation

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Logistics. Final Exam on Friday at 3pm in CHEM 102

Arrays Classes & Methods, Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Self-review Questions

A A B U n i v e r s i t y

Programming overview

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

INHERITANCE AND EXTENDING CLASSES

CS 251 Intermediate Programming Inheritance

9/10/2018 Programming Data Structures Inheritance

Transcription:

Inheritance and Encapsulation Amit Gupta

Project 1 How did it go? What did you like about it? What did you not like? What can we do to help?

Suggestions Ask questions if you don t understand a concept Start Early!* (early!= (duedate 2)) Come to Office Hours Come to Consulting Hours Ask Questions!!! *This is the secret to success in this course!

Inheritance What do you know already?

Basic Inheritance Process of creating inherited class out of an existing class is called inheriting a class, deriving a class or subclassing. Existing class is called a parent class, base class or super class. New class is called child class, derived class or subclass. Child class inherits all of members and methods of parent class.

Why Inheritance? Adding Functionality Child class adds additional functionality to the parent class. No interference with basic functionality of the underlying class. For example, a simple Fish class might be able to do things like swim and feed. Another child class adds the ability to eat other Fish objects.

Why Inheritance? Code Reuse Minimizes any copying and pasting of code and promotes code reuse. Old mistakes are propagated with copying and pasting. Total amount of code increases even without the issue of errors introduced by copying and pasting.

Inheritance Skeleton public class derived extends base { } //Instance Vars for derived class //Omit I.V. s from base class //Overridden & New Methods //Omit Identical Methods from base

Inheritance in Java Base Class Fish Derived Classes Shark and Flounder

Object Class Do you know about Object class?

Object Class Every class in Java extends Object class. Every class in Java contains these methods. Java provides basic implementation of these methods in Object class but they need to be overridden so as to work with your object classes.

The super keyword Sometimes, we may wish to call methods defined in base class from the derived class. This is done with the keyword super. Example: super.baseclassmethod()

Constructors and Inheritance All the constructors in an object s inheritance tree must run when you make a new object. Child class object can be thought of containing parent class object inside of it. The call to super() must be the first statement in each constructor.

Overloaded Constructors Use this() to call a constructor from another overloaded constructor in the same class. Call to this() must be the first statement in a constructor. A constructor can have a call to super() OR this(), but never both.

Overriding methods and hiding members Methods in base class can be overridden in child classes to provide new functionality. Just as methods are overridden, member variables are hidden.

Representing different Polygons Using Inheritance Polygon class is the base class and represents a general polygon. Triangle and Rectangle are more specific polygons and hence they extend the general Polygon class by extending it. Inheritance hierarchy represents IS A relationship.

Polygon Class Represents general Polygon class. Number of things that can be done with general polygon are limited.

Triangle Class gettype() method allows us to perform specific to triangles. Super constructor must be the first line in the Triangle constructor.

Rectangle Class getarea() method is specific to a rectangle

Square Class Square is a more specific type of Rectangle. Square IS A rectangle.

Summarizing Inheritance Duplicate Code can be avoided. Common Code can be put in one place and if it needs to be changed, it has to be changed in only one place. Inheritance defines a common protocol for a set of classes related through inheritance.

Encapsulation and Data Hiding What do you already know? What is the purpose of encapsulation? What keywords pertaining to encapsulation are you familiar with?

Encapsulation and Data Hiding Encapsulation is the ability of an object to be a container for related properties i.e. data variables and methods. Data Hiding is the ability to shield variables from external access and is a consequence of encapsulation principle. Java provides different access levels for data hiding.

Package Access You have likely seen public, protected, and private access Package Access allows the class itself, as well as classes in the same package access Is this more or less strict than protected?

Encapsulation Summary Public Protected Package Private Class Itself Same Package Derived Class Other Classes Yes Yes Yes Yes Yes Yes Yes No Yes Yes No No Yes No No No Slide Author: Dr. Keith Frikken, Miami University

Questions

Quiz Time