250 likes | 265 Views
This agenda covers topics such as packages in Java, package hierarchy, famous Java packages, package access, writing a packaged Java project, inheritance in Java, UML class diagrams, and a quiz.
E N D
Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2010
Agenda • Packages • Inheritance Sharif University of Technology
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 Sharif University of Technology
Some famous java packages • java.util • java.lang • java.io Sharif University of Technology
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 Sharif University of Technology
Using packages Sharif University of Technology
java.lang • java.lang is implicitly imported Sharif University of Technology
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. Sharif University of Technology
Write a packaged java project • Lets write … Sharif University of Technology
Inheritance Sharif University of Technology
UML Class Diagram Sharif University of Technology
UML Class Diagram Sharif University of Technology
Inheritance • Java offers inheritance • As any object oriented programming language • Code reuse • extends keyword • is-a relationship Sharif University of Technology
Faculty & Staff • Attributes of staff is inherited to Faculty Sharif University of Technology
Shapes Example • Shape is an abstract class • The definition of some methods should be changed in sub-classes • getArea() • getPerimeter() Sharif University of Technology
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-classof Rectangle • Rectangle is-a Shape Sharif University of Technology
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() Sharif University of Technology
Quiz! Sharif University of Technology
Quiz • Draw a UML class diagram for these Classes • Person (name, phoneNumber) • Student (average, entranceYear) • MS Student (thesis title) • Instructor (offeredCourses) Sharif University of Technology