CSC 1214: Object-Oriented Programming

Size: px
Start display at page:

Download "CSC 1214: Object-Oriented Programming"

Transcription

1 CSC 1214: Object-Oriented Programming J. Kizito Makerere University www: materials: e-learning environment: office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 Encapsulation, Inheritance, and Polymorphism Kizito (Makerere University) CSC 1214 February, / 24

2 Overview 1 Encapsulation 2 Inheritance Introduction Variable Referencing Using super Method overriding Using final Abstract Methods and Classes 3 Polymorphism 4 Homework Kizito (Makerere University) CSC 1214 February, / 24

3 Encapsulation Encapsulation Encapsulation (aka Information hiding) is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse This is achieved by making the fields in a class private and providing access to the fields via public methods Access to the code and data is controlled through a well-defined interface In java the basis of encapsulation is the class The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code Encapsulation gives maintainability, flexibility and extensibility to our code Kizito (Makerere University) CSC 1214 February, / 24

4 Encapsulation Encapsulation Example 1 (without encapsulation) class Car { String numberplate; // e.g. UBC 080A double speed = 0.0; // in kilometers per hour double maxspeed; // in kilometers per hour int year; double oldcarspeedlimit = 180.0; class CarDriver { public static void main(string args[]){ Car mycar = new Car(); mycar.speed = 380.0; // No encapsulation Kizito (Makerere University) CSC 1214 February, / 24

5 Encapsulation Encapsulation Example 2 (with encapsulation): Accessors and Mutators aka Getters and Setters public class EncapTest{ private String name, idnum; private int age; public int getage(){ return age; public String getname(){ // Accessor return name; public String getidnum(){ // Accessor return idnum; public void setage(int newage){ age = newage; public void setname(string newname){ name = newname; public void setidnum(string newid){ idnum = newid; public class RunEncap{ public static void main(string args[]){ EncapTest encap = new EncapTest(); // encap.name = "James"; Errro! encap.setname("james"); encap.setage(20); encap.setidnum("12343ms"); System.out.print( "Name : " + encap.getname()+ ", Age : " + encap.getage()); Kizito (Makerere University) CSC 1214 February, / 24 Output Name : James, Age : 20

6 Encapsulation Encapsulation Example 2 (with encapsulation): Accessors and Mutators aka Getters and Setters public class EncapTest{ private String name, idnum; private int age; public int getage(){ return age; public String getname(){ // Accessor return name; public String getidnum(){ // Accessor return idnum; public void setage(int newage){ age = newage; public void setname(string newname){ name = newname; public void setidnum(string newid){ idnum = newid; public class RunEncap{ public static void main(string args[]){ EncapTest encap = new EncapTest(); // encap.name = "James"; Errro! encap.setname("james"); encap.setage(20); encap.setidnum("12343ms"); System.out.print( "Name : " + encap.getname()+ ", Age : " + encap.getage()); Kizito (Makerere University) CSC 1214 February, / 24 Output Name : James, Age : 20

7 Encapsulation Encapsulation Example 2 (with encapsulation): Accessors and Mutators aka Getters and Setters public class EncapTest{ private String name, idnum; private int age; public int getage(){ return age; public String getname(){ // Accessor return name; public String getidnum(){ // Accessor return idnum; public void setage(int newage){ age = newage; public void setname(string newname){ name = newname; public void setidnum(string newid){ idnum = newid; public class RunEncap{ public static void main(string args[]){ EncapTest encap = new EncapTest(); // encap.name = "James"; Errro! encap.setname("james"); encap.setage(20); encap.setidnum("12343ms"); System.out.print( "Name : " + encap.getname()+ ", Age : " + encap.getage()); Kizito (Makerere University) CSC 1214 February, / 24 Output Name : James, Age : 20

8 Encapsulation Encapsulation Access control Visibility Modifiers Recall... In Java, we accomplish encapsulation through the appropriate use of visibility modifiers A modifier is a Java reserved word that specifies particular characteristics of a method or data Java has three visibility modifiers: public, protected, and private Access Specifiers/ Visibility Modifiers Variables Methods public Violate encapsulation Provide services to client objects private Enforce encapsulation Support other methods in the class Kizito (Makerere University) CSC 1214 February, / 24

9 Introduction Inheritance Inheritance is the process by which one object acquires the properties of another Supports the concept of hierarchical classification An inherited subclass (or child class) inherits all of the attributes from each of its ancestors in the class hierarchy The class from which the subclass is derived is called a superclass (also a base class or a parent class) Inheritance interacts with encapsulation as well To inherit a class, you incorporate the definition of one class into another by using the extends keyword All classes in the Java platform are descendants of Object Kizito (Makerere University) CSC 1214 February, / 24

10 Introduction Inheritance Example (3) class MountainBike extends Bicycle { /* * new fields and methods defining * a mountain bike would go here */ Java does not support multiple inheritance. OO languages that support multiple inheritance include C++, Python, Common Lisp In Java, we use the reserved keyword extends to establish an inheritance relationship between classes Kizito (Makerere University) CSC 1214 February, / 24

11 Introduction Inheritance Example (4) Box.java 1. // This program uses inheritance to extend Box 2. // Also illustrates method/constructor overloading 3. class Box { 4. double width, height, depth; 5. // construct clone of an object 6. Box(Box ob) { // pass object to constructor 7. width = ob.width; 8. height = ob.height; 9. depth = ob.depth; // constructor used when all dimensions specified 12. Box(double w, double h, double d) { 13. width = w; 14. height = h; 15. depth = d; Box() { // constructor used when no dimensions specified 18. // use -1 to indicate an uninitialized box 19. width = height = depth = -1; // constructor used when cube is created 22. Box(double len) { 23. width = height = depth = len; // compute and return volume 26. double volume() { 27. return width * height * depth; Kizito (Makerere University) CSC 1214 February, / 24

12 Introduction Inheritance Example (4) DemoBoxWeight.java 1. // Here, Box is extended to include weight 2. class BoxWeight extends Box { 3. double weight; // weight of the box // constructor for BoxWeight 6. BoxWeight(double w, double h, double d, double m) { 7. width = w; 8. height = h; 9. depth = d; 10. weight = m; class DemoBoxWeight { 15. public static void main(string args[]) { 16. BoxWeight mybox1= new BoxWeight(10, 20, 15, 34.3); 17. BoxWeight mybox2= new BoxWeight(2, 3, 4, 0.076); 18. Double vol; vol = mybox1.volume(); 21. System.out.println("Volume of mybox1 is " + vol); 22. System.out.println("Weight of mybox1 is " + mybox1.weight); 23. System.out.println(); vol = mybox2.volume(); 26. System.out.println("Volume of mybox2 is " + vol); 27. System.out.println("Weight of mybox2 is " + mybox2.weight); Output: Volume of mybox1 is Weight of mybox1 is 34.3 Volume of mybox2 is 24.0 Weight of mybox2 is Kizito (Makerere University) CSC 1214 February, / 24

13 Variable Referencing Inheritance Superclass variable can reference subclass object BoxWeight wbox = new BoxWeight(3, 5, 7, 8.37); Box box = new Box(); double vol; // assign BoxWeight ref to Box ref box = wbox; vol = box.volume(); // OK // the following is invalid System.out.print("box s weight is "+ box.weight); Kizito (Makerere University) CSC 1214 February, / 24

14 Using super Inheritance Using super Avoids duplication of code, which is inefficient Subclasses do not necessarily have access to superclass members Two general forms: 1 Call a constructor method of a superclass super(parameter-list); 2 Access members of a superclass super.member Kizito (Makerere University) CSC 1214 February, / 24

15 Using super Inheritance Example (5) Using super // implementation of BoxWeight using super class Box { double width, height, depth; // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; // constructor used when no dimensions specified Box() { // use -1 to indicate an uninitialized box width = height = depth = -1; // constructor used when cube is created Box(double len) { width = height = depth = len; // compute and return volume double volume() { return width * height * depth; 30. // Add weight 31. class BoxWeight extends Box { 32. double weight; // weight of the box // construct clone of passed object 35. BoxWeight(BoxWeight ob) { 36. super(ob); 37. weight = ob.weight; // constructor when all parameters are specified 40. BoxWeight(double w, double h, double d, double m) 41. { 42. super(w, h, d); 43. weight = m; // default constructor 46. BoxWeight() { 47. super(); 48. weight = -1; // constructor used when cube is created 51. BoxWeight(double len, double m) { 52. super(len); 53. weight = m; Kizito (Makerere University) CSC 1214 February, / 24

16 Method overriding Inheritance Method overriding In a class hierarchy, when a method in a subclass has the same signature as a method in its superclass The super class method is said to be hidden By same signature we mean the subclass method has the same name, number and type of parameters, and return type as the one it overrides/hides An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type Another use of super unhides the hidden method Kizito (Makerere University) CSC 1214 February, / 24

17 Method overriding Method overriding Example (6) class A { int i, j; A(int a, int b) {i = a; j = b; void show() { System.out.print("i and j: "+ i +" "+ j); class B extends A { int k; //... void show() { super.show(); // call superclass version System.out.print(" k: "+ k); Kizito (Makerere University) CSC 1214 February, / 24

18 Using final Inheritance Using final to prevent Overriding Methods declared as final cannot be overridden class A { final void meth() { System.out.println("A final method"); class B extends A { void meth() { // ERROR! Can t override System.out.println("Illegal"); Kizito (Makerere University) CSC 1214 February, / 24

19 Using final Inheritance Using final to prevent Inheritance Classes declared as final cannot be inherited A final class has all of its methods as final, too final class A { //... // The following class is illegal class B extends A { // ERROR Can t subclass A //... Kizito (Makerere University) CSC 1214 February, / 24

20 Using final Inheritance Method Overloading Overloading Vs Overriding Don t confuse the concepts of overloading and overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Kizito (Makerere University) CSC 1214 February, / 24

21 Abstract Methods and Classes Inheritance Abstract Methods and Classes You can require that certain methods be overridden by subclasses An abstract method is a method that is declared without an implementation abstract void moveto(double deltax, double deltay); An abstract class is a class that is declared abstract it may or may not include abstract methods A class that includes abstract methods must be declared abstract When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract An abstract class can not be instantiated Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods If an abstract class contains only abstract method declarations, it should be declared as an interface instead Kizito (Makerere University) CSC 1214 February, / 24

22 Abstract Methods and Classes Abstract Methods and Classes Example (7) abstract class GraphicObject { int x, y; //... void moveto(int newx, int newy) { /*... */ abstract void draw(); abstract void resize(); class Circle extends GraphicObject { void draw() { /*... */ void resize() { /*... */ class Rectangle extends GraphicObject { void draw() { /*... */ void resize() { /*... */ Kizito (Makerere University) CSC 1214 February, / 24

23 Polymorphism Polymorphism Polymorphism (from the Greek, meaning many forms ) is a feature that allows one interface to be used for a general class of actions In OOP, a reference can be polymorphic, which can be defined as having many forms One interface, multiple methods Consider a program that requires three types of stacks (integer, floating-point, and character values) Stack algorithm is the same Data being stored defers though Polymorphism, Encapsulation, and Inheritance work together We have already seen cases where Inheritance allows subclasses of a class to define their own unique behaviors and yet share some of the same functionality of the parent class this is Polymorphism Kizito (Makerere University) CSC 1214 February, / 24

24 Polymorphism Polymorphism Example (8) Recall our Bicycle example. We could have a method printdescription() overriden as below: public class MountainBike extends Bicycle { //... public void printdescription() { super.printdescription(); System.out.println("The MountainBike has a" + getsuspension() +" suspension."); public class RoadBike extends Bicycle { //... public void printdescription() { super.printdescription(); System.out.println("The RoadBike has "+gettirewidth()+"mm tires."); Kizito (Makerere University) CSC 1214 February, / 24

25 Homework Assignment Ia Classes and Objects Class Libraries, Packages, and GUI Applications Read about Class libraries, Packages, and GUI Applications (AWT and Swing) See ClassLibrariesPackagesGUIApps.pdf Kizito (Makerere University) CSC 1214 February, / 24

26 Homework Assignment Ib Classes and Objects Approximating Mathematical Constant π = Using Objects in the Java Language class simplecalc { // Your task is to define this class simplecalcdemo { public static void main (String [] argv) { // This I have written for you Group size and what to hand in Due date In case of groups, maximum size is 6 (six)? Hand in hard copies of your source code Monday 26 th February 2018 Some π approximations: gives more exact digits? 3 1, 22 7, , , , , , , , , , Test your program correct to at least 14 decimal places Kizito (Makerere University) CSC 1214 February, / 24

27 Homework Assignment Ib Classes and Objects Approximating Mathematical Constant π = Using Objects in the Java Language class simplecalc { // Your task is to define this class simplecalcdemo { public static void main (String [] argv) { // This I have written for you Group size and what to hand in Due date In case of groups, maximum size is 6 (six)? Hand in hard copies of your source code Monday 26 th February 2018 Some π approximations: gives more exact digits? 3 1, 22 7, , , , , , , , , , Test your program correct to at least 14 decimal places Kizito (Makerere University) CSC 1214 February, / 24

JAVA. Lab-9 : Inheritance

JAVA. Lab-9 : Inheritance JAVA Prof. Navrati Saxena TA- Rochak Sachan Lab-9 : Inheritance Chapter Outline: 2 Inheritance Basic: Introduction Member Access and Inheritance Using super Creating a Multilevel Hierarchy Method Overriding

More information

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Unit 3 INFORMATION HIDING & REUSABILITY -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Inheritance Inheritance is one of the cornerstones of objectoriented programming

More information

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. PART 17 17. Inheritance Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

More information

Hierarchical abstractions & Concept of Inheritance:

Hierarchical abstractions & Concept of Inheritance: UNIT-II Inheritance Inheritance hierarchies- super and subclasses- member access rules- super keyword- preventing inheritance: final classes and methods- the object class and its methods Polymorphism dynamic

More information

Inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.

Inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. Unit-2 Inheritance: Inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. The general form of a class declaration that inherits a super class is

More information

Java Object Oriented Design. CSC207 Fall 2014

Java 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 information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

INHERITANCE Inheritance Basics extends extends

INHERITANCE Inheritance Basics extends extends INHERITANCE Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING 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 information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Class A class is a template that specifies the attributes and behavior of things or objects. A class is a blueprint or prototype from which objects are created. A class is the implementation of an abstract

More information

ITI Introduction to Computing II

ITI 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 information

CS-202 Introduction to Object Oriented Programming

CS-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 information

ITI Introduction to Computing II

ITI 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 information

Inheritance. The Java Platform Class Hierarchy

Inheritance. The Java Platform Class Hierarchy Inheritance In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those

More information

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

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

What is Inheritance?

What 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 information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

More information

Programming II (CS300)

Programming 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 information

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

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

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

8.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 information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical 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 information

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

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

INHERITANCE & 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 information

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616 OBJECT ORIENTED PROGRAMMING Course 4 Loredana STANCIU loredana.stanciu@upt.ro Room B616 Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class,

More information

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

IT101. Inheritance, Encapsulation, Polymorphism and Constructors 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

More information

(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson )

(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson ) (SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson ) Dongwon Jeong djeong@kunsan.ac.kr; http://ist.kunsan.ac.kr/ Information Sciences and Technology Laboratory, Dept. of

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

ECE 122. Engineering Problem Solving with Java

ECE 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 information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, 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 information

G Programming Languages - Fall 2012

G 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 information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract

More information

Chapter 5 Object-Oriented Programming

Chapter 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 information

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

Argument 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 information

CS 251 Intermediate Programming Inheritance

CS 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 information

Chapter 5. Inheritance

Chapter 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 information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

Subclasses, Superclasses, and Inheritance

Subclasses, Superclasses, and Inheritance Subclasses, Superclasses, and Inheritance To recap what you've seen before, classes can be derived from other classes. The derived class (the class that is derived from another class) is called a subclass.

More information

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

Object 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 information

Computer Science 210: Data Structures

Computer Science 210: Data Structures Computer Science 210: Data Structures Summary Today writing a Java program guidelines on clear code object-oriented design inheritance polymorphism this exceptions interfaces READING: GT chapter 2 Object-Oriented

More information

Inheritance. 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 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 information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object 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 information

Inheritance and Polymorphism

Inheritance 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 information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

HAS-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. 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 information

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding Conformance Conformance and Class Invariants Same or Better Principle Access Conformance Contract Conformance Signature Conformance Co-, Contra- and No-Variance Overloading and Overriding Inheritance as

More information

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

Day 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 information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

More information

CMSC 132: Object-Oriented Programming II. Inheritance

CMSC 132: Object-Oriented Programming II. Inheritance CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

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

Programming 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 information

Exercise: Singleton 1

Exercise: Singleton 1 Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance

More information

8. Polymorphism and Inheritance

8. Polymorphism and Inheritance 8. Polymorphism and Inheritance Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe polymorphism and inheritance in general Define interfaces

More information

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object Oriented Programming: Based on slides from Skrien Chapter 2 Object Oriented Programming: A Review Based on slides from Skrien Chapter 2 Object-Oriented Programming (OOP) Solution expressed as a set of communicating objects An object encapsulates the behavior and

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming

More information

Inheritance (continued) Inheritance

Inheritance (continued) Inheritance Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses

More information

Inheritance -- Introduction

Inheritance -- 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 information

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

Computer 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 information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary Programming in Java, 2e Sachin Malhotra Saurabh Choudhary Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

HAS-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. 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 information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract 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 information

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

Contents. 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 information

Week 5-1: ADT Design

Week 5-1: ADT Design Week 5-1: ADT Design Part1. ADT Design Define as class. Every obejects are allocated in heap space. Encapsulation : Data representation + Operation Information Hiding : Object's representation part hides,

More information

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

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation CSE 452: Programming Languages Data Abstraction and Object-Orientation Previous Lecture Abstraction Abstraction is the elimination of the irrelevant and the amplification of the essential Robert C Martin

More information

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 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 information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following. Inheritance The concept of inheritance is one of the key features of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define more specific

More information

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Class Interaction There are 3 types of class interaction. One

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

ENCAPSULATION AND POLYMORPHISM

ENCAPSULATION 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 information

Implements vs. Extends When Defining a Class

Implements vs. Extends When Defining a Class Implements vs. Extends When Defining a Class implements: Keyword followed by the name of an INTERFACE Interfaces only have method PROTOTYPES You CANNOT create on object of an interface type extends: Keyword

More information

Inheritance. Transitivity

Inheritance. 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 information

Overview of Eclipse Lectures. Module Road Map

Overview of Eclipse Lectures. Module Road Map Overview of Eclipse Lectures 1. Overview 2. Installing and Running 3. Building and Running Java Classes 4. Refactoring Lecture 2 5. Debugging 6. Testing with JUnit 7. Version Control with CVS 1 Module

More information

INHERITANCE. Spring 2019

INHERITANCE. Spring 2019 INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose

More information

Java for Non Majors Spring 2018

Java for Non Majors Spring 2018 Java for Non Majors Spring 2018 Final Study Guide The test consists of 1. Multiple choice questions - 15 x 2 = 30 points 2. Given code, find the output - 3 x 5 = 15 points 3. Short answer questions - 3

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Chapter 5: Classes and Objects in Depth. Information Hiding

Chapter 5: Classes and Objects in Depth. Information Hiding Chapter 5: Classes and Objects in Depth Information Hiding Objectives Information hiding principle Modifiers and the visibility UML representation of a class Methods Message passing principle Passing parameters

More information

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018 Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Class Inheritance: Deriving classes Method overriding Class hierarchies Abstract classes Visibility and inheritance

More information

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1 Object-Oriented Languages and Object-Oriented Design Ghezzi&Jazayeri: OO Languages 1 What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Lecture 12 Summary Inheritance Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Multiplicity 1 By the end of this lecture, you will

More information

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 In Previous Lecture We

More information

Darrell Bethea June 6, 2011

Darrell Bethea June 6, 2011 Darrell Bethea June 6, 2011 Program 4 due Friday Testing/debugging help online Final exam Comprehensive Monday, 6/13, 8-11 AM SN014 2 3 Inheritance 4 We have discussed before how classes of objects can

More information

UCLA PIC 20A Java Programming

UCLA PIC 20A Java Programming UCLA PIC 20A Java Programming Instructor: Ivo Dinov, Asst. Prof. In Statistics, Neurology and Program in Computing Teaching Assistant: Yon Seo Kim, PIC Chapter 5 Classes & Inheritance Creating Classes

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming

More information

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015 COMP 110-001 Information Hiding and Encapsulation Yi Hong June 03, 2015 Review of Pass-By-Value What is the output? public void swap(student s1, Student s2) Student s3 = s1; s1 = s2; s2 = s3; Student berkeley

More information

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable. Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2008 January Exam Question Max Internal

More information