Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Similar documents
Inheritance and Polymorphism

Object Orientated Analysis and Design. Benjamin Kenwright

ITI Introduction to Computing II

Introduction to Programming Using Java (98-388)

Java Object Oriented Design. CSC207 Fall 2014

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ITI Introduction to Computing II

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Questions Answer Key Questions Answer Key Questions Answer Key

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Class, Variable, Constructor, Object, Method Questions

Chapter 10 Classes Continued. Fundamentals of Java

Reusing Classes. Hendrik Speleers

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 14 Abstract Classes and Interfaces

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Methods Common to all Classes

Advanced Placement Computer Science. Inheritance and Polymorphism

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CH. 2 OBJECT-ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Object-Oriented Programming Concepts

Questions Answer Key Questions Answer Key Questions Answer Key

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

15CS45 : OBJECT ORIENTED CONCEPTS

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Chapter 5. Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

Java Fundamentals (II)

The Essence of OOP using Java, Nested Top-Level Classes. Preface

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Inheritance Motivation

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

Full file at Chapter 2 - Inheritance and Exception Handling

Chapter 3: Inheritance and Polymorphism

Programming 2. Inheritance & Polymorphism

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Use the scantron sheet to enter the answer to questions (pages 1-6)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Questions Answer Key Questions Answer Key Questions Answer Key

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Exam Duration: 2hrs and 30min Software Design

Chapter 6 Introduction to Defining Classes

Inheritance and Polymorphism

Instance Members and Static Members

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

index.pdf January 21,

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

OVERRIDING. 7/11/2015 Budditha Hettige 82

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java Magistère BFA

Example: Count of Points

Chapter 13 Abstract Classes and Interfaces

C++ Important Questions with Answers

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

ITI Introduction to Computing II

ITI Introduction to Computing II

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

1.00 Lecture 13. Inheritance

Compaq Interview Questions And Answers

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CLASSES AND OBJECTS. Fundamentals of Computer Science I

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Practice for Chapter 11

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Making New instances of Classes

core Advanced Object-Oriented Programming in Java

First IS-A Relationship: Inheritance

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Chapter 5 Object-Oriented Programming

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

More Relationships Between Classes

Data Structures (list, dictionary, tuples, sets, strings)

Programming Languages and Techniques (CIS120)

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CS260 Intro to Java & Android 03.Java Language Basics

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)


Transcription:

Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua

Objectives Implement encapsulation Implement inheritance including visibility modifiers and composition Implement polymorphism Override hashcode, equals, and tostring methods from Object class Create and use singleton classes and immutable classes Develop code that uses static keyword on initialize blocks, variables, methods, and classes

Encapsulation In Object Oriented Programming (OOP), data and associated behavior forms a single unit, which is referred to as a class. The term encapsulation refers to combining data and associated functions as a single unit. For example, in a Circle class, radius and center are defined as private fields. And you can add methods such as draw() and fillcolor() along with fields radius and center, since the fields and methods are closely related to each other. All the data (fields) required for the methods in the class are available inside the class itself. In other words, the class encapsulates its fields and methods together.

Access Modifiers Java supports four types of access modifiers: public private protected default (no access modifier specified) To illustrate the four types of access modifiers, let s assume that you have the following classes in a drawing application: Shape, Circle, Circles, and Canvas classes. The Canvas class is in appcanvas package and the other three classes are in graphicshape package

Example package graphicshape; class Shape { protected int color; package graphicshape; public class Circle extends Shape { private int radius; // private field public void area() { // public method // access to private field radius inside the class: System.out.println("area: " + 3.14 * radius * radius); // The fillcolor method has default access void fillcolor() { //access to protected field, in subclass: System.out.println("color: " + color);

Example package graphicshape; class Circles { void getarea() { Circle circle = new Circle(); // call to public method area() within package: circle.area(); // calling fillcolor() with default access in package: circle.fillcolor(); package appcanvas; import graphicshape.circle; class Canvas { void getarea() { Circle circle = new Circle(); circle.area(); // call to public method area()

Access Modifiers Access modifiers/ accessibility Within the same class Subclass inside the package Subclass outside the package Other class inside the package Other class outside the package public Yes Yes Yes Yes Yes protected Yes Yes Yes Yes No Default Yes Yes No Yes No private Yes No No No No

Inheritance Inheritance is a reusability mechanism in object-oriented programming. With inheritance, the common properties of various objects are exploited to form relationships with each other. The abstract and common properties are provided in the superclass, which is available to the more specialized subclasses. For example, a color printer and a black-and-white printer are kinds of a printer (single inheritance); an all-in-one printer is a printer, scanner, and photocopier (multiple inheritance).

Inheritance In the Java library, you can see extensive use of inheritance. The Number class abstracts various numerical (reference) types such as Byte, Integer, Float, Double, Short, BigInteger and BigDecimal.

Polymorphism Polymorphism can be of two forms: dynamic and static. When different forms of a single entity are resolved during runtime (late binding), such polymrphism is called dynamic polymorphism. Overriding is an example of runtime polymorphism. When different forms of a single entity are resolved at compile time (early binding), such polymorphism is called static polymorphism. Function overloading is an example of static polymorphism.

Polymorphism class Shape { public double area() { return 0; class Circle extends Shape { private int radius; public Circle(int r) { radius = r; public double area() { return Math.PI * radius * radius; class Square extends Shape { private int side; public Square(int a) { side = a; public double area() { return side * side;

Polymorphism public class TestShape { public static void main(string []args) { Shape shape1 = new Circle(10); System.out.println(shape1.area()); Shape shape2 = new Square(10); System.out.println(shape2.area()); This program illustrates how the area() method is called based on the dynamic type of the Shape. In this code, the statement shape1.area(); calls the Circle's area() method while the statement shape2.area(); calls Square's area() method and hence the result.

Method Overloading Let s implement a method in the Circle class called fillcolor() that fills a circle object with different colors. When you specify a color, you need use a coloring scheme, and let us consider two schemes RGB scheme and HSB scheme class Circle { // other members public void fillcolor (int red, int green, int blue) { /* color the circle using RGB color values */ public void fillcolor (float hue, float saturation, float brightness) { /* color the circle using HSB values */

Constructor Overloading A default constructor is useful for creating objects with a default initialization value. When you want to initialize the objects with different values in different instantiations, you can pass them as the arguments to constructors. In a class, the default constructor can initialize the object with default initial values, while another constructor can accept arguments that need to be used for object instantiation. public Circle(int x, int y, int r) { xpos = x; ypos = y; radius = r; public Circle(int x, int y) { xpos = x; ypos = y; radius = 10; // default radius public Circle() { xpos = 20; // assume some default values for xpos and ypos ypos = 20; radius = 10; // default radius

Overload Resolution public static void amethod (int val) { System.out.println ("int"); public static void amethod (short val) { System.out.println ("short"); public static void amethod (Object val) { System.out.println ("object"); public static void amethod (String val) { System.out.println ("String");

Example

Questions?

Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua