Polymorphism. Polymorphism. CSC 330 Object Oriented Programming. What is Polymorphism? Why polymorphism? Class-Object to Base-Class.

Size: px
Start display at page:

Download "Polymorphism. Polymorphism. CSC 330 Object Oriented Programming. What is Polymorphism? Why polymorphism? Class-Object to Base-Class."

Transcription

1 Polymorphism CSC 0 Object Oriented Programming Polymorphism is considered to be a requirement of any true -oriented programming language (OOPL). Reminder: What are the other two essential elements in OOPL? Polymorphism 2 What is Polymorphism? Polymorphism refers to a programming language s ability to process s differently depending on their data type or class. Why polymorphism? With polymorphism, one method can cause different actions to occur, depending on the type of the on which the method is invoked. Number person Me real complex kid adult Teacher Mom Derived-Class Class-Object to Base-Class Class-Object Conversion Class hierarchies Can assign derived-class s to base-class references - A fundamental part of programs that process s polymorphically Can explicitly cast between types in a class hierarchy An of a derived-class can be treated as an of its base-class (The reverse is NOT true, i.e.: base-class is NOT an of any of its derived classes) Can have arrays of base-class references that refer to s of many derived-class types 1 // Point.cs 2 // Point class represents an x-y coordinate Definition pair. of class Point using System; 6 // Point class definition implicitly inherits from Object 7 public class Point // point coordinate private int x, y; // default constructor 1 public Point() 1 { 1 // implicit call to Object constructor occurs here } // constructor 1 public Point( int xvalue, int yvalue ) 20 { 21 // implicit call to Object constructor occurs here X = xvalue; 2 Y = yvalue; 2 } 2 26 // property X 27 public int X 2 2 get 0 { 1 return x; 2 } Point.cs 6 1

