public class Animal // superclass { public void run() { MessageBox.Show("Animals can run!"); } }

Size: px
Start display at page:

Download "public class Animal // superclass { public void run() { MessageBox.Show("Animals can run!"); } }"

Transcription

1 Lecture #8 What is inheritance? Inheritance and Polymorphism Inheritance is an important object-oriented concept. It allows you to build a hierarchy of related classes, and to reuse functionality defined in existing classes. Inheritance is the IS-A relationship. When on object is a specialized version of another object, there is an IS-A relationship between them. For example, a grasshopper is an insect. Inheritance involves a base class and a derived class. The base class is the general class (insect) and the derived class is the specialized class (grasshopper). So, in C#, inheritance is all about building a derived class that inherits attributes and methods from the base class. C# inheritance One of the fundamental activities of object-oriented programming is establishing relationships between classes. Two fundamental ways to relate classes are inheritance and composition. In object-oriented programming, the relationship happens when two classes interact with each other. Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from -- the base class -- after the colon, as follows: public class A public A() public class B : A public B() In the example above, class B is effectively both B and A. When you access a B object, you can use the cast operation to convert it to an A object. The B object is not changed by the cast, but your view of the B object becomes restricted to A s data and behaviors. After casting a B to an A, that A can be cast back to a B. In the following example, the Cat class is related to the Animal class by inheritance, because a cat is a kind of animal. Notice that the Main() method is in the Cat class. By the way, C# does not allow multiple inheritance from classes. public class Animal // superclass public void run() MessageBox.Show("Animals can run!"); public class Cat : Animal // subclass Cat mycat = new Cat(); mycat.run(); Visual C# - Dr. Penn P. Wu 217

2 In biology, the term inheritance refers to attributes acquired via biological heredity from the parents. In a society, inheritance is the practice of passing on property, titles, debts, and obligations upon the death of an individual. However, in object-oriented programming, inheritance refers to creating a new class (called the superclass or base class or parent class) and sharing its class members with other classes (called the subclass or derived class or child class). public class Cat : Animal // subclass Cat c1 = new Cat(); Animal c2 = new Animal(); Animal c3 = new Cat(); Cat c4 = new Animal(); // invalid In the subclass, you can create instance of both superclass and subclass, such as c1 and c2 in the above example. Since, the subclass (Cat) is part of the superclass (Animal), you can say A cat is an animal!, you can create a superclass object and specialize it to the subclass, such as c3. However, An animal is not necessary a cat!, so c4 is an invalid instantiation. The following example has two classes. The parent class is named Base and the child class is called Derived. The child class uses existing code from parent. For example, public class Games // parent private int screenwidth; private int screenheight; private string genre; public Games() // empty default constructor public Games(int swidth, int sheight, string gr) screenwidth = swidth; screenheight = sheight; genre = gr; // method public void display() MessageBox.Show("It is a " + genre + " game."); public class Console : Games // child Games alien = new Games(); Games pacman = new Games(480, 320, "Maze"); pacman.display(); Visual C# - Dr. Penn P. Wu 218

3 Another example, public class Base public String str; public Base() str = "1. Base construct of the Base class.\n"; public void display() str += "4. Function of the Base Class.\n"; public class Derived : Base public Derived() str += "2. Derived Constructor.\n"; show(); public void show() str += "3. Function of the Derived Class.\n"; Derived child = new Derived(); child.display(); MessageBox.Show(child.str); The following Derived declaration tells the compiler to use Base as the base class of Derived. The base class is specified by adding a colon, ":", after the derived class identifier and then specifying the base class name. public class Derived : Base The following code segment declare an object called child, which represents the Derived() construct. Because it is part of the Main() function, the Derived() will be executed automatically to produce an output -- Derived Constructor -- on screen. Derived child = new Derived(); The following line simply tells the compiler to associate the display() method to the child object. Derived does not have its own display() method, so it uses the Base display() method. You can see the results in the 3rd line of output. child.display(); Visual C# - Dr. Penn P. Wu 219

