Handout 9 OO Inheritance.
|
|
- Easter Rich
- 5 years ago
- Views:
Transcription
1 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 1 of 11 Handout 9 OO Inheritance. All classes in Java form a hierarchy. The top of the hierarchy is class Object Example: classicalarchives.com website maintains a list of its customers and members, who are identified by their address. Members maintain a password. CLASS Object.... all other classes CLASS Customer String set () get () Notes: Subclass "IS a more specific Kind of " Superclass. Every object of a subclass is also an object of the superclass. The reverse is not true. Superclass (also called parent class) contains variables and methods common to all subclasses. A subclass (a.k.a. child class) automatically inherits all the data fields (variables) and (public) behaviors (methods) of the superclass (a.k.a. parent) class. Subclass can define additional methods and variables that are unique to it. This will not affect objects of the parent class, or siblings. Subclass can define its own version of an inherited method (will see lated). This is called overriding. The Object class: CLASS Member String password setpassword() superclass of all Java classes, i.e. Root of the entire Java class hierarchy. Defines the default implementation of the tostring() method to return a Java internal object ID string. 1
2 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 2 of 11 The extend keyword is used to create a subclass of a class. class subclass_name extends superclass_name {... // Customer.java - implements a customer of online service public class Customer { private String ; // login name public String get () {return this. ; public void set (String addr) {this. = addr; // Member.java - subclass of Customer public class Member extends Customer { private String password; public void setpassword(string passwd) { this.password = passwd; Member can access (i.e. use) all public and protected methods/instance vars and methods of Customer. Member inherits instance variable from the Customer class, but must use accessor/mutator to access it, since is private! /* Demonstrates inheritance of methods and instance vars, * Assignment between subclass and superclass type variables * and type casting. */ public class DemoInheritance { public static void main(string[] args) { Customer c = new Customer(); c.set ("madeye@hg.mg"); System.out.println(c.get ()); Member m = new Member(); m.set ("cbrown@bentley.edu"); //inherited from Customer m.setpassword("foo1234"); // defined in class Member System.out.println(m.get ()); //inherited from Customer // Also note, every Member is a Customer, // so the following assignment is legal: Customer c1 = m; System.out.println(c1.get ()); 2
3 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 3 of 11 Notes: // System.out.println(c1.getPassword()); does not work, but ((Member) c1).setpassword("foo");// works since c1 is // typecast as Member // but not every Customer is a Member, // so the following would not work: // Member m1 = c; // Error: cannot convert from Customer to Member, i.e. //and Member m1 = (Member) c; generates a runtime error 1. A variable (or method parameter) of superclass type can store (be passed) a value of any of its subclass types. (e.g. a var of Customer type can be assigned a Member-type object). e.g. Customer c1 = m; where since declaration is Member m; 2. The reverse of 1. is not allowed. 3. When a method is called, the compiler checks that the method is defined for the declared class of the variable storing the calling object or its ancestor, e.g. m.set ("cbrown"); is allowed since declaration is Member m; m.setpassword("foo1234"); is allowed, since declaration is Member m; c1.getpassword(); is NOT allowed since declaration is Customer c1 because getpassword() is defined only in Member 4. A variable of the superclass type can be typecast as a subclass, e.g. ((Member) c1).setpassword("foo"); If the value stored in c1 is not an object of type Member, a run-time error stating: setpassword() is not applicable to c1 will ensue. 5. To check if an object is an instance of a class use instanceof operator, e.g. o m instanceof Member -> true o m instanceof Customer -> true o m instanceof String -> false To obtain the class name of an object s class (bottom of the inheritance hierarchy) use method getclass() o c.getclass() -> Customer o m.getclass() -> Member (Practice question: which class defines method getclass()?) 3
4 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 4 of public and protected methods and variables of the superclass can be accessed directly from the subclass. private variables of superclass must be accessed using superclass public methods from the subclass Example: // won t work public Member(String ) { this. = name; // will work public Member(String ) { this.set ( ); // or just: set ( ); *** Within a subclass, use the set and get methods of the superclass for accessing the private instance variables of that superclass. 2. Overriding: Defining another version of an inherited method. The overriding method has the same signature (different from overloading different signature). Example tostring() method. It s defined in the Object class. When classes define their own tostring() method they override tostring() of Object class. Dynamic Binding: When an overridden method is called, the run-time class of the calling object (i.e. type of the actual object) determines which method is called. Example: download() method (see code on next pages): Business rules Customers are allowed only 3 downloads. Members are allowed unlimited number of downloads. Implementation: Customer class defines download() method. Member class re-defines, i.e. overrides it. 4
5 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 5 of 11 /* Customer.java - now with download. Allows only up to MAX_DOWNLOADS downloads */ public class Customer { public static final int MAX_DOWNLOADS = 3; private String ; // login name private int numdownloads = 0; // counter of number of downloads // made by this customer public String get () {return this. ; public void set (string addr){this. = addr; /** Check if reached maximum allowed number of downloads, or just increment numdownloads by 1 */ public void download(){ if (this.numdownloads < Customer.MAX_DOWNLOADS){ this.incrementnumdownloads(); System.out.println((Customer.MAX_DOWNLOADS this.numdownloads) + " downloads left."); else System.out.println("Cannot download more than "+ Customer.MAX_DOWNLOADS+" times."); /* Increment numdownloads by 1 */ public void incrementnumdownloads(){ this.numdownloads++; /* Member.java - subclass of Customer Overrides download() method allowing unlimited downloads*/ public class Member extends Customer { private String password; public void setpassword(string passwd) { this.password = passwd; public void download(){ // note this.numdownloads++ would not work, because it's private // Would have worked if it were public or protected System.out.println("Number of downloads is not limited."); this.incrementnumdownloads(); // iherited from superclass 5
6 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 6 of 11 When an overridden method is called, the run-time class of the calling object (i.e. type of the actual object) determines which method is called. /* Demonstrates inheritance of methods and instance vars, * Assignment between subclass and superclass type variables, dynamic binding. */ public class DemoOverriding { public static void main(string[] args) { Customer c = new Customer(); c.set ("madeye@hg.mg"); Member m = new Member(); m.set ("cbrown@bentley.edu"); m.setpassword("foo1234"); System.out.println(c.get ()); for (int i = 1; i<=4; i++){ c.download(); /* Prints: madeye@hg.mg 2 downloads left. 1 downloads left. 0 downloads left. Cannot download more than 3 times. */ System.out.println("\n"+m.get ()); for (int i = 1; i<=4; i++){ m.download(); /* Prints: cbrown@bentley.edu Number of downloads is not limited. Number of downloads is not limited. Number of downloads is not limited. Number of downloads is not limited. */ Customer next; String whoisnext = JOptionPane.showInputDialog ("Who downloads next: " + c.get () + "[1] or " + m.get () + "[anything]?" ); if (whoisnext.equals("1")) next = c; else next = m; // What will be printed? System.out.println("\nPicked "+next.get ()); next.download(); /* Depending on value stored in next, prints Picked madeye@hg.mg Cannot download more than 3 times. or Picked cbrown@bentley.edu Number of downloads is not limited. */ 6
7 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 7 of 11 Note: Changing the number or type of parameters is called method overloading, not overriding. Warning: if a subclass method is identical in all ways to a superclass method except that it has a different return type, then a compiler error will result. Polymorphism: (a.k.a: dynamic binding, late binding, or runtime binding) Means can take many forms Methods with the same name perform different actions on different objects Implemented by overloading and overriding in Java Polymorphic method (e.g. download() in the Customer/Member example, tostring() in other examples) Has many structures (different signatures, when overloaded) Can operate on different types of objects. Each object knows how it is to be acted on. Overriding is also referred to as: dynamic typing, dynamic method dispatch, or pure polymorphism. Dynamic ~ Run Time (vs. compile time) 3. Inheritance and Constructors If constructor not included, default one (no-args) is supplied automatically Constructors are never inherited. The superclass constructor is invoked from a subclass constructor by super() (possibly, with parameters) as a first statement. If invocation of a superclass constructor is not included in subclass constructor as he first statement, default (no-argument) constructor of superclass is called automatically before executing the first line of the subclass constructor. If superclass default constructor (i.e., no-arg) does not exist, but a constructor with args does, then error will result. // Customer.java - with constructors public class Customer { public static final int MAX_DOWNLOADS = 3; private String ; // login name private int numdownloads = 0; // counter of number of downloads // made by this customer public Customer() { this. = "unknown"; public Customer(String ) { this. = ; // the rest of Customer class follows... 7
8 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 8 of 11 // Member.java no constructor defined public class Member extends Customer { private String password; public void setpassword(string passwd) { this.password = passwd; /* Increments counter of downloads, prints a message */ public void download(){... code omitted Given the above Member m = new Member(); System.out.println(m.get ); prints: unknown To explicitly reference a superclass constructor, must be the first statement inside of the constructor for the subclass, along with any required arguments. /* Member.java - subclass of Customer, now with constructors */ public class Member extends Customer { private String password; public Member() { // implicitely calls super(); public Member(String , String passwd) { super( ); //calls 1-arg constructor of Customer class this.password = passwd; public void setpassword(string passwd) { this.password = passwd; /* Increments counter of downloads, prints a message */ public void download(){... code omitted 8
9 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 9 of 11 Another Example: overriding, using super to access methods of superclass Class Object CLASS BankAccount... balance deposit() withdraw() CLASS CheckingAccount minbalance charge processcheck() withdraw() CLASS SavingsAccount interestrate postinterest() The withdraw method can be defined for the superclass BankAccount, and for the subclass CheckingAccount public class BankAccount { private double balance; // instance variable // constructors public BankAccount(){ // no arg constructor public BankAccount(double b) {balance = b; // starting balance // mutator methods - increment balance public void setbalance(double amount) {balance = amount; public void deposit(double amount) { balance += amount; public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); // accessor method return balance public double getbalance() {return balance; 9
10 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 10 of 11 public class CheckingAccount extends BankAccount { private double minbalance; private double charge; public CheckingAccount(double minbalance, double charge) { // will look for no-arg super constructor first // since not explicitly invoking a super constructor this.minbalance = minbalance; this.charge = charge; public void processcheck(double amount) { this.withdraw(amount); public void withdraw(double amount) { if (getbalance() >= minbalance) super.withdraw(amount); else super.withdraw(amount + charge); // this not required Instances of a subclass can always be used where an object of the superclass is expected. An instance of CheckingAccount always inherits from BankAccount a CheckingAccount is always a BankAccount Another example: (are BankAccount or CheckingAcount methods used?) BankAccount myaccount = new CheckingAccount(100, 20); myaccount.deposit(500); myaccount.withdraw(10.0); The following code is not legal: myaccount.processcheck(25.00); because type of myaccount variable is BankAccound, and processcheck is not defined in BankAccount. Compiler can't tell that myaccount is also a CheckingAccount processcheck is not defined for class BankAccount Solution: either of the three below would work Declare myaccount as a CheckingAccount from the start Define a dummy, or shell processcheck method for BankAccount to be overridden by certain subclasses. Cast myaccount to CheckingAccount 10
11 Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 11 of 11 Problems: public class SavingsAccount extends BankAccount { private double interestrate; // subclass instance variable public SavingsAccount(double amount, double rate) { super(amount); // must be first! interestrate = rate; public void postinterest() { double balance = getbalance(); double interest = interestrate/100*balance; setbalance(balance + interest); public String tostring() { return "\tsavingsaccount balance = " + this.getbalance(); 1. Given the earlier definitions for BankAccount and the above definition for SavingsAccount, is this legal? BankAccount myaccount = new BankAccount(); SavingsAccount saccount = new SavingsAccount(200.0,5.0); saccount = myaccount; 2. Which of the following will result in a compiler error if account anaccount is declared as follows? Identify the class of the invoked method if no error results. (a) SavingsAccount anaccount= new SavingsAccount(100, 20); (b) BankAccount anaccount= new SavingsAccount(100, 20); anaccount.postinterest(); anaccount.withdraw(5.0); anaccount.processcheck(10.0); anaccount.setbalance(50.0); anaccount.getbalance(); 11
Intro to Computer Science 2. Inheritance
Intro to Computer Science 2 Inheritance Admin Questions? Quizzes Midterm Exam Announcement Inheritance Inheritance Specializing a class Inheritance Just as In science we have inheritance and specialization
More informationInheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13
Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13 Fall 2006 Adapted from Java Concepts Companion Slides 1 Chapter Goals To learn about inheritance To understand how
More informationCHAPTER 10 INHERITANCE
CHAPTER 10 INHERITANCE Inheritance Inheritance: extend classes by adding or redefining methods, and adding instance fields Example: Savings account = bank account with interest class SavingsAccount extends
More informationChapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy
Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package access control To
More informationIntroduction to Inheritance
Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits
More informationInheritance (P1 2006/2007)
Inheritance (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To learn about
More informationInheritance and Subclasses
Software and Programming I Inheritance and Subclasses Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Packages Inheritance Polymorphism Sections 9.1 9.4 slides are available at www.dcs.bbk.ac.uk/
More informationInheritance: Definition
Inheritance 1 Inheritance: Definition inheritance: a parent-child relationship between classes allows sharing of the behavior of the parent class into its child classes one of the major benefits of object-oriented
More informationInheritance & Abstract Classes Fall 2018 Margaret Reid-Miller
Inheritance & Abstract Classes 15-121 Margaret Reid-Miller Today Today: Finish circular queues Exercise: Reverse queue values Inheritance Abstract Classes Clone 15-121 (Reid-Miller) 2 Object Oriented Programming
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationChapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.
Chapter 10 Inheritance Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package
More informationInheritance -- Introduction
Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept
More informationCS-202 Introduction to Object Oriented Programming
CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction
More informationArgument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar
More informationHandout 8 Classes and Objects Continued: Static Variables and Constants.
Handout 8 CS603 Object-Oriented Programming Fall 16 Page 1 of 8 Handout 8 Classes and Objects Continued: Static Variables and Constants. 1. Static variable Declared with keyword static One per class (instead
More informationMore On inheritance. What you can do in subclass regarding methods:
More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature
More informationRules and syntax for inheritance. The boring stuff
Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for
More informationCheck out Polymorphism from SVN. Object & Polymorphism
Check out Polymorphism from SVN Object & Polymorphism Inheritance, Associations, and Dependencies Generalization (superclass) Specialization (subclass) Dependency lines are dashed Field association lines
More informationPractice for Chapter 11
Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing
More informationINHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U
C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes
More informationInheritance. Transitivity
Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated
More informationWhat is Inheritance?
Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting
More informationInheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L
Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive
More informationCMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.
CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object
More informationGraphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java
More informationCSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism
CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism Review from Lecture 22 Added parent pointers to the TreeNode to implement increment and decrement operations on tree
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationObject Oriented Programming. Java-Lecture 11 Polymorphism
Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class
More informationChapter 5 Object-Oriented Programming
Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares
More informationName Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?
1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes
More informationLesson 35..Inheritance
Lesson 35..Inheritance 35-1 Within a new project we will create three classes BankAccount, SavingsAccount, and Tester. First, the BankAccount class: public class BankAccount public BankAccount(double amt)
More informationInheritance CSC 123 Fall 2018 Howard Rosenthal
Inheritance CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference
More informationObject Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance
CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects:
More informationBinghamton University. CS-140 Fall Dynamic Types
Dynamic Types 1 Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected
More informationPolymorphism. return a.doublevalue() + b.doublevalue();
Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More informationLecture 18 CSE11 Fall 2013 Inheritance
Lecture 18 CSE11 Fall 2013 Inheritance What is Inheritance? Inheritance allows a software developer to derive a new class from an existing one write code once, use many times (code reuse) Specialization
More informationC++ Important Questions with Answers
1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children
More informationOOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.
OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every
More informationJava Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.
Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either
More informationClass, Variable, Constructor, Object, Method Questions
Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract
More informationJava Magistère BFA
Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt
More information25. Generic Programming
25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and
More informationInheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.
Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods
More informationProgramming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017
Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract
More informationEncapsulation. Mason Vail Boise State University Computer Science
Encapsulation Mason Vail Boise State University Computer Science Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes) Object Identity Data (variables) make
More information8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14
8.1 Inheritance superclass 8.1 Class Diagram for Words! Inheritance is a fundamental technique used to create and organize reusable classes! The child is- a more specific version of parent! The child inherits
More informationIST311. Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition
More informationInheritance and Polymorphism
Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab
More informationSoftware Design and Analysis for Engineers
Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:
More informationDay 4. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information
More informationC# Programming for Developers Course Labs Contents
C# Programming for Developers Course Labs Contents C# Programming for Developers...1 Course Labs Contents...1 Introduction to C#...3 Aims...3 Your First C# Program...3 C# The Basics...5 The Aims...5 Declaring
More informationSTUDENT LESSON A5 Designing and Using Classes
STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of
More informationInheritance and Polymorphism. CS180 Fall 2007
Inheritance and Polymorphism CS180 Fall 2007 Definitions Inheritance object oriented way to form new classes from pre-existing ones Superclass The parent class If class is final, cannot inherit from this
More informationOverview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008
Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object
More informationProgramming in C# Inheritance and Polymorphism
Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data
More informationCS 251 Intermediate Programming Inheritance
CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?
More informationPolymorphism and Inheritance
Walter Savitch Frank M. Carrano Polymorphism and Inheritance Chapter 8 Objectives Describe polymorphism and inheritance in general Define interfaces to specify methods Describe dynamic binding Define and
More informationObject-oriented basics. Object Class vs object Inheritance Overloading Interface
Object-oriented basics Object Class vs object Inheritance Overloading Interface 1 The object concept Object Encapsulation abstraction Entity with state and behaviour state -> variables behaviour -> methods
More informationInheritance and Polymorphism
Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg
More informationInheritance, Polymorphism, and Interfaces
Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)
More information1 Shyam sir JAVA Notes
1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write
More informationSuper-Classes and sub-classes
Super-Classes and sub-classes Subclasses. Overriding Methods Subclass Constructors Inheritance Hierarchies Polymorphism Casting 1 Subclasses: Often you want to write a class that is a special case of an
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationInheritance and Polymorphism
Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html
More informationCLASS DESIGN. Objectives MODULE 4
MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors
More informationCOSC This week. Will Learn
This week COSC1030.03 Read chapters 9, 11 S tart thinking about assignment 2 Week 4. J anuary 26, 2004 Will Learn how to inherit and override superclass methods how to invoke superclass constructors about
More informationWhat are the characteristics of Object Oriented programming language?
What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is
More informationChapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,
Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design
More informationThe software crisis. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:
More informationCSC Inheritance. Fall 2009
CSC 111 - Inheritance Fall 2009 Object Oriented Programming: Inheritance Within object oriented programming, Inheritance is defined as: a mechanism for extending classes by adding variables and methods
More informationMore about inheritance
Main concepts to be covered More about inheritance Exploring polymorphism method polymorphism static and dynamic type overriding dynamic method lookup protected access 4.1 The inheritance hierarchy Conflicting
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic
More informationMore Relationships Between Classes
More Relationships Between Classes Inheritance: passing down states and behaviors from the parents to their children Interfaces: grouping the methods, which belongs to some classes, as an interface to
More informationComputer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7
Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer
More informationLecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting
Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006
More informationUNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED
UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance
More informationBig software. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting
More informationChapter 5. Inheritance
Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism through Method Overriding Learn the keywords : super
More informationSWC test question #01. Using objects part I
SWC test question #01 Using objects part I Using objects part I Give a presentation of the concept objects, and how you use objects You could describe Types Variables Variable definitions The assignment
More informationCS Programming I: Inheritance
CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance
More informationInheritance Motivation
Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
More informationENCAPSULATION AND POLYMORPHISM
MODULE 3 ENCAPSULATION AND POLYMORPHISM Objectives > After completing this lesson, you should be able to do the following: Use encapsulation in Java class design Model business problems using Java classes
More informationOverriding Variables: Shadowing
Overriding Variables: Shadowing We can override methods, can we override instance variables too? Answer: Yes, it is possible, but not recommended Overriding an instance variable is called shadowing, because
More informationCOP 3330 Final Exam Review
COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits
More informationVIRTUAL FUNCTIONS Chapter 10
1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions
More informationAbstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal
Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information
More informationInheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass
Inheritance and Polymorphism Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism Inheritance (semantics) We now have two classes that do essentially the same thing The fields are exactly
More informationData Abstraction. Hwansoo Han
Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed
More information24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini
24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationPROGRAMMING LANGUAGE 2
31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It
More informationCS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:
CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides
More informationObjective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5
Objective Questions BCA Part III page 1 of 5 1. Java is purely object oriented and provides - a. Abstraction, inheritance b. Encapsulation, polymorphism c. Abstraction, polymorphism d. All of the above
More informationStarting Out with Java: From Control Structures Through Objects Sixth Edition
Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 10 Inheritance Chapter Topics (1 of 2) 10.1 What Is Inheritance? 10.2 Calling the Superclass Constructor 10.3 Overriding
More information