2 set { 6 x = value; // no need for validation 7 } 8 } // end property X 0 1 // property Y 2 public int Y { get { 6 return y; 7 } 8 set 0 { 1 y = value; // no need for validation 2 } } // end property Y 6 // return string representation of Point 7 public override string ToString() return "[" + X + ", " + Y + "]"; 60 } } // end class Point Point.cs 7 1 // Circle.cs 2 // Circle class that inherits from Definition class Point. of class Circle which inherits from class Point using System; 6 // Circle class definition inherits from Point 7 public class Circle : Point private double radius; // circle's radius 11 // default constructor 12 public Circle() 1 { 1 // implicit call to Point constructor occurs here 1 } 17 // constructor 18 public Circle( int xvalue, int yvalue, double radiusvalue ) 1 : base( xvalue, yvalue ) 20 { 21 Radius = radiusvalue; } 2 2 // property Radius 2 public double Radius 26 { 27 get 2 2 return radius; 0 } 1 Circle.cs 8 2 set { if ( value >= 0 ) // validate radius radius = value; 7 8 } // end property Radius 0 // calculate Circle diameter 1 public double Diameter() 2 { return Radius * 2; } 6 // calculate Circle circumference 7 public double Circumference() return Math.PI * Diameter(); 0 } 1 2 // calculate Circle area public virtual double Area() { return Math.PI * Math.Pow( Radius, 2 ); 6 } 7 8 // return string representation of Circle public override string ToString() 60 { 61 return "Center = " + base.tostring() + 62 "; Radius = " + Radius; 6 } 6 6 } // end class Circle Circle.cs 1 // PointCircleTest.cs 2 // Demonstrating inheritance and polymorphism. using System; 8 class PointCircleTest circle1 (derived-class using System.Windows.Forms; Create a Point 6 7 // PointCircleTest class definition Assign Circle Create a Circle PointCircleTest. cs { ) to Point // main entry point for application. Use base-class reference to 11 static void Main( string[] args ) Casts point2 ( downcasts ) reference (baseclass point2 reference) (which - Point access a derived-class 1 Point point1 = new Point( 0, 0 ); references OK since a Circleis-a 1 Circle circle1 = new Circle( 120, 8, 2.7 ); 1 circle1) Point to a Circle string output = "Point point1: " + point1.tostring() and then assigns the + result 17 "\ncircle circle1: " + circle1.tostring(); to the Circle reference 18 1 // use 'is a' relationship to assign circle2. (Cast would be 20 // Circle circle1 to Point reference dangerous if point2 were 21 Point point2 = circle1; referencing a Point! It is 2 output += "\n\nccircle circle1 (via point2): OK since " point2 + 2 point2.tostring(); // Circle s ToString references called, a Circle) not Point s // ToString, since point2 references now a Circle (circle1). // (Object type determines method version, not reference type.) 2 26 // downcast (cast base-class reference to derived-class 27 // data type) point2 to Circle circle2 28 Circle circle2 = ( Circle ) point2; 2 0 output += "\n\ncircle circle1 (via circle2 [and point2]): " + 1 circle2.tostring(); 2 output += "\narea of circle1 (via circle2): " + circle2.area().tostring( "F" ); 11 6 // attempt to assign point1 to Circle reference 7 if ( point1 is Circle ) circle2 = ( Circle ) point1; // Incorrect! Objects may be // cast only to their own type or their base-class types. PointCircleTest. // Here, point1 s own type is Point, and Circle cs // is not a base-class type for Point. output += "\n\ncast successful"; 0 1 } 2 else { output += "\n\npoint1 does not refer to a Circle"; } 6 7 MessageBox.Show( output, Test if point1 references a Circle 8 "Demonstrating the 'is a' relationship" ); it does not since point1 references a Point 0 } // end method Main 1 2 } // end class PointCircleTest Abstract Classes and Methods Concrete classes use the keyword override to provide implementations for all the abstract methods and properties of the base- class Any class with an abstract method or property must be declared abstract Even though abstract classes cannot be instantiated, we can use abstract class references to refer to instances of any concrete class derived from the abstract class 2

3 Case Study: Inheriting from an Abstract Class & Implementation Abstract base class Shape Concretevirtual method Area (default return value is 0) Concretevirtual method Volume (default return value is 0) Abstract read-only property Name Class Point2 inherits from Shape Overrides abstract property Name (required) Does NOT override methods Area and Volume Area = 0 and Volume =0 are correct for a point Class Circle2 inherits from Point2 Overrides property Name Overrides method Area, but not Volume Volume =0 is correct for a circle Class Cylinder2 inherits from Circle2 Overrides property Name Overrides methods Area and Volume 1 implicit inheritance explicit inheritance Object (namespace: System) abstract class Shape Concrete virtual method Area Concrete virtual method Volume Abstract read-only property Name class Point2 Overrides abstract property Name (required) Does NOT override methods Area and Volume (Default return values Area = 0 & Volume =0 are correct for a point) class Circle2 Overrides property Name Overrides method Area, but not Volume (Volume =0 is correct for a circle) class Cylinder2 Overrides property Name Overrides methods Area and Volume // Point2.cs 2 // Point2 inherits from abstract class Shape and represents // an x-y coordinate pair. using System; 6 // Point2 inherits from abstract class Shape 7 public class Point2 : Shape private int x, y; // Point2 coordinates 11 // default constructor 12 public Point2() 1 { 1 // implicit call to Object constructor occurs here 1 } 17 // constructor 18 public Point2( int xvalue, int yvalue ) 20 X = xvalue; 21 Y = yvalue; } Class Point2 inherits from class Shape 2 2 // property X 2 public int X 26 { 27 get 2 2 return x; 0 } 1 2 set { x = value; needed // no validation } Point2.cs 7 8 // property Y public int Y Point2 s implementation of the 0 { 1 get read-only Name property 2 { return y; } 6 set 8 y = value; validation needed // no } 0 } 1 2 // return string representation of Point2 public override string ToString() { return "[" + X + ", " + Y + "]"; 6 } 7 8 // implement abstract property Name of class Shape public override string Name 60 { 61 get 62 { 6 return "Point2"; 6 } 6 } } // end class Point2 Point2.cs 17 1 // Circle2.cs 2 // Circle2 inherits from class Point2 and overrides key members. using System; // Circle2 inherits from class Point2 6 public class Circle2 : Point2 8 private double radius; // Circle2 radius // default constructor 11 public Circle2() 1 // implicit call to Point2 constructor occurs here 1 } 1 // constructor 17 public Circle2( int xvalue, int yvalue, double radiusvalue ) 18 : base( xvalue, yvalue ) 20 Radius = radiusvalue; 21 } Definition of class Circle2 which 2 // property Radius inherits from class Point2 2 public double Radius 2 { 26 get 2 28 return radius; 2 } 0 Circle2.cs 18

4 1 set 2 { // ensure non-negative radius value if ( value >= 0 ) radius = value; Override the Area method 7 } 8 (defined in class Shape) // calculate Circle2 diameter 0 public double Diameter() 1 { 2 return Radius * 2; } // calculate Circle2 circumference 6 public double Circumference() 8 return Math.PI * Diameter(); } 0 1 // calculate Circle2 area 2 public override double Area() { return Math.PI * Math.Pow( Radius, 2 ); } 6 7 // return string representation of Circle2 8 public override string ToString() { 60 return "Center = " + base.tostring() + 61 "; Radius = " + Radius; 62 } Circle2.cs // override property Name from class Point2 6 public override string Name 66 { 67 get 6 6 return "Circle2"; 70 } 71 } 72 7 } // end class Circle2 Override the read-only Name property Circle2.cs 20 1 // Cylinder2.cs 2 // Cylinder2 inherits from class Circle2 and overrides key members. using System; // Cylinder2 inherits from class Circle2 6 public class Cylinder2 : Circle2 8 private double height; // Cylinder2 height // default constructor 11 public Cylinder2() 1 // implicit call to Circle2 constructor occurs here 1 } 1 // constructor 17 public Cylinder2( int xvalue, int yvalue, double radiusvalue, 18 double heightvalue ) : base( xvalue, yvalue, radiusvalue ) 20 Height = heightvalue; 21 } 2 // property Height 2 public double Height Class Cylinder2 2 { 26 get derives from Circle return height; 2 } 0 1 set 2 { // ensure non-negative height value if ( value >= 0 ) height = value; Cylinder2.cs 21 Override read-only property Name Cylinder2.cs 7 } 8 // calculate Cylinder2 area 0 public override double Area() 1 { 2 return 2 * base.area() + base.circumference() * Height; } // calculate Cylinder2 volume 6 public override double Volume() 8 return base.area() * Height; } 0 1 // return string representation of Circle2 2 public override string ToString() { return base.tostring() + "; Height = " + Height; } 6 7 // override property Name from class Circle2 8 public override string Name { 60 get Override Area implementation of class Circle2 61 { 62 return "Cylinder2"; 6 } 6 } Override Volume implementation of class Shape 6 66 } // end class Cylinder2 2 1 // AbstractShapesTest.cs 2 // Demonstrates polymorphism in Point-Circle-Cylinder hierarchy. using System; using System.Windows.Forms; Assign a Shape reference AbstractShapesTe 6 public class AbstractShapesTest to reference Create an a array Point2 of Shape s st.cs Assign a Shape reference to 8 public static void Main( string[] args ) reference Assign a a Shape Cylinder2 reference to { Cylinder2 reference s a Circle2 // instantiate Point2, Circle2 and 11 Point2 point = new Point2( 7, 11 ); 12 Circle2 circle = new Circle2(, 8,. ); 1 Cylinder2 cylinder = new Cylinder2(,,., ); 1 1 // create empty array of Shape base-class references Shape[] arrayofshapes = new Shape[ ]; // arrayofshapes[ 0 ] refers to Point2 1 arrayofshapes[ 0 ] = point; // arrayofshapes[ 1 ] refers to Circle2 arrayofshapes[ 1 ] = circle; 2 2 // arrayofshapes[ 1 ] refers to Cylinder2 2 arrayofshapes[ 2 ] = cylinder; string output = point.name + ": " + point + "\n" + 28 circle.name + ": " + circle + "\n" + 2 cylinder.name + ": " + cylinder; 0 1 // display Name, Area and Volume for each 2 // in arrayofshapes polymorphically foreach( Shape shape in arrayofshapes ) { output += "\n\n" + shape.name + ": " + shape + 6 "\narea = " + shape.area().tostring( "F" ) + 7 "\nvolume = " + shape.volume().tostring( "F" ); 8 } 0 MessageBox.Show( output, "Demonstrating Polymorphism" ); 1 } 2 } Use a foreach loop to access every element of the array Rely on polymorphism to call appropriate version of methods 2 AbstractShapesTe st.cs

5 sealed Methods and Classes sealed is a keyword in C# sealed methods and sealed classes 1)sealed methods cannot be overridden in a derived class Methods that are declared static or private are implicitly sealed 2)sealed classes cannot have any derived-classes Creating sealed classes can allow some runtime optimizations e.g., virtual method calls can be transformed into non-virtual method calls 2 Case Study: Creating and Using Interfaces Interfaces are defined using keyword interface Use inheritance notation to specify a class implements an interface (ClassName : InterfaceName) Classes may implement more then one interface (a comma separated list of interfaces) Classes that implement an interface, must provide implementations for every method and property in the interface definition // IShape.cs 2 // Interface IShape for Point, Circle, Cylinder Hierarchy. public interface IShape IShape.cs { 6 // classes that implement IShape must implement these methods 7 // and this property 8 double Area(); double Volume(); string Name { get; } Definition of IShape interface 11 } Classes implementing the interface must define methods Area and Volume (each of which take no arguments and return a double) and a read-only string property Name 27 1 // Point.cs 2 // Point implements interface IShape and represents // an x-y coordinate pair. using System; 6 // Point implements IShape interface 7 public class Point : IShape private int x, y; // Point coordinates 11 // default constructor 12 public Point() 1 { 1 // implicit call to Object constructor occurs here 1 } 17 // constructor 18 public Point( int xvalue, int yvalue ) 20 X = xvalue; 21 Y = yvalue; } 2 Class Point implements 2 // property X the IShape interface 2 public int X 26 { 27 get 2 2 return x; 0 } 1 Point.cs 28 2 set { x = value; } 7 Implementation of IShape method Area 8 // property Y public int Y (required), declared virtual to allow 0 { deriving classes to override 1 get 2 { return y; } 6 set 8 y = value; } 0 } 1 2 // return string representation of Point public override string ToString() { return "[" + X + ", " + Y + "]"; 6 } 7 8 // implement interface IShape method Area public virtual double Area() 60 { 61 return 0; 62 } 6 Point.cs 2 6 // implement interface IShape method Volume 6 public virtual double Volume() 66 { 67 return 0; Point.cs 68 } 6 70 // implement property Name of IShape interface 71 public virtual string Name 72 { 7 get Implementation of IShape method 7 return "Point"; Volume (required), declared virtual to 76 } 77 } allow deriving classes to override 78 7 } // end class Point Implementation of IShape property Name (required), declared virtual to allow deriving classes to override 0

6 1 // Circle.cs 2 // Circle inherits from class Point and overrides key members. using System; // Circle inherits from class Point 6 public class Circle : Point 8 private double radius; // Circle radius // default constructor 11 public Circle() 1 // implicit call to Point constructor occurs here 1 } 1 // constructor 17 public Circle( int xvalue, int yvalue, double radiusvalue ) 18 : base( xvalue, yvalue ) 20 Radius = radiusvalue; 21 } 2 // property Radius Definition of class Circle 2 public double Radius 2 { which inherits from Point 26 get 2 28 return radius; 2 } 0 Circle.cs 1 1 set 2 { // ensure non-negative Radius value if ( value >= 0 ) radius = value; 7 } 8 Override the Point // calculate Circle diameter 0 public double Diameter() implementation of Area 1 { 2 return Radius * 2; } // calculate Circle circumference 6 public double Circumference() 8 return Math.PI * Diameter(); } 0 1 // calculate Circle area 2 public override double Area() { return Math.PI * Math.Pow( Radius, 2 ); } 6 7 // return string representation of Circle 8 public override string ToString() { 60 return "Center = " + base.tostring() + 61 "; Radius = " + Radius; 62 } 6 Circle.cs 2 6 // override property Name from class Point 6 public override string Name 66 { 67 get 6 6 return "Circle"; 70 } 71 } 72 7 } // end class Circle Override the Point implementation of Name Circle.cs 1 // Cylinder.cs 2 // Cylinder inherits from class Circle2 and overrides key members. using System; // Cylinder inherits from class Circle 6 public class Cylinder : Circle 8 private double height; // Cylinder height // default constructor 11 public Cylinder() 1 // implicit call to Circle constructor occurs here 1 } 1 // constructor 17 public Cylinder( int xvalue, int yvalue, double radiusvalue, 18 double heightvalue ) : base( xvalue, yvalue, radiusvalue ) 20 Height = heightvalue; 21 } Declaration of class Cylinder 2 // property Height which inherits from class Circle 2 public double Height 2 { 26 get 2 28 return height; 2 } 0 Cylinder.cs 1 set 2 { Override the Point // ensure non-negative Height value implementation of Volume if ( value >= 0 ) height = value; Override the Circle implementation of Area 7 } 8 // calculate Cylinder area 0 public override double Area() 1 { 2 return 2 * base.area() + base.circumference() * Height; } // calculate Cylinder volume 6 public override double Volume() 8 return base.area() * Height; } 0 1 // return string representation of Cylinder 2 public override string ToString() { return "Center = " + base.tostring() + "; Height = " + Height; 6 } 7 Cylinder.cs 8 // override property Name from class Circle public override string Name 60 { 61 get 62 { 6 return "Cylinder"; 6 } 6 } 66 Override the Circle 67 } // end class Cylinder implementation of Name Cylinder.cs 6 6

7 7 1 // Interfaces2Test.cs 2 // Demonstrating polymorphism with interfaces in // Point-Circle-Cylinder hierarchy. Create an array of using System.Windows.Forms; Interfaces2Test. IShape references 6 cs (IShape is an 7 public class Interfaces2Test interface) Assign an IShape reference public static void Main( string[] args ) to reference a Point { 11 // instantiate Point, Circle and Cylinder s Assign an IShape reference 12 Point point = new Point( 7, 11 ); 1 Circle circle = new Circle(, 8,. ); to reference a Circle 1 Cylinder cylinder = new Cylinder(,,., ); Assign an IShape reference to 1 // create array of IShape references reference a Cylinder 17 IShape[] arrayofshapes = new IShape[ ]; 18 1 // arrayofshapes[ 0 ] references Point 20 arrayofshapes[ 0 ] = point; 21 // arrayofshapes[ 1 ] references Circle 2 arrayofshapes[ 1 ] = circle; 2 2 // arrayofshapes[ 2 ] references Cylinder 26 arrayofshapes[ 2 ] = cylinder; string output = point.name + ": " + point + "\n" + 2 circle.name + ": " + circle + "\n" + 0 cylinder.name + ": " + cylinder; 1 2 foreach ( IShape shape in arrayofshapes ) { output += "\n\n" + shape.name + ":\narea = " + shape.area() + "\nvolume = " + shape.volume(); 7 8 MessageBox.Show( output, "Demonstrating Polymorphism" ); } 0 } Use polymorphism to call the appropriate class s method or property 8 Interfaces2Test. cs Operator Overloading They are in the form: public static ReturnType operator operator-to-be-overloaded( arguments ) These methods must be declared public and static The return type is the type returned as the result of evaluating the operation The keyword operator follows the return type to specify that this method defines an operator overload The last piece of information is the operator to be overloaded (such as +, -, *, etc.) If the operator is unary, one argument must be specified, if the operator is binary, then two, etc. 0 1 // ComplexNumber.cs 2 // Class that overloads operators for adding, subtracting // and multiplying complex numbers. public class ComplexNumber ComplexNumber.cs 6 { 7 private int real; 8 private int imaginary; Property Real provides access to the // default constructor real part of the complex number 11 public ComplexNumber() {} 12 1 // constructor 1 public ComplexNumber( int a, int b ) 1 { Real = a; Class ComplexNumber definition 17 Imaginary = b; 18 } 1 20 // return string representation of ComplexNumber 21 public override string ToString() { 2 return "( " + real + 2 ( imaginary < 0? " - " + ( imaginary * -1 ) : 2 " + " + imaginary ) + "i )"; // conditional operator (?:) 26 } // property Real 2 public int Real 0 { 1 get 2 { return real; } 6 set 8 real = value; } 0 Overload the addition (+) operator 1 } // end property Real 2 for ComplexNumbers. // property Imaginary public int Imaginary { 6 get 8 return imaginary; } 0 1 set 2 { Property Imaginary provides imaginary = value; } access to the imaginary part 6 } // end property Imaginary of a complex number 7 8 // overload the addition operator public static ComplexNumber operator + ( 60 ComplexNumber x, ComplexNumber y ) 61 { 62 return new ComplexNumber( 6 x.real + y.real, x.imaginary + y.imaginary ); 6 } // non-overloaded + (for int s) is used above twice 6 // since x.real, y.real, x.imaginary, y.imaginary are all // int s - see Lines 7-8 & ComplexNumber.cs 2 66 // provide alternative to overloaded + operator 67 // for addition 68 public static ComplexNumber Add( ComplexNumber x, 6 ComplexNumber y ) 70 { ComplexNumber.cs 71 return x + y; // overloaded + Method (just Subtract defined in provides lines -6 an in 72 7 } // this class) used to add 2 ComplexNumbers x & y alternative to the subtraction operator 7 // subtraction operator overload the 7 public static ComplexNumber operator - ( Overloads the multiplication (*) ComplexNumber x, ComplexNumber y ) { operator for ComplexNumbers 78 return new ComplexNumber( 7 x.real - y.real, x.imaginary - y.imaginary ); 80 } // non-overloaded - (for int s) is used above twice // since x.real, y.real, x.imaginary, y.imaginary are all // int s - see Lines 7-8 & // provide alternative to overloaded - operator 8 // for subtraction 8 public static ComplexNumber Subtract( ComplexNumber x, 8 ComplexNumber y ) 86 { 87 return x - y; // overloaded - Method (just def. Add in provides lines 7-80 an in this 88 } // class) used to subtract ComplexNumber alternative to y the from addition ComplexNumber operator x 0 8 // overload the multiplication operatoroverload the subtraction (-) 1 public static ComplexNumber operator * ( operator for ComplexNumbers 2 ComplexNumber x, ComplexNumber y ) { return new ComplexNumber( x.real * y.real - x.imaginary * y.imaginary, 6 x.real * y.imaginary + y.real * x.imaginary ); 7 } // non-overloaded *, - and + (for int s) are used above // since x.real, y.real, x.imaginary, y.imaginary are all // int s - see Lines 7-8 &

8 8 // provide alternative to overloaded * operator 0 // for multiplication 1 public static ComplexNumber Multiply( ComplexNumber x, 2 ComplexNumber y ) { return x * y; // overloaded * (just def. in lines 1-7 in this } // class) used to multiply 2 ComplexNumbers x & y 6 7 } // end class ComplexNumber Note: Method Multiply provides an alternative to the multiplication operator ComplexNumber.cs 1) The alternative methods: public static ComplexNumber Add ( ComplexNumber x, ComplexNumber y ) public static ComplexNumber Subtract ( ComplexNumber x, ComplexNumber y ) public static ComplexNumber Multiply( ComplexNumber x, ComplexNumber y ) 1 // OperatorOverloading.cs 2 // An example that uses operator overloading using System; using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; using System.Data; 11 public class ComplexTest : System.Windows.Forms.Form 1 private System.Windows.Forms.Label reallabel; 1 private System.Windows.Forms.Label imaginarylabel; 1 private System.Windows.Forms.Label statuslabel; 17 private System.Windows.Forms.TextBox realtextbox; 18 private System.Windows.Forms.TextBox imaginarytextbox; 1 20 private System.Windows.Forms.Button firstbutton; 21 private System.Windows.Forms.Button secondbutton; private System.Windows.Forms.Button addbutton; 2 private System.Windows.Forms.Button subtractbutton; 2 private System.Windows.Forms.Button multiplybutton; 2 26 private ComplexNumber x = new ComplexNumber(); 27 private ComplexNumber y = new ComplexNumber(); 28 2 [STAThread] 0 static void Main() 1 { 2 Application.Run( new ComplexTest() ); } OperatorOverload ing.cs private void firstbutton_click( 6 sender, System.EventArgs e ) 8 x.real = Int2.Parse( realtextbox.text ); x.imaginary = Int2.Parse( imaginarytextbox.text ); OperatorOverload 0 realtextbox.clear(); ing.cs 1 imaginarytextbox.clear(); 2 statuslabel.text = "First Complex Number is: " + x; } Use overloaded addition operator to add two ComplexNumbers private void secondbutton_click( 6 sender, System.EventArgs e ) 8 y.real = Int2.Parse( realtextbox.text ); y.imaginary = Int2.Parse( imaginarytextbox.text ); 0 realtextbox.clear(); 1 imaginarytextbox.clear(); Use overloaded subtraction operator 2 statuslabel.text = "Second Complex Number is: " + y; to subtract two ComplexNumbers } // add complex numbers 6 private void addbutton_click( sender, System.EventArgs e ) 8 statuslabel.text = x + " + " + y + " = " + ( x + y ); } // subtract complex numbers 62 private void subtractbutton_click( 6 sender, System.EventArgs e ) 6 { 6 statuslabel.text = x + " - " + y + " = " + ( x - y ); 66 } // multiply complex numbers 6 private void multiplybutton_click( 70 sender, System.EventArgs e ) { statuslabel.text = x + " * " + y + " = " + ( x * y ); OperatorOverload 7 } ing.cs 7 7 } // end class ComplexTest Use overloaded multiplication operator to multiply two ComplexNumbers 7 OperatorOverload ing.cs 8

CSC330 Object Oriented Programming. Inheritance

CSC330 Object Oriented Programming. Inheritance CSC330 Object Oriented Programming Inheritance Software Engineering with Inheritance Can customize derived classes to meet needs by: Creating new member variables Creating new methods Override base-class

More information

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I CSC0 Object Oriented Programming Inheritance Software Engineering with Inheritance Can customize derived classes to meet needs by: Creating new member variables Creating new methods Override base-class

More information

Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 9 - Object-Oriented Programming: Polymorphism Chapter 9 - Object-Oriented Programming: Polymorphism Polymorphism Program in the general Introduction Treat objects in same class hierarchy as if all superclass Abstract class Common functionality Makes

More information

CSC 330 Object-Oriented Programming. Encapsulation

CSC 330 Object-Oriented Programming. Encapsulation CSC 330 Object-Oriented Programming Encapsulation Implementing Data Encapsulation using Properties Use C# properties to provide access to data safely data members should be declared private, with public

More information

Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic Outline 6.1 Test-Driving the Enhanced Inventory Application 6.2 Variables 6.3 Handling the TextChanged

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclasses and Subclasses 9.5 Case Study: Three-Level

More information

Building non-windows applications (programs that only output to the command line and contain no GUI components).

Building non-windows applications (programs that only output to the command line and contain no GUI components). C# and.net (1) Acknowledgements and copyrights: these slides are a result of combination of notes and slides with contributions from: Michael Kiffer, Arthur Bernstein, Philip Lewis, Hanspeter Mφssenbφck,

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Tutorial 5 Completing the Inventory Application Introducing Programming

Tutorial 5 Completing the Inventory Application Introducing Programming 1 Tutorial 5 Completing the Inventory Application Introducing Programming Outline 5.1 Test-Driving the Inventory Application 5.2 Introduction to C# Code 5.3 Inserting an Event Handler 5.4 Performing a

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

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects 1 Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline 19.1 Test-Driving the Microwave Oven Application 19.2 Designing the Microwave Oven Application 19.3 Adding a New

More information

Part 11 Object Oriented Programming: Inheritance

Part 11 Object Oriented Programming: Inheritance 1 Part 11 Object Oriented Programming: Inheritance 2003 Prentice Hall, Inc. All rights reserved. Main concepts to be covered 2 Superclasses and Subclasses protected Members Relationship between Superclasses

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

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

Classes in C# namespace classtest { public class myclass { public myclass() { } } }

Classes in C# namespace classtest { public class myclass { public myclass() { } } } Classes in C# A class is of similar function to our previously used Active X components. The difference between the two is the components are registered with windows and can be shared by different applications,

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

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

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;

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

Arrays. Arrays: Declaration and Instantiation. Array: An Array of Simple Values

Arrays. Arrays: Declaration and Instantiation. Array: An Array of Simple Values What Are Arrays? CSC 0 Object Oriented Programming Arrays An array is a collection variable Holds multiple values instead of a single value An array can hold values of any type Both objects (reference)

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More 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

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

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

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

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition 11 Features Advanced Object-Oriented Programming C# Programming: From Problem Analysis to Program Design C# Programming: From Problem Analysis to Program Design 1 4th Edition Chapter Objectives 2 Chapter

More information

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

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

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

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

Polymorphism (Deitel chapter 10) (Old versions: chapter 9)

Polymorphism (Deitel chapter 10) (Old versions: chapter 9) Polymorphism (Deitel chapter 10) (Old versions: chapter 9) 1 2 Plan Introduction Relationships Among Objects in an Inheritance Hierarchy Polymorphism Examples Abstract Classes and Methods Example: Inheriting

More information

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case Lesson11-Inheritance-Abstract-Classes The GeometricObject case GeometricObject class public abstract class GeometricObject private string color = "White"; private DateTime datecreated = new DateTime(2017,

More information

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods. Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods

More information

Framework Fundamentals

Framework Fundamentals Questions Framework Fundamentals 1. Which of the following are value types? (Choose all that apply.) A. Decimal B. String C. System.Drawing.Point D. Integer 2. Which is the correct declaration for a nullable

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

An array can hold values of any type. The entire collection shares a single name

An array can hold values of any type. The entire collection shares a single name CSC 330 Object Oriented Programming Arrays What Are Arrays? An array is a collection variable Holds multiple values instead of a single value An array can hold values of any type Both objects (reference)

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

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

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

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

Inheriting Windows Forms with Visual C#.NET

Inheriting Windows Forms with Visual C#.NET Inheriting Windows Forms with Visual C#.NET Overview In order to understand the power of OOP, consider, for example, form inheritance, a new feature of.net that lets you create a base form that becomes

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

Chapter 11: Inheritance

Chapter 11: Inheritance Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved. Reading

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

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

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

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

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

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

EECS 1001 and EECS 1030M, lab 01 conflict

EECS 1001 and EECS 1030M, lab 01 conflict EECS 1001 and EECS 1030M, lab 01 conflict Those students who are taking EECS 1001 and who are enrolled in lab 01 of EECS 1030M should switch to lab 02. If you need my help with switching lab sections,

More information

25. Generic Programming

25. Generic Programming 25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

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

Chapter 20 - C++ Virtual Functions and Polymorphism

Chapter 20 - C++ Virtual Functions and Polymorphism Chapter 20 - C++ Virtual Functions and Polymorphism Outline 20.1 Introduction 20.2 Type Fields and switch Statements 20.3 Virtual Functions 20.4 Abstract Base Classes and Concrete Classes 20.5 Polymorphism

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University (12-1) OOP: Polymorphism in C++ D & D Chapter 12 Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University Key Concepts Polymorphism virtual functions Virtual function tables

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

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

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

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

Chapter 10 Object-Oriented Programming

Chapter 10 Object-Oriented Programming Chapter 10 Object-Oriented Programming Software Reuse and Independence Objects, Classes, and Methods Inheritance and Dynamic Binding Language Examples: Java, C++, Smalltalk Design Issues and Implementation

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

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

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

C# and.net (1) cont d

C# and.net (1) cont d C# and.net (1) cont d Acknowledgements and copyrights: these slides are a result of combination of notes and slides with contributions from: Michael Kiffer, Arthur Bernstein, Philip Lewis, Hanspeter Mφssenbφck,

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

Introductory Programming Inheritance, sections

Introductory Programming Inheritance, sections Introductory Programming Inheritance, sections 7.0-7.4 Anne Haxthausen, IMM, DTU 1. Class hierarchies: superclasses and subclasses (sections 7.0, 7.2) 2. The Object class: is automatically superclass for

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

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

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(b): Abstract classes & Polymorphism Lecture Contents 2 Abstract base classes Concrete classes Polymorphic processing Dr. Amal Khalifa,

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

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3: PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java

More information

Web Services in.net (2)

Web Services in.net (2) Web Services in.net (2) These slides are meant to be for teaching purposes only and only for the students that are registered in CSE4413 and should not be published as a book or in any form of commercial

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

Chapter 10: Inheritance

Chapter 10: Inheritance Chapter 10: Inheritance Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 10 discusses the following main topics: What Is Inheritance?

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

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object-Oriented Programming (OOP) Fundamental Principles of OOP Object-Oriented Programming (OOP) O b j e c t O r i e n t e d P r o g r a m m i n g 1 Object-oriented programming is the successor of procedural programming. The problem with procedural programming is

More information

Flag Quiz Application

Flag Quiz Application T U T O R I A L 17 Objectives In this tutorial, you will learn to: Create and initialize arrays. Store information in an array. Refer to individual elements of an array. Sort arrays. Use ComboBoxes to

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

Advanced C++ Topics. Alexander Warg, 2017

Advanced C++ Topics. Alexander Warg, 2017 www.kernkonzept.com Advanced C++ Topics Alexander Warg, 2017 M I C R O K E R N E L M A D E I N G E R M A N Y Overview WHAT IS BEHIND C++ Language Magics Object Life Time Object Memory Layout INTRODUCTION

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems Basics of OO Programming with Java/C# Basic Principles of OO Abstraction Encapsulation Modularity Breaking up something into smaller, more manageable pieces Hierarchy Refining through levels of abstraction

More information

Table of Contents Preface Bare Necessities... 17

Table of Contents Preface Bare Necessities... 17 Table of Contents Preface... 13 What this book is about?... 13 Who is this book for?... 14 What to read next?... 14 Personages... 14 Style conventions... 15 More information... 15 Bare Necessities... 17

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

Polymorphism. return a.doublevalue() + b.doublevalue();

Polymorphism. return a.doublevalue() + b.doublevalue(); Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More 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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 10 Inheritance Chapter Topics (1 of 2) 10.1 What Is Inheritance? 10.2 Calling the Superclass Constructor 10.3 Overriding

More information

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