4 The output looks: Notice that derived classes have exactly the same capabilities as Base. Because of this, you can also say Derived is a Base. This is shown in the Main() method of Derived when the display() method is called. C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. Also, Base classes are automatically instantiated before derived classes. Look at the output from about code; it also indicates the sequence of execution. Basically the Base constructor executed before the Derived constructor. Shape Square Rectangle Rhombus Figure 1. In Figure 1, the Shape class is a superclass and can be specialized to three subclasses: Square, Rectangle, and Rhombus. Consider the following Shape class which contains three instance variables w, h, and cortex as well as one method area(). All of these three class members apply to squares and rectangles. public class Shape public int cortex = 4; public double w, h; public Shape(double x, double y) w = x; h = y; area(); public void area() MessageBox.Show("The area is " + w*h + " and the number of cortex is " + cortex); Shape myshape = new Shape(2,3); Visual C# - Dr. Penn P. Wu 220

5 Since inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality, it will be convenient and efficient to allow both Square and Rectangle class to share these three members. C# uses the : operator to set the relationship between a superclass (parent class) and a subclass (child class). For example, public class Rectangle : Shape public Rectangle(double x, double y) w = x; h = y; area(); public static void main() Rectangle myrectangle1 = new Rectangle(3.1,4.6); In C# the subclass inherits the superclass, so the subclass inherits the superclass. The inheritance relationship means that the subclass inherits the members of the superclass. In the above examples, the Rectangle classes inherits w, h, and area() from the Shape class. Subclass can also have its own constructor. In the following example, the Square class has a constructor named Square() with one parameter x in it. public class Shape public int cortex = 4; public double w, h; public void area() MessageBox.Show("The area is " + w*h); class Square : Shape // inheritance public Square(double x) // constructor of subclass w = h = x; area(); Square mysquare1 = new Square(2.5); The subclass can override the methods it inherits from the superclass. A subclass can override any member of the superclass including instance variables and methods. However, instance variables usually do not have to be overridden because they do not define any special behavior. In the following example, the area() method of the Shape class is overridden by the area() method of the Rectangle class. Both w and h variables of the Shape class are overridden as well. The overriding is done with the new keyword. Visual C# - Dr. Penn P. Wu 221

6 public class Shape public int cortex = 4; public double w, h; public void area() MessageBox.Show("The area is " + w*h + "\n"); public class Rectangle : Shape new int w; // overriding w new int h; // overriding h public Rectangle(int x, int y) w = x; h = y; area(); public new void area() // overriding area() MessageBox.Show(w*h + " is the area.\n"); Rectangle myrectangle1 = new Rectangle(3,4); The output looks: There is another way to inherit from one class without using the : operator. In the following code, the Car class is an individual class that is a reference class. In the Hybrid class, the mycar object is instantiated as Car class. The Hybrid() constructor creates the mycar object, calls the checkthecar() function of the Car class. public class Car public String checkthecar() return "The original car!\n"; Visual C# - Dr. Penn P. Wu 222

7 public class Hybrid Car hybrid1; // declare hybrid1 as instance of Car class public String msg; public Hybrid() hybrid1 = new Car(); // create an instance msg = hybrid1.checkthecar(); msg += newengine(); public String newengine() return "The car is more powerful now with hybrid engine!"; public class mycar Hybrid car1 = new Hybrid(); MessageBox.Show(car1.msg); The output looks: The base method A subclass can inherit accessible members from its superclass. However, the constructor of the superclass is not inherited automatically. A constructor is used to construct an instance of a class. Unlike properties and methods, constructors are not inherited; they cannot be invoked from the subclass. The constructor in a superclass can only be called by its subclasses through the use of base method. The syntax is: SubclassMethod() : base() or For example, SubclassMethod() : base(parameters) public class Person public Person(char gender) // superclass constructor String msg; switch (gender) Visual C# - Dr. Penn P. Wu 223

8 case 'F' : msg = "woman"; break; case 'M' : msg = "man"; break; default : msg = "No such gender"; break; MessageBox.Show(msg); public class MyStudent : Person public MyStudent(char gender) : base(gender) MyStudent obj1 = new MyStudent('F'); The output looks: The super statement must be placed at the first line in order to call the superclass constructor. A subclass constructor may have multiple forms (polymorphism) and can overload its superclass constructor through the use of base method. A later section will discuss polymorphism in details. For example, public class Person public String str; public Person() // superclass constructor form 1 str += "human\n"; public Person(char gender) // superclass constructor form 2 switch (gender) case 'F' : str += "woman\n"; break; case 'M' : str += "man\n"; break; default : str += "No such gender\n"; break; public Person(int age) // superclass constructor form 3 if (age >= 21) str += "adult\n"; else str += "Not adult\n"; Visual C# - Dr. Penn P. Wu 224

9 public class MyStudent : Person public MyStudent() : base() public MyStudent(char gender) : base(gender) public MyStudent(int age): base(age) public MyStudent(double height) // overloading str += height + " feet\n"; MyStudent obj1 = new MyStudent(); MyStudent obj2 = new MyStudent('F'); MyStudent obj3 = new MyStudent(21); MyStudent obj4 = new MyStudent(5.6); MessageBox.Show(obj1.str + obj2.str + obj3.str + obj4.str); The output looks: Polymorphism Polymorphism is a biology term that literally means multiple forms. It describes the phenomena that two or more different phenotypes often exist in the same population of a species. In terms of object-oriented programming, polymorphism is the ability for the same code to be used with several different types of objects and for the code to behave differently depending on the actual type of object used. In C#, polymorphism can be implemented by two different ways: overloading and overriding. In a given class, when multiple methods have the same name but different in behaviors (forms), these methods are overloaded methods. For example, in the myadd class there are two methods. However both methods have exactly the same name add. One accepts only integer type; the other accepts only double type. The overloading happens inside the myadd class. public class myadd // original form int add(int x, int y) return (x + y); double add(double x, double y) //form2 Visual C# - Dr. Penn P. Wu 225

10 return (x + y); public static void Main(String[] args) myadd obj = new myadd(); String str = obj.add(3, 5) + "\n"; str += obj.add(9.2, 4.5); MessageBox.Show(str); The output looks: More than two forms of methods can coexist in a given class. In the following example, the mytype class contains five definitions of a showit() method. public class mytype public String str; public mytype() str = ""; public void showit(int x) // form 1 str += "int type: " + x + "\n"; public int showit(int x, int y) // form 2 return (x * y); public void showit(double x, double y) // form 3 str += "Tow double types: " + (x / y) + "\n"; public void showit(char x) // form 4 str += "char type: " + x + "\n"; public void showit(string x) // form 5 str += "String type: " + x + "\n"; Visual C# - Dr. Penn P. Wu 226

11 After instantiating an object of the mylab class, the C# runtime will use the so-called latebinding technique to decide which of the many forms of a given method should be used. For example, public class mylab public static void main() mytype mt = new mytype(); mt.showit(5); mt.showit(3.72); mt.showit('a'); mt.showit("hello!"); Notice that overloading can happen in exactly the same class. It can also happen between a subclass and its superclass. For example, let the mychildtype class inherits from the mytype class, but override the showit() method in the mychildtype class. public class mychildtype : mytype // subclass public void showit(double x) // form 6 str += "double type: " + x*x + "\n"; The following code can demonstrate how the overriding works between a subclass and its superclass. class mylab public static void main(string[] args) mychildtype mct = new mychildtype(); mct.showit(7); // no overriding mct.showit(7.1); // overriding The following is a class named Coffee with a brew() method in it. The brew() method takes one String type of parameter. public class Coffee public String str; public Coffee() str = ""; public void brew(string flavor) // original form str += "The taste is " + flavor + ".\n"; You can instantiate an object (e.g. cup1 ) of the Coffee class and use it to call the brew() method with a string literal (e.g. Maxell Roast ) in it. class mypolymorphism Visual C# - Dr. Penn P. Wu 227

12 Coffee cup1 = new Coffee(); cup1.brew("maxwell Roast"); // using original form MessageBox.Show(cup1.str); The output looks: Let the following FrenchVanilla class be a subclass to Coffee so it can inherit members from the Coffee class. However, this FrenchVanilla class redefines the brew() method to override its original form (the one defined in Coffee). Notice that the second form of brew() does not require the parameter. public class FrenchVanilla : Coffee public void brew() // second form str += "The taste is French Vanilla.\n"; The instantiation of a FrenschVanilla object (e.g. cup2 ) can use the forms of brew() method as illustrated in the following example. Notice that the original form is inherited from Coffee). class mypolymorphism public static void main() FrenchVanilla cup2 = new FrenchVanilla(); cup2.brew(); // using 2nd form cup2.brew("nest"); // using original form MessageBox.Show(cup2.str); Objects of the FrenschVanilla class can react differently to the same method. The ability that allows a subclass to redefine a method it inherits from a superclass is called overriding in C#. By the same token, you can create another subclass named Latte with another form of brew() method in it, as shown below. This form of brew requires a char type of parameter. Inside the brew() method, it uses an if..else..if statement to evaluate the expression and respond accordingly. This form is the most complicated among these forms. public class Latte : Coffee public void brew(char option) // third form str += "The taste is "; Visual C# - Dr. Penn P. Wu 228

13 if (option=='b') str += "Blue mountain.\n"; else if (option=='m') str += "Mocca.\n"; else if (option=='u') str += "Japanese UCC.\n"; else str += "Unknown taste.\n"; The object of Latte can also enjoy two forms of brew(), as illustrated below. class mypolymorphism Latte cup3 = new Latte(); cup3.brew('m'); // using 3rd form cup3.brew("folgers"); // using original form MessageBox.Show(cup3.str); The output looks: It is necessary to understand the difference between method overloading and method overriding. Operation overloading overriding Description Creating multiple methods in a class with the same name but different parameters and types is called method overloading. Creating the method in a derived class with the same name, the same parameters and the same return type as in a base class is called method overriding. The following illustrates how overloading works. In the Car class, there are three forms of Method1(), each takes a parameter of different type. Every form of the Method1() also return a data of different type. public class Car private int Method1(int x) return x * x; private double Method1(double x) //overloading return x * x; Visual C# - Dr. Penn P. Wu 229

14 private String Method1(String x) //overloading return x + x; static void Main(string[] args) String str = ""; Car obj = new Car(); str += obj.method1(4) + "\n"; str += obj.method1(4.3) + "\n"; str += obj.method1("four"); MessageBox.Show(str); The following illustrates how overriding works across three classes: Car is the base class (superclass), SUV is the derived class that inherits Car. Pathfinder is a derived class that inherits SUV. Method overriding happens when SUV inherits Car, and, when Pathfinder inherits car through SUV. public class Car public int Method1(int x) return x * x; public class SUV : Car public double Method1(double x) //overriding return x * x; public class Pathfinder: SUV public String Method1(String x) //overriding return x + x; public class Program static void Main(string[] args) String str = ""; Pathfinder obj = new Pathfinder(); str += obj.method1(4) + "\n"; str += obj.method1(4.3) + "\n"; str += obj.method1("four"); MessageBox.Show(str); Visual C# - Dr. Penn P. Wu 230

15 Review Questions 1. According to the following code segment, which is an invalid instantiation? public class Cat : Animal A. Cat c1 = new Cat(); B. Animal c1 = new Animal(); C. Animal c1 = new Cat(); D. Cat c1 = new Animal(); 2. Given the following code segment, which is the base class? A. A B. B C. A and B D. not specified public class B : A Which declare an object called child to represent the Derived construct? A. Derived() child; B. Derived child : base; C. Derived child = new Derived(); D. child = new Derived(); 4. Which associates the show() method to an object called apple? A. apple.show(); B. apple::show(); C. apple += show(); D. apple -> show(); 5. Which statement is correct? A. A derived class inherits everything from the base class except constructors and destructors. B. The public members of the Base class become the public members of the Derived class also. C. Even the private members of the base class are inherited to the derived class, even though derived class can't access them. D. All of the above 6. Which is the correct way for a subclass (named "Apple") to inherit the default constructor from a superclass (named "Fruit") in C#? A. public Apple() : base() B. public Apple() : Fruit() C. public Apple() base(); D. public Fruit.Apple() 7. Given the following code, which statement is correct? public class B : A public B() A. B is the base class. A is the derived class. B. A is the base class. B is the derived class. C. B is the base class. A is the superclass. D. A is the derived class. B is the subclass. Visual C# - Dr. Penn P. Wu 231

