IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Similar documents
JAVA MOCK TEST JAVA MOCK TEST II

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Java Object Oriented Design. CSC207 Fall 2014

Programming II (CS300)

Software Development. Modular Design and Algorithm Analysis

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

ITI Introduction to Computing II

Inheritance -- Introduction

Programming II (CS300)

ITI Introduction to Computing II

QUESTIONS FOR AVERAGE BLOOMERS

Programming Exercise 14: Inheritance and Polymorphism

Chapter 14 Abstract Classes and Interfaces

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 5 Object-Oriented Programming

OVERRIDING. 7/11/2015 Budditha Hettige 82

C++ Important Questions with Answers

Inheritance and Polymorphism

Inheritance, and Polymorphism.

G Programming Languages - Fall 2012

Inheritance. Chapter 7. Chapter 7 1

What is Inheritance?

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

8. Polymorphism and Inheritance

What are the characteristics of Object Oriented programming language?

More on Inheritance. Interfaces & Abstract Classes

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance and Polymorphism

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

ECE 122. Engineering Problem Solving with Java

CS 251 Intermediate Programming Inheritance

INHERITANCE AND EXTENDING CLASSES

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Inheritance. COMP Week 12

Inheritance, Polymorphism, and Interfaces

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

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

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

Polymorphism and Inheritance

Data Abstraction. Hwansoo Han

Understanding Inheritance and Interfaces

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance (IS A Relationship)

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

More On inheritance. What you can do in subclass regarding methods:

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

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

CMSC 132: Object-Oriented Programming II

CS111: PROGRAMMING LANGUAGE II

CS-202 Introduction to Object Oriented Programming

Object Orientated Analysis and Design. Benjamin Kenwright

Arrays Classes & Methods, Inheritance

Chapter 5. Inheritance

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

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

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Inheritance and Polymorphism

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Lecture 18 CSE11 Fall 2013 Inheritance

CSC207 Week 3. Larry Zhang

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

[ L5P1] Object-Oriented Programming: Advanced Concepts

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

Inheritance and Polymorphism

Inheritance and Interfaces

PROGRAMMING LANGUAGE 2

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

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

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CS111: PROGRAMMING LANGUAGE II

CMSC 132: Object-Oriented Programming II

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Chapter 1: Object-Oriented Programming Using C++

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

Basics of Object Oriented Programming. Visit for more.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSC 1214: Object-Oriented Programming

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Java Programming Lecture 7

The major elements of the object-oriented model

CPS 506 Comparative Programming Languages. Programming Language

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

Computer Science 210: Data Structures

Comp 249 Programming Methodology

1.00 Lecture 13. Inheritance

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance and Encapsulation. Amit Gupta

Transcription:

IT101 Inheritance, Encapsulation, Polymorphism and Constructors

OOP Advantages and Concepts What are OOP s claims to fame? Better suited for team development Facilitates utilizing and creating reusable software components Easier program maintenance Main OOP Concepts Inheritance Encapsulation Polymorphism http://www.cs.columbia.edu/~gmw/teaching/1001-s04/lec17.ppt

Inheritance A class can extend another class, inheriting all its data elements and methods while redefining some of them and/or adding its own. Example: class Student extends Person A class can implement an interface, implementing all the specified methods. Example: class Poker extends Game implements Gambling Inheritance implements the is a relationship between objects. In Java, a subclass can extend only one superclass. In Java, a subinterface can extend one superinterface An abstract class can have code for some of its methods; other methods are declared abstract and left with no code. An interface only lists methods but does not have any code. A concrete class may extend an abstract class and/or implement one or several interfaces, supplying the code for all the methods. Inheritance plays a dual role: A subclass reuses the code from the superclass. A subclass (or a class that implements an interface) inherits the data type of the superclass (or the interface) as its own secondary type. In Java, a class can implement several interfaces this is Java s form of multiple inheritance.

Inheritance Inheritance leads to a hierarchy of classes and/or interfaces in an application: An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above. It also inherits the data types of all the classes and interfaces above. Inheritance is also used to extend hierarchies of library classes, reusing the library code and inheriting library data types. Inheritance implements the is a relationship. Not to be confused with embedding (an object has another object as a part), which represents the has a relationship. Sailboat IS A Boat Sailboat HAS A Sail

Encapsulation Encapsulation means that all data members (variables) of a class are declared private. Methods may be private, too. Example: private String ssn; Private methods or data are visible to and accessible only by that object. In other words, private data or methods can not be accessed from outside the object. The class interacts with other classes mainly through the class s constructors and public methods. When data or methods are declared public, other objects of your program can access it.

Polymorphism We often want to refer to an object by its primary, most specific, data type. This is necessary when we call methods specific to this particular type of object Polymorphism (meaning many forms in Greek) ensures that the appropriate method is called for an object of a specific type when the object is disguised as a more generic type http://www.c-sharpcorner.com/uploadfile/433c33/polymorphism-in-java/ Polymorphism is implemented using a technique called late (or dynamic) method binding: which exact method to call is determined at run time.

Constructors A constructor initializes an object when it is created. It has the same name (case sensitive) as its class and is syntactically similar to a method. Constructors have no return type, but they can accept parameters. Use constructors to set initial variable values. Example: class Candle { private String color; Candle (String acolor) { this.color = acolor; A subclass can (and typically should) call the constructor of the superclass using the special super method. The super method should be the first statement of the subclass constructor. Example: class ScentedCandle extends Candle { private float height; ScentedCandle(String c, float h) { super(c); this.height = h;

Homework Prior to developing this program, read the requirements and draw a class diagram of the classes you intend to build (see slide 4 for example of a class diagram). Mick s Wicks makes candles in various sizes. Create a class named Candle that contains data fields for color, height and price. Create get methods for all three fields, e.g.: public String getcolor() { return this.color; Create set methods for color and height, but not for price. Instead, when height is set, determine the price as $2 per inch. Example: public void setheight(float newheight) { float priceperinch = 2; this.height = newheight; this.price = this.height * priceperinch; Create a child class named ScentedCandle that contains an additional data field named scent and methods to get and set it. In the child class, override the parent s setheight() method to set the price of a ScentedCandle object at $3 per inch. Write a MickWicks class that instantiates an object of each type of candle, sets color and height, and displays the details (name, height and price). Run the MickWicks program to test the results.