Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Road Map Introduction to object oriented programming. Classes
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Lilian Blot Announcements The TPOP problem class this afternoon:  group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form  week 9.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programming in Java CSCI-2220 Object Oriented Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Interfaces and Inner Classes
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Improving structure with inheritance 3.0. The media project stores details about CDs and DVDs –CD: title, artist, number of tracks, playing time, got-it,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Abstract classes and interfaces
Software Construction
Objects as a programming concept
Inheritance and Polymorphism
CSC240 Computer Science III
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Abstract classes and interfaces
Object Oriented Programming in java
Abstract classes and interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 5 Classes.
Presentation transcript:

Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1

Lilian Blot Overview Instance Attributes & constants Access Level Modifier Constructors Methods Method overloading & method’s signature First Attempt at Inheritance Spring 2014 TPOP 2

Lilian Blot Instance Attributes What does public/private means?  Access modifiers in Java  a 3 rd one exists: protected One public class per file Instance attributes are declared outside the constructor. Spring 2014 TPOP 3 public class Item { private String title, UID; private Member member; private boolean status; … } Java Code

Lilian Blot Access Level Modifier modifierClassPackageSubclassWorld public YYYY protected YYYN no modifierYYNN private YNNN Spring 2014 TPOP 4 Access Level Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:  At the top level—public, or package-private (no explicit modifier).  At the member level—public, private, protected, or package-private (no explicit modifier).

Lilian Blot Tips on Choosing an Access Level If other programmers use your class, you want to ensure that errors from misuse cannot happen.  Access levels can help you do this. Use the most restrictive access level that makes sense for a particular member.  Use private unless you have a good reason not to. Avoid public fields except for constants.  Public fields tend to link you to a particular implementation  limit your flexibility in changing your code. Spring 2014 TPOP 5

Lilian Blot Constants Keyword final to create a constant  must be declared and initialised in same statement  convention: use upper case, word separated by underscores Keyword static for declaring a class constant/variable  called using name of class, e.g. Item.ON_LOAN. Spring 2014 TPOP 6 public class Item { // Declaration of constants (keyword final) public static final boolean AVAILABLE = true; public static final boolean ON_LOAN = false; public static final int PRINT_ALL = 1; private String title, UID; private Member member; private boolean status; Java Code

Lilian Blot Constructor The constructor is declared using the name of the class, followed by zero, one or more parameters  no need to have a self parameter  this is the equivalent of self in Python Spring 2014 TPOP 7 public class Item { … private String title, UID; … // the def __init__(self,...) in python public Item(String title, String UID){ this.title = title; // is the equivalent of in Python this.UID = UID; this.member = null; this.status = AVAILABLE; } Java Code

Lilian Blot Constructor Spring 2014 TPOP 8 public class Item { private String title, UID; // the def __init__(self,...) in python public Item(String title, String UID){ this.title = title; // is the equivalent of in Python this.UID = UID; this.member = null; this.status = AVAILABLE; } Java Code class Item(object): # the def __init__(self,...) in python def __init__(self, title, UID){ self.title = title; self.UID = UID; self.member = None; self.status = AVAILABLE; Python Code

Lilian Blot toString( ) method For a non-void function/method we must declare the type of the returned value  we MUST use the return statement Spring 2014 TPOP 9 public class Item { … public Item(String title, String UID){ … } public String toString(){ return ("<Item: uid = " + this.UID + ", " + this.title + ", available= "+this.status+", borrower= "+this.member+'>'); } Java Code Type of the returned value non-void function/method MUST use return statement method’s name

Lilian Blot print( ) method For a void function/method we must declare the type of the returned value as void.  No return statement needed  the return; statement could be used to exit the method Spring 2014 TPOP 10 public class Item { … public Item(String title, String UID){ … } public void print(int option){ if(…){ … } … Java Code Use keyword void to declare a void function/method method’s name

Lilian Blot Calling methods Spring 2014 TPOP public class Item {... public static void main(String[] args){ Item anItem = new Item(“The Joy of programming”,”book1001A”); anItem.print(Item.PRINT_ALL); System.out.println(anItem.toString()); // Equivalent to: System.out.println(anItem); } Java Code Method’s name Constructor’s nameKeyword to create a new object Object’s name Implicit call to the toString() method 11

Lilian Blot Method Signature In the class Item, we have two methods:  public void print() {…}  public void print(int option){…} It is authorised in Java (not in python) Java use method signature to differentiate them  the signature is composed of the name of the method  the number, type of parameters, and their order  NOT the returned type Nor the modifier Nor the parameter’s name  e.g. print() and print(int). This is called method overloading.  constructors can be overloaded Spring 2014 TPOP 12

Lilian Blot First Attempt at Inheritance Can only inherit from one super class  we MUST call a constructor from the super class  use of keywords extends and super. There is more, but not for now! Spring 2014 TPOP 13 public class DVD extends Item{ … public DVD(String title, String UID, String director){ super(title, UID); // calling the constructor Item(String, String) from the // super class Item // MUST be the first line of code in the DVD's constructor this.director = director; } Java Code Keyword extends to indicate inheritance MUST call a constructor from super class Name of super class

Lilian Blot Summary Now you should be able to create your own classes  declare instance attributes  declare class constants  declare constructors and methods  method signature and overloading Understand Access Level Modifier in Java Use inheritance  call constructors from super class Spring 2014 TPOP 14

Lilian Blot Exercises Rewrite part of your Library program in Java Spring 2014 TPOP 15