16 8. Given the following code, which can override w by changing its type to "int" in a subclass in C#? A. int w; B. int new w; C. new int w; D. (int) w; public class Shape public double w, h; 9. Given the following code, which can override the default constructor "myadd()"? public class myadd public myadd() A. public myadd(string str) B. public myadd(int i) C. public myadd(double x, double y) D. All of the above. 10. Which is the wrong way to override the add() method of the following code? public class myadd void add(string s) A. int add(int x) return x; B. double add(double x) return x; C. char add(char c) return c; D. void add(string s) return s; Visual C# - Dr. Penn P. Wu 232

17 Lab #8 Inheritance and Polymorphism Learning Activity #1: 1. Create a new directory called C:\CIS218 if it does not exist. 2. Launch the Developer Command Prompt. 3. In the prompt, type cd c:\cis218 and press [Enter] to change to the C:\CIS218 directory. 4. In the prompt, type notepad lab8_1.cs and press [Enter] to use Notepad to create a new source file called lab8_1.cs with the following contents: public class Shape public int cortex = 4; public double w, h; public void area() String str = "The area is " + w*h; MessageBox.Show(str); public class Square : Shape // inheritance public Square(double x) // default constructor w = h = x; area(); Square obj1 = new Square(2.5); // create an instance of Square 5. Compile and test the program. A sample output looks: 6. Download the assignment template, and rename it to lab8.doc if necessary. Capture a screen shot similar to the above and paste it to the Word document named lab8.doc (or.docx). Learning Activity #2: demonstration of inheritance 1. Under the C:\cis218 directory, use Notepad to create a new source file called lab8_2.cs with the following contents: Visual C# - Dr. Penn P. Wu 233

18 public class Parent public String parentstring; public Parent() parentstring = "5. Message of the Parent Constructor.\n"; public Parent(string mystring) parentstring += mystring; public void display() parentstring += "3. The display() method of the Parent Class.\n"; public class Child : Parent public Child() : base("1. Inherited from the Parent class\n") parentstring += "2. Message of the Child Constructor.\n"; public new void display() base.display(); parentstring += "4. The display() method of the Child Class.\n"; Child child = new Child(); child.display(); ((Parent)child).display(); MessageBox.Show(child.parentString); 2. Compile and test the program. A sample output looks: 3. Capture a screen shot similar to the above and paste it to the Word document named lab8.doc (or.docx). Learning Activity #3: demonstration of inheritance Visual C# - Dr. Penn P. Wu 234

19 1. Under the C:\cis218 directory, use Notepad to create a new source file called lab8_3.cs with the following contents: class Shape public class Square public double x; public Square(double x) // Constructor this.x = x; public virtual double Area() return x*x; class Cube : Square public Cube(double x): base(x) // Constructor this.x = x; public override double Area() // Calling the Area base method: return (x * (base.area())); double x = 3.47; Square s = new Square(x); Square c = new Cube(x); String str = "Area of Square = " + s.area() + "\n"; str += "Volume of Cube = " + c.area() + "\n"; MessageBox.Show(str); 2. Compile and test the program. A sample output looks: 3. Capture a screen shot similar to the above and paste it to the Word document named lab8.doc (or.docx). Visual C# - Dr. Penn P. Wu 235

20 Learning Activity #4: base() method 1. Under the C:\cis218 directory, use Notepad to create a new source file called lab8_4.cs with the following contents: public class Person public String str; public Person() // superclass constructor form 1 str += "human\n"; public Person(char gender) // superclass constructor form 2 switch (gender) case 'F' : str += "woman\n"; break; case 'M' : str += "man\n"; break; default : str += "No such gender\n"; break; public Person(int age) // superclass constructor form 3 if (age >= 21) str += "adult\n"; else str += "Not adult\n"; public class MyStudent : Person public MyStudent() : base() public MyStudent(char gender) : base(gender) public MyStudent(int age): base(age) public MyStudent(double height) // overloading str += height + " feet\n"; MyStudent obj1 = new MyStudent(); MyStudent obj2 = new MyStudent('F'); MyStudent obj3 = new MyStudent(23); MyStudent obj4 = new MyStudent(5.6); MessageBox.Show(obj1.str + obj2.str + obj3.str + obj4.str); 2. Compile and test the program. A sample output looks: Visual C# - Dr. Penn P. Wu 236

21 3. Capture a screen shot similar to the above and paste it to the Word document named lab8.doc (or.docx). Learning Activity #5: Polymorphism 1. Under the C:\cis218 directory, use Notepad to create a new source file called lab8_4.cs with the following contents: public class mytype // superclass public String str; public mytype() str = ""; public void showit(int x) // form 1 str += "int type: " + x + "\n"; public int showit(int x, int y) // form 2 return (x * y); public void showit(double x, double y) // form 3 str += "Tow double types: " + (x / y) + "\n"; public void showit(char x) // form 4 str += "char type: " + x + "\n"; public void showit(string x) // form 5 str += "String type: " + x + "\n"; public class mychildtype : mytype // subclass public void showit(double x) // form 6 str += "double type: " + x*x + "\n"; Visual C# - Dr. Penn P. Wu 237

22 class mypolymorphism // in-class overloading mytype mt = new mytype(); mt.showit(7); mt.showit(4.5, 3.17); mt.showit('m'); mt.showit("hello!"); mt.str += "Two int types: " + mt.showit(5, 7) + "\n"; // subclass overloading mychildtype mct = new mychildtype(); mct.showit(7.1); MessageBox.Show(mt.str + mct.str); 2. Compile and test the program. A sample output looks: 3. Capture a screen shot similar to the above and paste it to the Word document named lab8.doc (or.docx). Programming Exercise: 1. Use Notepad to create a new file named ex08.cs with the following heading lines (be sure to replace YourFullNameHere with the correct one): //File Name: ex08.cs //Programmer: YourFullNameHere 2. Under the above two heading lines, write C# codes to create a superclass (or base class) and a subclass (or derived class) based on the following specifications: Superclass (Parent, Base) Class Name: computer Class Members: o One public string field named model o One public double field named weight o One void method named ShowResult that displays the model and weight in a message box, as shown below. Subclass (Child, Derived) Class Name: laptop Visual C# - Dr. Penn P. Wu 238

23 3. In the laptop subclass, create a Main() method. In the Main() method, create an object named comp1 of the laptop class. Assign the following values of the fields of computer class. Field Name Value Model ASUS101 Weight Download the programming exercise template, and rename it to ex08.doc. Capture a screen shot similar to the above figure and then paste it to the Word document named ex08.doc (or.docx). 5. Compress the source code (ex08.cs), the executable (ex08.exe), and the Word document (ex08.doc or.docx) to a.zip file named ex08.zip. You may be given zero point if any of the required file is missing. Grading Criteria: You must be the sole author of the codes. You must meet all the requirements in order to earn credits. You will receive zero if you simply display the message. No partial credit is given. Submittal 1. Complete all the 5 learning activities and the Programming Exercise in this lab. 2. Create a.zip file named lab8.zip containing ONLY the following self-executable files. Lab8_1.exe Lab8_2.exe Lab8_3.exe Lab8_4.exe Lab8_5.exe Lab8.doc (or.docx) [You may be given zero point if this Word document is missing] 3. Log in to Blackboard and enter the course site. 4. Upload the zipped file as response to question Upload the ex08.zip file to Question 12 as response. Visual C# - Dr. Penn P. Wu 239

#using <System.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms;

#using <System.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; Lecture #13 Introduction Exception Handling The C++ language provides built-in support for handling anomalous situations, known as exceptions, which may occur during the execution of your program. Exceptions

More information

Car. Sedan Truck Pickup SUV

Car. Sedan Truck Pickup SUV Lecture #7 Introduction Inheritance, Composition, and Polymorphism In terms of object-oriented programming, inheritance is the ability that enables new classes to use the properties and methods of existing

More information

accessmodifier [static] returntype functionname(parameters) { } private string getdatetime() { } public void getdatetime() { }

accessmodifier [static] returntype functionname(parameters) { } private string getdatetime() { } public void getdatetime() { } Lecture #6 User-defined functions User-Defined Functions and Delegates All C# programs must have a method called Main(), which is the starting point for the program. Programmers can also add other methods,

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

Table 1: Typed vs. Generic Codes Scenario Typed Generic Analogy Cash Only Multiply Payment Options. Customers must pay cash. No cash, no deal.

