CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Computer Science I Inheritance Professor Evan Korth New York University.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
What is inheritance? It is the ability to create a new class from an existing class.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Advanced Programming in Java
Lecture 12 Inheritance.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance and Polymorphism
Advanced Programming in Java
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Modern Programming Tools And Techniques-I Inheritance
Chapter 10 Thinking in Objects
CSC 143 Inheritance.
CSC 113 Tutorial QUIZ I.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Sampath Kumar.S Assistant Professor/IT, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Programming, Second Edition
An Example of Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

CS 2430 Day 9

Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)

Features of OOP Encapsulation Data hiding Abstraction / abstract data type NEW: Inheritance

A new keyword: extends public class A { private int x = 0; public void doIt(int y) { x += y; } public void print() { System.out.println(x); } } public class B extends A { // empty! } public static void main(String args[]) { B b = new B(); // The method is inherited: b.doIt(10); // x is printed out! b.print(); }

Inheritance example UML: Class B inherits from class A AB Inheritance represents an “is a” relationship public class A {... } public class B extends A {... }

Is-a relationship public class Student extends Person {... } public class Square extends Rectangle {... } public class Civic extends Car {... }

An example project // Assume Class A and B exist and B a sub-class of A public class C { public static void main(String args[]) { A a = new A(); B b = new B(); a.doIt(5); a.print(); b.doIt(10); b.print(); } }

UML for example project Class C uses classes A and B Class B inherits from class A AB C

Any questions?

Why use inheritance? Basic idea: when you want to create a new class and there is already a class that includes some of the code you want Simply extend the existing class! Can reuse the fields and methods in the existing class Less typing!

Some inheritance details

Subclass and superclass A class that is derived from another class is called a subclass (a.k.a., child class) The class from which a class is derived is called a superclass (a.k.a., parent class)

Subclasses and constructors A subclass inherits all members of its parent Constructors are (technically) NOT members, so they are not inherited by subclasses The constructor of the superclass can (and MUST) be invoked from the subclass using super() –Must be invoked on the first line of the constructor!

Any questions?

Inheritance example: class Person

public class Person { private String name; private int age; // in years public Person(String inName, int inAge) { name = inName; age = inAge; } public void speak() { System.out.print("Hello, my name is: " + name); } public void happyBirthday() { age += 1; } }

A subclass of Person : Student

public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); // NECESSARY! GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; } }

Subclasses can modify public and protected methods from the superclass by overriding them

public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; public void speak() { System.out.println("Hello, my name is " + name + " and my GPA is " + GPA); // syntax error! } }

public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; public void speak() { super.speak(); // can't access name directly! System.out.println(" and my GPA is " + GPA); } }

public class Person { protected String name; protected int age; // in years public Person(String inName, int inAge) { name = inName; age = inAge; } public void speak() { System.out.print("Hello, my name is: " + name); } public void happyBirthday() { age += 1; }

public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; public void speak() { System.out.println("Hello, my name is " + name + " and my GPA is " + GPA); // NOT a syntax error anymore! } }

Any questions?