Sadegh Aliakbary Sharif University of Technology Fall 2010
Agenda Packages Inheritance Fall 2010Sharif University of Technology2
Package A package contains a group of classes Organized together under a single namespace Packages organize classes belonging to the same category or providing similar functionality A package provides a unique namespace for the types it contains Classes in one package has the same folder Packages may contain other packages Hierarchy of packages Fall 2010Sharif University of Technology3
Some famous java packages java.util java.lang java.io Fall 2010Sharif University of Technology4
Java Packages import keyword Class Qualified Name = package-name + class-name For example java.lang.String java.lang.Math double sqrt = Math.sqrt(123); java.util.Scanner java.awt.Event org.w3c.dom.events.Event Fall 2010Sharif University of Technology5
Using packages Fall 2010Sharif University of Technology6
java.lang java.lang is implicitly imported Fall 2010Sharif University of Technology7
Package Access Remember public and private access specifiers The default access has no keyword It is commonly referred to as package access and sometimes friendly Other classes in the current package have access to that member To classes outside of this package, the member appears to be private. Fall 2010Sharif University of Technology8
Write a packaged java project Lets write … Fall 2010Sharif University of Technology9
Fall 2010Sharif University of Technology10
Fall 2010Sharif University of Technology11
Fall 2010Sharif University of Technology12
UML Class Diagram Fall 2010Sharif University of Technology13
Fall 2010Sharif University of Technology14
Fall 2010Sharif University of Technology15
Fall 2010Sharif University of Technology16
UML Class Diagram Fall 2010Sharif University of Technology17
Inheritance Java offers inheritance As any object oriented programming language Code reuse extends keyword is-a relationship Fall 2010Sharif University of Technology18
Faculty & Staff Attributes of staff is inherited to Faculty Fall 2010Sharif University of Technology19
Shapes Example Shape is an abstract class The definition of some methods should be changed in sub-classes getArea() getPerimeter() Fall 2010Sharif University of Technology20
Terminology Class Rectangle is inherited from class Shape Rectangle is a subclass of Shape Rectangle is a child of Shape Rectangle is a derived class of Shape Shape is the super-class of Rectangle Shape is the parent of Rectangle Shape is the base-class of Rectangle Rectangle is-a Shape Fall 2010Sharif University of Technology21
Inheritance in java Every class is inherited from class Object Primitive-types are not objects Object class has some operations equals() toString() … Every class adds some operations And changes some operations toString() Fall 2010Sharif University of Technology22
Quiz! Fall 2010Sharif University of Technology23
Quiz Draw a UML class diagram for these Classes Person (name, phoneNumber) Student (average, entranceYear) MS Student (thesis title) Instructor (offeredCourses) Fall 2010Sharif University of Technology24
Fall 2010Sharif University of Technology25