Table 1: Typed vs. Generic Codes Scenario Typed Generic Analogy Cash Only Multiply Payment Options. Customers must pay cash. No cash, no deal. Lecture #15 Overview Generics in Visual C++ C++ and all its descendent languages, such as Visual C++, are typed languages. The term typed means that objects of a program must be declared with a data type,

More information

Term Object-Oriented Paradigm Real-life example Abstraction Class declaration, creation, and definition

Term Object-Oriented Paradigm Real-life example Abstraction Class declaration, creation, and definition Lecture #6 What is a class and what is an object? Object-Oriented Programming Assuming that the instructor found a new breed of bear and decided to name it Kuma. In an email, the instructor described what

More information

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism Programming using C# LECTURE 07 Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism What is Inheritance? A relationship between a more general class, called the base class

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

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

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

More information

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

Arrays Classes & Methods, Inheritance

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

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

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

Calling predefined. functions (methods) #using <System.dll> #using <System.dll> #using <System.Windows.Forms.dll>

Calling predefined. functions (methods) #using <System.dll> #using <System.dll> #using <System.Windows.Forms.dll> Lecture #5 Introduction Visual C++ Functions A C++ function is a collection of statements that is called and executed as a single unit in order to perform a task. Frequently, programmers organize statements

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

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

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

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

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

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

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

More information

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

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

More information

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

CIS 190: C/C++ Programming. Lecture 11 Polymorphism

CIS 190: C/C++ Programming. Lecture 11 Polymorphism CIS 190: C/C++ Programming Lecture 11 Polymorphism 1 Outline Review of Inheritance Polymorphism Limitations Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors

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

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

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance in Java 1. Inheritance 2. Types of Inheritance 3. Why multiple inheritance is not possible in java in case of class? Inheritance in java is a mechanism in which one object acquires all the

More information

#using <System.dll> #using <System.Windows.Forms.dll>

#using <System.dll> #using <System.Windows.Forms.dll> Lecture #8 Introduction.NET Framework Regular Expression A regular expression is a sequence of characters, each has a predefined symbolic meaning, to specify a pattern or a format of data. Social Security

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

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

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

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

Operating System x86 x64 ARM Windows XP Windows Server 2003 Windows Vista Windows Server 2008 Windows 7 Windows Server 2012 R2 Windows 8 Windows 10

Operating System x86 x64 ARM Windows XP Windows Server 2003 Windows Vista Windows Server 2008 Windows 7 Windows Server 2012 R2 Windows 8 Windows 10 Lecture #1 What is C++? What are Visual C++ and Visual Studio? Introducing Visual C++ C++ is a general-purpose programming language created in 1983 by Bjarne Stroustrup. C++ is an enhanced version of the

More information

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions Chapter 15: Inheritance, Polymorphism, and Virtual Functions 15.1 What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version

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

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

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

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

Polymorphism. Agenda

Polymorphism. Agenda Polymorphism Lecture 11 Object-Oriented Programming Agenda Classes and Interfaces The Object Class Object References Primitive Assignment Reference Assignment Relationship Between Objects and Object References

More information

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

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

More information

Java for Non Majors Spring 2018

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

More information

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

However, it is necessary to note that other non-visual-c++ compilers may use:

However, it is necessary to note that other non-visual-c++ compilers may use: Lecture #2 Anatomy of a C++ console program C++ Programming Basics Prior to the rise of GUI (graphical user interface) operating systems, such as Microsoft Windows, C++ was mainly used to create console

More information

Object Orientated Programming Details COMP360

Object Orientated Programming Details COMP360 Object Orientated Programming Details COMP360 The ancestor of every action is a thought. Ralph Waldo Emerson Three Pillars of OO Programming Inheritance Encapsulation Polymorphism Inheritance Inheritance

More information

Sorting. Sorting. Selection sort

Sorting. Sorting. Selection sort Sorting 1 Sorting Given a linear list of comparable objects of the same class (or values of the same type), we wish to sort (or reärrange) the objects in the increasing order. For simplicity, let s just

More information

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance? CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

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

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

The following table distinguishes these access modifiers.

The following table distinguishes these access modifiers. Lecture #7 Introduction User-Defined Functions and Delegates In programming, a function is defined as named code block that can perform a specific task. Some programming languages make a distinction between

