Download presentation
Presentation is loading. Please wait.
Published byVictoria Coby Modified over 9 years ago
1
Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1
2
Lilian Blot Overview Instance Attributes & constants Access Level Modifier Constructors Methods Method overloading & method’s signature First Attempt at Inheritance Spring 2014 TPOP 2
3
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
4
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).
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
Lilian Blot Exercises Rewrite part of your Library program in Java Spring 2014 TPOP 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.