COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Phil Campbell London South Bank University Java 1 First Steps.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
CS442: ADVANCED PROGRAMMING USING JAVA Lab 6: Classes and objects Computer Science Department.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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.
BY:- TOPS Technologies
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Modern Programming Tools And Techniques-I
Polymorphism.
Lecture 12 Inheritance.
Overriding Method.
03/10/14 Inheritance-2.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lecture 17: Polymorphism (Part II)
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
CS 1430: Programming in C++.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Inheritance, Polymorphism, and Interfaces. Oh My
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance.
METHOD OVERRIDING in JAVA
COMPUTER 2430 Object Oriented Programming and Data Structures I
Sampath Kumar S Assistant Professor, SECE
Chapter 10: Method Overriding and method Overloading
Lecture 6 Inheritance CSE /26/2018.
Lecture 18: Polymorphism (Part II)
Object-Oriented Programming
Sampath Kumar S Assistant Professor, SECE
F II 8. More on Inheritance Objectives
Object-Oriented Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Topics OOP Review Inheritance Review Abstract Classes
CS Problem Solving and Object Oriented Programming Spring 2019
CS 240 – Advanced Programming Concepts
Programming in C# CHAPTER 5 & 6
Inheritance and Polymorphism
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Class Person is a subclass of Object public class Person { protected String name; public Person(String s) name = s; } @Override public String toString() return name; Class Person is a subclass of Object Overrides toString

Student is a subclass of Person public class Student extends Person { private float gpa; public Student( String s, float value) super(s); gpa = value; } public boolean setGPA (float value) public float getGPA() @Override public String toString() return "Student " + name + " has a GPA of " + gpa; Student is a subclass of Person Overrides toString

Professor is a subclass of Person public class Professor extends Person { private String discipline; public Professor(String s, String d) super(s); discipline = d; } @Override public String toString() return "Professor " + name + " is in discipline of " + discipline; Professor is a subclass of Person Overrides toString

Subclasses override methods toString public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); } Subclasses override methods toString

The compiler will check this public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p.setGPA(3.3F); // Invalid s.setGPA(3.3F); // Valid f.setGPA(3.3F); // Invalid } For a class variable, only methods of the class can be called on the variable. The compiler will check this

public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); s = p; // Valid? }

An instance of base class may not be an instance of its subclasses public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); s = p; // Invalid System.out.println(s.getGPA()); } An instance of base class may not be an instance of its subclasses

public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid? }

public static void main(String[] args) Person p = new Person("Frank"); public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.anyBaseClassMethod(); } An instance of subclasses is an instance of its base class And inherits all methods of the base class

public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.toString(); // What’s the output? }

public static void main(String[] args) Person p = new Person("Frank"); public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.toString(); // What’s the output? } String from the subclass: Run-Time Bindding Object-Oriented Programming!

Inheritance Variables of subclasses cannot point to instances of its base class Variables of base class can point to instances of its subclasses You can use an instance of child class wherever a instance of parent class is expected (Not the other way around!) Run time binding: the instance determine which version of a method will be fired if the method is overridden in the subclass

Unified Modeling Language (UML) Very simple UML class diagram One way association (A “uses” B) (More specific in CS3430) Quiz is coming? Program due! A B

Unified Modeling Language (UML) Very simple UML class diagram One way association (A “uses” B) (More specific in CS3430) Quiz is coming? Program due! Prog0 ScoresList

Unified Modeling Language (UML) Very simple UML class diagram Ignore the data and methods Show class name only A inherits B Quiz is coming? Program due! A B

UML Person Student Quiz is coming? Program due! Professor

Lab 2 Part of Prog1 Five (5) points Due 10 pm, Wednesday, September 27 Must use the SE Tool to log your time as part of Prog1 https://alpha.ion.uwplatt.edu/CSSE/Account/Login Click Here

Lab2 Two files are at K:\Courses\CSSE\yangq\cs2430\1CourseMaterials\Lab2 GolfLeagueMember.java Golfer.java