More information

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points. Java for Non Majors Final Study Guide April 26, 2017 The test consists of 1. Multiple choice questions 2. Given code, find the output 3. Code writing questions 4. Code debugging question 5. Short answer

More information

Chapter 5. Inheritance

Chapter 5. Inheritance Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism through Method Overriding Learn the keywords : super

More information

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

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

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

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

JAVA MOCK TEST JAVA MOCK TEST II

JAVA MOCK TEST JAVA MOCK TEST II http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

More information

Computer Science 210: Data Structures

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

More information

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

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass? COMP9024: Data Structures and Algorithms Week One: Java Programming Language (II) Hui Wu Session 1, 2014 http://www.cse.unsw.edu.au/~cs9024 Outline Inheritance and Polymorphism Interfaces and Abstract

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

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

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More 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

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

INHERITANCE Mrs. K.M. Sanghavi

INHERITANCE Mrs. K.M. Sanghavi INHERITANCE Mrs. K.M. Sanghavi Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java

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

Fundamentals of Object Oriented Programming

Fundamentals of Object Oriented Programming INDIAN INSTITUTE OF TECHNOLOGY ROORKEE Fundamentals of Object Oriented Programming CSN- 103 Dr. R. Balasubramanian Associate Professor Department of Computer Science and Engineering Indian Institute of

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

Introduction to Computation and Problem Solving

Introduction to Computation and Problem Solving Class 13: Inheritance and Interfaces Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward 2 More on Abstract Classes Classes can be very general at the top of

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

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

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Sung Hee Park Department of Mathematics and Computer Science Virginia State University September 18, 2012 The Object-Oriented Paradigm

More information

1 Method Signatures and Overloading (3 minutes, 2 points)

1 Method Signatures and Overloading (3 minutes, 2 points) CS180 Spring 2010 Exam 1 Solutions, 15 February, 2010 Prof. Chris Clifton Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited. Time will be tight. If you spend more than

More information

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

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

More information

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

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

More information

Module 10 Inheritance, Virtual Functions, and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism Module 10 Inheritance, Virtual Functions, and Polymorphism Table of Contents CRITICAL SKILL 10.1: Inheritance Fundamentals... 2 CRITICAL SKILL 10.2: Base Class Access Control... 7 CRITICAL SKILL 10.3:

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

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

C# Programming for Developers Course Labs Contents

C# Programming for Developers Course Labs Contents C# Programming for Developers Course Labs Contents C# Programming for Developers...1 Course Labs Contents...1 Introduction to C#...3 Aims...3 Your First C# Program...3 C# The Basics...5 The Aims...5 Declaring

More information

CMSC202 Computer Science II for Majors

CMSC202 Computer Science II for Majors CMSC202 Computer Science II for Majors Lecture 14 Polymorphism Dr. Katherine Gibson Last Class We Covered Miscellaneous topics: Friends Destructors Freeing memory in a structure Copy Constructors Assignment

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java CSCI/CMPE 3326 Object-Oriented Programming in Java inheritance Dongchul Kim Department of Computer Science University of Texas Rio Grande Valley Lab8 Please at the top of the program, as a comment include

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

Classes and Inheritance Extending Classes, Chapter 5.2

Classes and Inheritance Extending Classes, Chapter 5.2 Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.

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

Java Fundamentals (II)

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

More information

Brief Summary of Java

Brief Summary of Java Brief Summary of Java Java programs are compiled into an intermediate format, known as bytecode, and then run through an interpreter that executes in a Java Virtual Machine (JVM). The basic syntax of Java

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

CSE 143 Lecture 20. Circle

CSE 143 Lecture 20. Circle CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public

More information

Chapter 6 Reflection

Chapter 6 Reflection Table of Contents Date(s) Title/Topic Page #s 3/11 Chapter 6 Reflection/Corrections 94 Chapter 7: Inheritance 95 7.1 Creating Subclasses 96 Chapter 6 Reflection look over your Ch 6 Test and write down

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

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

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

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

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2 Tutorial 7 If it compiles, what does it print? Here are some exercises to practice inheritance concepts, specifically, overriding, overloading, abstract classes, constructor chaining and polymorphism.

More information