Programming in Java: lecture 7

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Advanced Object-Oriented Programming
Inheritance and Polymorphism
One class is an extension of another.
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
UML Class Diagram: class Rectangle
Nested class.
Object Oriented Programming
Chapter 10 Thinking in Objects
One class is an extension of another.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Interfaces.
Advanced Java Programming
Advanced Programming in Java
Java – Inheritance.
Java Programming, Second Edition
Java Inheritance.
Advanced Programming in Java
Inheritance and Polymorphism
Object Oriented Analysis and Design
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Programming in Java: lecture 7 Inheritance Polymorphism Abstract Classes this and super Interfaces Nested Classes and other details Example Slides made for use with ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Some figures are taken from ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Lecture 7 covers Section 5.5 to 5.7

Last time Objects, Classes and Instances Getters and setters Constructors and object initialization Wrapper Classes and Autoboxing Garbage collection and the heap Object oriented analysis and design Example

Classes and Objects A Class is a template Objects are objects Objects are instances of a given class Integer Class static members parseInt(String s) Integer Object non-static member equals(int i)

Inheritance Objects are instances of a given class Class A superclass Object of type A Class B extends A subclass Object of type B

Inheritance Vehicle HasWheels Flying Vehicle Truck Car Plane Helicopter

Inheritance Syntax Extending existing classes new methods override methods new instance variables

Class hierarchy Everything extends Object

Polymorphism Two concepts We can write code that can handle all future subclasses We can have variables without knowing the exact type of the object that it refers to

Abstract class Cannot make objects from abstract classes Can make variables from abstract classes Abstract Class A Object of type A Class B extends A Object of type B

Abstract example

this and super special variables cannot be assigned to this – the object we are currently in super – used to call methods of the super class forgets the exact type of the object special use in constructors Used as a method name Calls other constructors

this – example

super – example

Constructor example

Multiple inheritance Not allowed in Java

Interfaces Describes an aspect Completely abstract class nothing can be implemented

Interfaces Implementing multiple interfaces (serializable) Use of objects

Nested classes Classes inside classes Static Only one new type Non-static One new type per object Object o1 same type Class static nested class Object o2 same type

Nested classes Classes inside classes Static Only one new type Non-static One new type per object Object o1 different types Class nested class Object o2 different types

Example – static

Example – non static

Anonymous Inner Classes If you only need it in one place

Static import

Enums Enums are classes each enumerated type is a public static final member

Example Team programming