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

Size: px
Start display at page:

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

Transcription

1 Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes Composition Inheritance Extending Classes Method Overriding Initialization Combining Composition and Inheritance Abstract Classes Creating Abstract Classes Extending Abstract Classes Interfaces Creating Interfaces Implementing an Interface Multiple Inheritance Polymorphism Static Binding Dynamic Binding Interchangeable Objects & Upcasting Extensibility... 16

2 1 Reusing Classes Once a class has been created and tested, it should (ideally) represent a useful unit of code. If it happens that such a class has a good design and is useful we might want to reuse this class. Code reuse is one of the greatest advantages that object-oriented programming languages provide. There are two possibilities to reuse classes in object-oriented programs: Reusing implementation Reusing interface If we want to reuse implementation we basically compose a new class from the already existing classes, thus creating a composite class. We say that the composite class reuses the implementation from its components. On the other hand, if we want to reuse interface of a class we create a subclass of that class. Then we say that the subclass inherits functionality (methods) from the superclass. Thus, the subclass reuses the interface of the superclass. 1.1 Composition The simplest way to reuse a class within a new class is to place an object of that class inside the new class. The new class can be made up of any number and type of other objects, in any combination that is required to achieve the functionality that is needed. Because we are composing a new class from existing classes, this concept is called composition (aggregation). Composition is often referred to as a has-a relationship, as in a car has an engine. Let us look on the following example to see how composition works. Suppose we want to create the class definition for simple 2D shape objects. Each shape object has a color, a position where it is placed on the screen, a dimension (width and height), and so on. Now, suppose that we already have classes that represent color objects, point objects and dimension objects. For instance, the Color class might look as follows: public class Point{ private int x_; private int y_; public Point(){ public Point(int x, int y){ x_ = x; y_ = y;

3 public int getx(){ return x_; public int gety(){ return y_; public void setx(int x){ x_ = x; public void sety(int y){ y_ = y; Suppose that similar to that the Color class and the Dimension class have been defined and that they encapsulate the RGB values, and the values for width and height, respectively. Further, they provide a number of methods to manipulate the encapsulated data, as well as constructors for a parameterized initialization of objects. Now we might compose the Shape class from the Color, Point and Dimension classes: public class Shape{ private Color color_; private Point position_; private Dimension dim_; public Shape(){ public Shape(Point position, Dimension dim){ this(position, dim, new Color(0, 0, 0)); public Shape(Point position, Dimension dim, Color color){ position_ = position; dim_ = dim; color_ = color // here comes some code

4 // public Rectangle getbounds(){ return new Rectangle(position_.getX(), position_.gety(), dim_.getwidth(), dim_.getheight()); public void setbounds(rectangle bounds){ position_.setx(bounds.getx()); position_.sety(bounds.gety()); dim_.setwidth(bounds.getwidth()); dim_.setheight(bounds.getheight()); In two methods, printed in bold in the above code, we see an example of providing the Shape class with a certain functionality (getting and setting the bounds) by reusing the functionality, which is already provided by the Point and Dimension classes. Another interesting thing to notice in the above example is how we nested constructor calls. The second constructor, which takes a Point and a Dimension object calls the third constructor, which takes a Point, a Dimension and a Color object. This is achieved by using the self-reference of an object: this. Summarizing, composition comes with a great deal of flexibility. The member objects of the new class are usually private, making them inaccessible to the client programmers who are using the class. This allows programmers to change those members without disturbing existing client code. They can also change the member objects at run-time, to dynamically change the behavior of the composed objects. 1.2 Inheritance The second way to reuse a class code can be achieved through inheritance. Inheritance allows programmers to define a class as a subclass of an existing class. The subclass inherits in this way all instance variables and all methods from the basic class, as long as they declared as public or protected. The level of inheritance can be arbitrarily deep. That means that we can create a subclass from a class that is already a subclass of another class. If we have more then one level of inheritance then a subclass inherits variables and methods from all of ancestors of its superclass.

5 The subclass can use the instance variables or methods as is, or it can hide the member variables or override the methods Extending Classes The inheritance concept is called extending a class in Java. Thus, when we create a subclass of a class we say that we extend that class. Let us look again on an example. We use again the same example from the previous section. Suppose we have the Shape class representing 2D shapes that we can draw on the screen. In the previous section we presented this class to demonstrate the composition principle. Here we give the complete code for the Shape class. Note, that the code now includes methods that reflect a typical behavior that we would expect from the Shape class. Thus, we have methods that we can call in order to draw a shape or erase a shape for example. public class Shape{ protected Color color_; protected Point position_; protected Dimension dim_; public Shape(){ public Shape(Point position, Dimension dim){ this(position, dim, new Color(0, 0, 0)); public Shape(Point position, Dimension dim, Color color){ position_ = position; dim_ = dim; color_ = color public void draw(graphics g){ // here comes implementation public void erase(){ // here comes implementation

6 public Rectangle getbounds(){ return new Rectangle(position_.getX(), position_.gety(), dim_.getwidth(), dim_.getheight()); public void setbounds(rectangle bounds){ position_.setx(bounds.getx()); position_.sety(bounds.gety()); dim_.setwidth(bounds.getwidth()); dim_.setheight(bounds.getheight()); Suppose now that we want to represent different Shape objects, such as circles, rectangles, polygons, triangles, ellipses, etc. Obviously all these objects are shapes and share the same characteristics and behavior, such as they all have a color, a position, a dimension, or they can all be drawn, moved, erased, and so on. However, they all differ from each other in some way. For example, a triangle has three distinct points, whereas a polygon can have 6 points, etc. Obviously, the Shape class that we introduced represents very well the things that all shapes have in common, but we need more specialized classes in order to represent different shapes. These subclasses would extend the Shape class and add required instance variables or implement different, more specialized behavior. Thus, the Circle class would draw a circle on a user screen and the Triangle class would draw a triangle. Note also that we changed the access modifiers for instance variables of the Shape class from private to protected. We did so because we want subclasses of the Shape class to obtain access to its instance variables. Let us now look on an example for the Circle class: public class Circle extends Shape{ public void draw(graphics g){ // here comes the Circle specific code First, we notice the keyword extend, which declares a class to be a subclass of another class. Then we see that the Circle class just overrides (implements in another way) the draw method of the basic Shape class in order to implement a specific behavior for circle objects.

7 Let us look now on another example: public class Polygon extends Shape{ private Point[] points_; public Polygon(Point[] points){ points_ = points; public void draw(graphics g){ // here comes the Polygon specific code Note that the Polygon class not only overrides the draw method, but also adds a new, specific instance variable to the Polygon class. It is an array of Points that represent points of that polygon Method Overriding The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then override methods as needed. We saw this already on the examples of the Circle and the Polygon class that override the draw method of the basic Shape class. Let us look more closely on these two overridden methods: public class Polygon extends Shape{ public void draw(graphics g){ g.setcolor(color_); for(int i = 0; i < (points_.length 1); i++) // here is other code g.drawline(points_[i].getx(), points[i].gety(), points_[i+1].getx(), points_[i+1].gety());

8 public class Circle extends Shape{ public void draw(graphics g){ g.setcolor(color_); g.drawoval(position_.getx(), position_.gety(), dim_.getwidth(), dim_.getheight()); Thus, we see that these two classes provide different implementations for the draw method and therefore a different behavior for the two classes. Another important thing to notice is that the return type, method name, and number and type of the parameters for the overriding method must match those in the overridden method Initialization Another important aspect of the inheritance mechanism is the initialization of objects of a subclass. Basically, an object of a subclass is an instance of both classes: subclass and superclass. Thus, a proper initialization of both objects has to be accomplished. In Java, the default empty constructor of a class is automatically called when an object is created. If a class is a subclass of another class then the Java system automatically invokes first the constructor of the superclass. In the case that programmer wants to define constructors that takes arguments, then he/she has to call the constructor of the superclass by him/herself. Let us look on the following example to demonstrate this: public class Polygon extends Shape{ private Point[] points_; public Polygon(Point[] points, Point position, Dimension dim){ super(position, dim); points_ = points; // here comes the rest The code in bold calls the constructor of the superclass to initialize its instance variables properly. Note that this call has to be the first thing that a subclass constructor executes.

9 1.3 Combining Composition and Inheritance Sometimes we want to create more complex classes that possible combine composition and inheritance mechanism. The following example shows the creation of a more complex class, using both inheritance and composition. Suppose that we want to have a composite shape object, which groups together a number of other shapes. In that way we can treat all shapes object that are put into the grouped object as just one shape object. Obviously, such object is a special shape object, i.e., it can be represented by a subclass of the Shape class and at the same time it is a composite object composed of say an array of other shape objects. The code for such Group class might look as follows: public class Group extends Shape{ private Shape[] shapes_; public Group(Shape[] shapes){ shapes_ = shapes; public void draw(graphics g){ for(int i = 0; i < shapes_.length; i++) shapes_[i].draw(g); The cold in bold from the example above represents inheritance and composition mechanisms.

10 2 Abstract Classes Sometimes, a class that we define represents an abstract concept and, as such, should not be instantiated. Rather it should just serve as a base superclass for a number of more specialized classes. That means a basic abstract class defines just an interface that should be shared among its subclasses. Let us revisit our Shape class example and try to think about it in terms of an abstract class. Basically, we used the Shape class just as a superclass for a number of different subclasses, such as the Circle, Polygon, Group class and so on. We didn t use the Shape class to directly create instances of it. If we think more about it we can conclude that it is not necessary to create instances of the Shape class, because an instance of a Shape class that is not also an instance of a special subclass is of no much use, because such Shape instance is not aware of how to draw itself, for example. It knows that it is a Shape but it doesn t know which Shape it is. Obviously, we could and should (to prevent creation of objects that don t know how to draw themselves, for example) define the Shape class to be an abstract class. Technically, to define an abstract class means that we define a number of its methods to be abstract, i.e., we don t provide the implementation for them but rather leave the subclasses to do so. Obviously, the draw method and for example the erase method of the Shape classes should be defined abstract, because special classes should implement this special behavior themselves. 2.1 Creating Abstract Classes To create an abstract class we must declare it to be abstract and in addition to that we must declare a number of its methods to be abstract. Let us revisit the Shape class from above and define it to be an abstract class: abstract public class Shape{ protected Color color_; protected Point position_; protected Dimension dim_; public Shape(){ public Shape(Point position, Dimension dim){ this(position, dim, new Color(0, 0, 0));

11 public Shape(Point position, Dimension dim, Color color){ position_ = position; dim_ = dim; color_ = color abstract public void draw(graphics g); abstract public void erase(); public Rectangle getbounds(){ return new Rectangle(position_.getX(), position_.gety(), dim_.getwidth(), dim_.getheight()); public void setbounds(rectangle bounds){ position_.setx(bounds.getx()); position_.sety(bounds.gety()); dim_.setwidth(bounds.getwidth()); dim_.setheight(bounds.getheight()); Note the code in bold declaring the Shape class and two methods of it to be abstract. Another important thing to notice here are constructors of the abstract Shape class. Although the Shape class defines a number of constructors we cannot use these constructors directly to create instances of the Shape class. The only way we can use these constructors is from subclasses of the Shape class. Thus, if we have a subclass of the Shape class, say the Polygon class, it can invoke as the first line in its constructor a constructor from the Shape class in order to initialize instance variables from the Shape class. 2.2 Extending Abstract Classes Basically, extending an abstract class is same as extending a normal class. The only difference is that a subclass in order to become a normal class, i.e., a class from which we can create instances has to implement all abstract methods from its abstract superclass. Otherwise this subclass is considered to be an abstract class as well and therefore it is not possible to create its instances.

12 3 Interfaces In the last section we saw in which way we can use and create abstract classes. Let us now think about an abstract class that has all its methods declared to be abstract. Obviously, such abstract class provides just an interface for all of its subclasses. In Java this concept obtained a special keyword: interface. An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior. This class obtains also the type of that interface. Because an interface is simply a list of unimplemented, and therefore abstract methods, what is the difference between an interface and an abstract class? The differences are significant: An interface cannot implement any methods, whereas an abstract class can. A class can implement many interfaces but can have only one superclass. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. 3.1 Creating Interfaces Let us now look on an example of an interface. We already saw how we can use collections to store objects of different types. We also introduced the concept of an iterator, which is an object that we use to traverse through the objects of a particular collection. The iterator object is a typical example of where we would use an interface instead of a class. Basically, an iterator object allows us to go forth and back, for example, between members of a collection. These members are stored within that collection, but an iterator just agrees to traverse behavior. It does not contain the objects from that collection. We could define an iterator interface like this: public interface Iterator{ public Object next(); public boolean hasnext(); Note, the interface keyword in bold. We also see from the above example that an interface just declares a number of methods similarly to an abstract class. The difference here is that we don t need to use the abstract keyword to do so. 3.2 Implementing an Interface If a class chooses to extend an interface we say that this class implements that interface. The principle here is the same as with abstract classes. If a class implements an interface it should implement all of its methods in order not to be an abstract class. Otherwise the class is considered to be an abstract class and therefore we cannot create instances of that class. Let us look on an example of a class that implements the Iterator interface from above:

13 public class ArrayList implements Iterator{ private int cursor_; public boolean hasnext(){ return (cursor_!= size()); public Object next(){ return get(cursor_++); public Iterator iterator(){ cursor_ = 0; return this; // other code comes here In the example above the ArrayList class implements the Iterator interface. The ArrayList class implements all methods declared in the Iterator interface, thus this class is not an abstract class and we can create its instances. There are several important issues of the example above that we have to mention here: ArrayList class implements itself the Iterator interface, therefore the method iterator returns this reference, i.e., the reference of the ArrayList object itself. Recollect that a class implementing an interface has also the type of that interface. Because the ArrayList class implements the Iterator interface, the methods that override that interface might call other methods of the ArrayList directly. For example, the method next calls the method get of the ArrayList class. Another class could be created, say ArrayListIterator, which could be a separate class only implementing the Iterator interface. In that case the iterator method from the ArrayList class would create a separate instance of the ArrayListIterator class and return this instance. 3.3 Multiple Inheritance As we mentioned before a class can extend another class in order to specialize its characteristics and its behavior. In some object-oriented languages a class can extend not only one class but also a number of other classes. This is called multiple inheritance. There are a number of problems with this concept. For instance, suppose a class extends two classes that declare two same methods but with other implementations (e.g. in both classes we have one method with the same name, arguments and return values but with a different implementation). Now, if we call this method on an instance of the subclass, it is not clear to which code to bind it, i.e., should we call this method from the first or from the second superclass.

14 Such and similar issues caused that there is no support for pure multiple inheritance in Java. That means in Java a class can extend just one superclass. However, this might be too restrictive in some cases and therefore Java supports a kind of multiple inheritance in the following way. A class in Java might implement a number of interfaces, thus it inherits from multiple sources. Since interfaces are just declarations of methods and there is no implementation defined by interfaces the problem that we described before does not exist in Java.

15 4 Polymorphism Let us look again on the example of the Group shape class from above. Precisely, let us investigate in more details its draw method: public class Group extends Shape{ public void draw(graphics g){ // for(int i = 0; i < shapes_.length; i++) shapes_[i].draw(g); An instance of the Group class holds an array of instances of other Shape classes, such as circles, polygons, triangles, etc. The draw method of the Group class iterates through all shapes and draws them one by one on the user screen. Lines of the code in bold above show the invocation of the draw method of each particular Shape object. Here we encounter a very important mechanism in object-oriented languages. We call in our code the method of an instance of a base class (the Shape class), but at the run-time the system calls the right method of each particular object, i.e., if it encounters a circle object it calls the draw method of that circle object; if it encounters a polygon object it calls the draw method of the polygon object, etc. As the effect of that shape objects are drawn in the way we expect them to be drawn. This mechanism is called polymorphism. Technically, due to polymorphism objects are interchangeable or substitutable in object-oriented programs. This means we can write code that talks to shapes and automatically handle anything that fits the description of a shape, i.e., handle anything that is a subclass of the Shape class. Let us now examine how object-oriented programming languages implement polymorphism. 4.1 Static Binding In traditional programming languages, e.g. in procedural programming languages such as C, the compiler makes a function call statically. It means the compiler generates a call to a specific function name, and the linker resolves this call to the absolute address of the code to be executed. This principle is called static binding or early binding of a function call. The static binding means that in the case of the above Shape example the compiler would bind the call to the draw method of the Shape class at the compile time, thus at the run-time the draw method of the base Shape class would be called for each particular shape object regardless of its real type. 4.2 Dynamic Binding As we already saw static binding cannot be applied if we want to support polymorphism. Therefore, object-oriented languages support another type of binding function calls. Binding supported by object-oriented languages is called dynamic binding or late binding.

16 Dynamic binding means that when you send a message to an object, the code being called isn t determined until run-time. The compiler does ensure that the function exists and performs type checking on the arguments and return value, but it doesn t know the exact code to execute. To perform late binding, Java uses a special bit of code in lieu of the absolute call. This code calculates the address of the function body, using information stored in the object. Thus, each object can behave differently according to the contents of that special bit of code. When you send a message to an object, the object actually does figure out what to do with that message. 4.3 Interchangeable Objects & Upcasting Polymorphism allows us to interchange (substitute) an object of the type A for an object of the type B, where the type A is a subclass of the type B (no matter at which level of inheritance). Let us look again on the Shape example: Shape[] shapes = new Shape[2]; shapes[0] = new Circle(new Point(100, 100), 50); shapes[1] = new Polygon(new Point(0, 0), 100, 50); Group group = new Group(shapes); First, we notice that we substitute a circle and a polygon object for objects of its base Shape class (e.g., shapes[0] and shapes[1]). We treat instances of inheriting classes as instances of a base class. We call this process of treating a derived type as though it were its base type upcasting. The name cast is used in the sense of casting into a mold and the up comes from the way the inheritance diagram is typically arranged, with the base type at the top and the derived classes fanning out downward. Thus, casting to a base type is moving up the inheritance diagram: upcasting. 4.4 Extensibility Polymorphism allows object-oriented programs to be easily extensible. Imagine that we write the code for another Shape class, say Ellipse class. It is clear that we don t need to change the code for the Group class in order to include ellipse objects in new group objects. Since the Ellipse class is a subclass of the Shape class everything perfectly fits. It doesn t matter that we created the Ellipse class additionally.

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

Software Architecture (Lesson 2) Object-Oriented Paradigm (1) Software Architecture (Lesson 2) Object-Oriented Paradigm (1) Table of Contents Introduction... 2 1.1 Basic Concepts... 2 1.1.1 Objects... 2 1.1.2 Messages... 3 1.1.3 Encapsulation... 4 1.1.4 Classes...

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

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

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

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

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

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

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

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Polymorphism and Interfaces. CGS 3416 Spring 2018

Polymorphism and Interfaces. CGS 3416 Spring 2018 Polymorphism and Interfaces CGS 3416 Spring 2018 Polymorphism and Dynamic Binding If a piece of code is designed to work with an object of type X, it will also work with an object of a class type that

More information

What are the characteristics of Object Oriented programming language?

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

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented

More information

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space. 1 All programming languages provide abstractions. Assembly language is a small abstraction of the underlying machine. Many imperative languages (FORTRAN, BASIC, and C) are abstractions of assembly language.

More information

CST242 Object-Oriented Programming Page 1

CST242 Object-Oriented Programming Page 1 CST4 Object-Oriented Programming Page 4 5 6 67 67 7 8 89 89 9 0 0 0 Objected-Oriented Programming: Objected-Oriented CST4 Programming: CST4 ) Programmers should create systems that ) are easily extensible

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

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

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

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

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

2. The object-oriented paradigm!

2. The object-oriented paradigm! 2. The object-oriented paradigm! Plan for this section:! n Look at things we have to be able to do with a programming language! n Look at Java and how it is done there" Note: I will make a lot of use of

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 (Outsource: )

Inheritance (Outsource: ) (Outsource: 9-12 9-14) is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, inherit attributes and behavior of the pre-existing classes,

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 Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Object Oriented Programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 23, 2010 G. Lipari (Scuola Superiore

More information

First IS-A Relationship: Inheritance

First IS-A Relationship: Inheritance First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class

More information

Programming Exercise 14: Inheritance and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism Programming Exercise 14: Inheritance and Polymorphism Purpose: Gain experience in extending a base class and overriding some of its methods. Background readings from textbook: Liang, Sections 11.1-11.5.

More information

INHERITANCE: EXTENDING CLASSES

INHERITANCE: EXTENDING CLASSES INHERITANCE: EXTENDING CLASSES INTRODUCTION TO CODE REUSE In Object Oriented Programming, code reuse is a central feature. In fact, we can reuse the code written in a class in another class by either of

More information

More Relationships Between Classes

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A. HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,

More information

COURSE 2 DESIGN PATTERNS

COURSE 2 DESIGN PATTERNS COURSE 2 DESIGN PATTERNS CONTENT Fundamental principles of OOP Encapsulation Inheritance Abstractisation Polymorphism [Exception Handling] Fundamental Patterns Inheritance Delegation Interface Abstract

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

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

More information

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

Logistics. Final Exam on Friday at 3pm in CHEM 102 Java Review Logistics Final Exam on Friday at 3pm in CHEM 102 What is a class? A class is primarily a description of objects, or instances, of that class A class contains one or more constructors to create

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

Introduction to Design Patterns

Introduction to Design Patterns Introduction to Design Patterns First, what s a design pattern? a general reusable solution to a commonly occurring problem within a given context in software design It s not a finished design that can

More information

Data Structures and Other Objects Using C++

Data Structures and Other Objects Using C++ Inheritance Chapter 14 discuss Derived classes, Inheritance, and Polymorphism Inheritance Basics Inheritance Details Data Structures and Other Objects Using C++ Polymorphism Virtual Functions Inheritance

More information

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;

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

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

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

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

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created. + Inheritance + Inheritance Classes that we design in Java can be used to model some concept in our program. For example: Pokemon a = new Pokemon(); Pokemon b = new Pokemon() Sometimes we need to create

More information

Inheritance & Polymorphism

Inheritance & Polymorphism E H U N I V E R S I T Y T O H F R G E D I N B U Murray Cole Classifying Things 1 Hierarchies help us to classify things and understand their similarities and differences Some aspects are common to everything

More information

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System? Page 1 of 21 Page 2 of 21 and identity. Objects are members of a class, and the attributes and behavior of an object are defined by the class definition. The Essence of Object Oriented Programming with

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Polymorphism and Inheritance

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

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

Practice for Chapter 11

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

Chapter 11 Classes Continued

Chapter 11 Classes Continued Chapter 11 Classes Continued The real power of object-oriented programming comes from its capacity to reduce code and to distribute responsibilities for such things as error handling in a software system.

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

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

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.) Inheritance Inheritance (cont.) Object oriented systems allow new classes to be defined in terms of a previously defined class. All variables and methods of the previously defined class, called superclass,

More information

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int) Inheritance Object OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int) PrintStream write(int) write(byte[]) Early binding Consider

More information

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Comp 249 Programming Methodology Chapter 8 - Polymorphism Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified

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

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

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

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

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1 Design Principles Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques 2-1 Data Structures Data Structure - A systematic way of organizing and accessing

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017 Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data

More information

Inheritance, polymorphism, interfaces

Inheritance, polymorphism, interfaces Inheritance, polymorphism, interfaces "is-a" relationship Similar things (sharing same set of attributes / operations): a group / concept Similar groups (sharing a subset of attributes / operations): a

More information

Compaq Interview Questions And Answers

Compaq Interview Questions And Answers Part A: Q1. What are the difference between java and C++? Java adopts byte code whereas C++ does not C++ supports destructor whereas java does not support. Multiple inheritance possible in C++ but not

More information

index.pdf January 21,

index.pdf January 21, index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version

More information

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 10 Classes Continued. Fundamentals of Java Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an

More information

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

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages CSE 307: Principles of Programming Languages Classes and Inheritance R. Sekar 1 / 52 Topics 1. OOP Introduction 2. Type & Subtype 3. Inheritance 4. Overloading and Overriding 2 / 52 Section 1 OOP Introduction

More information

Advanced Placement Computer Science. Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,

More information

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson January 13, 2005 January 18, 2005 1 of 38 Lecture Goals Introduce the basic concepts of object-oriented analysis/design/programming

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship The "is-a" Relationship Object classification

More information

Lecture Notes on Programming Languages

Lecture Notes on Programming Languages Lecture Notes on Programming Languages 85 Lecture 09: Support for Object-Oriented Programming This lecture discusses how programming languages support object-oriented programming. Topics to be covered

More information

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

More About Objects. Zheng-Liang Lu Java Programming 255 / 282 More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism

More information

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

Inheritance. CSE 142, Summer 2002 Computer Programming 1. Inheritance CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 29-July-2002 cse142-14-inheritance 2002 University of Washington 1 Reading Readings and

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 (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

Self-review Questions

Self-review Questions 7Class Relationships 106 Chapter 7: Class Relationships Self-review Questions 7.1 How is association between classes implemented? An association between two classes is realized as a link between instance

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship Object classification is typically hierarchical.

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 (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Object-Oriented Programming Paradigm

Object-Oriented Programming Paradigm Object-Oriented Programming Paradigm Sample Courseware Object-Oriented Programming Paradigm Object-oriented programming approach allows programmers to write computer programs by representing elements of

More information

Inheritance and object compatibility

Inheritance and object compatibility Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can

More information