CS 241 – Computer Programming II Lab Kalpa Gunaratna –

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
Chapter 71 Inheritance Chapter 7. 2 Reminders Project 4 was due last night Project 5 released: due Oct 10:30 pm Project 2 regrades due by midnight.
More on Objects CS 102 More, more, more on Objects.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 MATERI PENDUKUNG METHOD OVERRIDING Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
Multiple Choice Solutions True/False a c b e d   T F.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 14, Mar 27/28, 2013.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 TCSS 143, Autumn 2004 Lecture Notes Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 14, Mar 27/28, 2013.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
CS 241 – Computer Programming II Lab
EKT472: Object Oriented Programming
CS 241 – Computer Programming II Lab
CS 240 – Computer Programming I Lab
CS 240 – Computer Programming I Lab
Haidong Xue Summer 2011, at GSU
Interface, Subclass, and Abstract Class Review
Module 5: Common Type System
An Introduction to Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
C++ Programming CLASS This pointer Static Class Friend Class
Building Java Programs
Presentation transcript:

CS 241 – Computer Programming II Lab Kalpa Gunaratna –

Contact  Contact by   Office hours  376 Joshi  Mondays & Wednesday 3:00 pm – 4:00 pm

Inheritance  Example : A B B is a sub class of A A is the parent class B is the child class

 Use extends keyword. public class A { int x = 5; A() { } A(int x) { this.x = x; } public int getX() { return x; } public class B extends A { int y = 10; B(int y) { this.y = y; }

 If you do not have the default constructor, you have to call specifically a constructor by keyword super in the child class. public class A { int x = 5; A(int x) { this.x = x; } public int getX() { return x; } public class B extends A { int y = 10; B(int y) { super(5); this.y = y; }

 If you want to get parent class’s method behavior to a child class’s method, the parent method should be called in the first place in your child method using the keyword super and equivalent parameters. public class A { int x = 5; A(int x) { this.x = x; } public int getX() { return x; } public int calAge(int val) { int z = val * x; return z; } public class B extends A{ int y = 10; B(int y) { super(5); this.y = y; } public int calAge(int val) { int k = super.calAge(val); k += 3; return k; }

In lab  You need to understand extends & super keywords.  Do not use protected or instanceOf keywords.  Modify the two classes so that no code is redundant and Trekkie is a sub class of Geek.  Change code where necessary but the output should match to the original code output.

Post lab  Make classes as explained in the assignment.  Use polymorphic objects (geeks) in your test code.  Draw an UML class diagram for all of your classes so that it clearly represents inheritance from one to another.  Submit the UML diagram with your project to WebCT.