0% found this document useful (0 votes)
15 views

Classes

The document discusses classes in Java. It explains that everything in Java is class-based and object-oriented. A class defines the template for a new type of object, including its fields, constructors, and methods. Fields store an object's data, constructors initialize new object instances, and methods allow communication with the object. The document provides examples of defining a class and its components like fields, constructors, and methods. It also demonstrates how to create an instance of a class.

Uploaded by

Yasmine Ghazlane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Classes

The document discusses classes in Java. It explains that everything in Java is class-based and object-oriented. A class defines the template for a new type of object, including its fields, constructors, and methods. Fields store an object's data, constructors initialize new object instances, and methods allow communication with the object. The document provides examples of defining a class and its components like fields, constructors, and methods. It also demonstrates how to create an instance of a class.

Uploaded by

Yasmine Ghazlane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

11/30/2020

Classes

Brandon Krakowsky

Classes

Property of Penn Engineering |

Classes
• Everything in Java is object‐oriented and class‐based
‐ This means you have to create at least one class to write a Java program
• A class describes an object
‐ It’s like a template for a new kind of object
‐ When you define a class, you’re defining a new data type
• To use the object, you create an instance of the class
‐ It’s a very similar concept in Python
• A class includes:
‐ Fields (instance variables) that hold the data for each object
‐ Constructors that describe how to create a new object instance of the class
‐ Methods that describe the actions the object can perform

Property of Penn Engineering |

3
11/30/2020

Defining a Class
• Here’s simple syntax for defining a sample class:

public class ClassName {


// The fields (instance variables) of the object
// The constructors for creating the object
// The methods for communicating with the object
}
• public is an access modifier that defines the visibility of the class
‐ public means any other program in the Java project can use the class (i.e., create instances or
call methods)
‐ We’ll talk about other access modifiers later in the course
• Things in a class can be in any order

Property of Penn Engineering |

Defining Fields in a Class


• An object’s data is stored in fields (instance variables)
‐ The fields describe the state of the object
‐ Fields are defined as variable declarations in the class
• Sample class definition with instance variables:

public class ClassName {


// The fields (instance variables) of the object
String name; //declaration to store a String in the object,
defaults to null
double health; //declaration to store a double in the object
int age = 0; //declaration to store an int in the object, initially
set to 0
}
• Fields are available throughout the entire class that declares them

Property of Penn Engineering |

Defining a Constructor for a Class


• A constructor is code to create an object
• The syntax for a constructor is:

public ClassName(parameters) {
//code using parameters to set up initial state of object
}

• public means the constructor is accessible by any other program in the Java project
• ClassName has to be the same name as the class that the constructor occurs in
• The constructor parameters are a comma‐separated list of variable declarations

Property of Penn Engineering |

6
11/30/2020

Defining a Constructor for a Class


• Sample class definition with constructor:
public class ClassName {
// The fields (instance variables) of the object
String name; //declaration to store a String in the object,
defaults to null
double health; //declaration to store a double in the object
int age = 0; //declaration to store an int in the object, initially
set to 0
// The constructor for creating the object
public ClassName(parameters) {
//code using parameters to set up initial state of object
}
}

Property of Penn Engineering |

Defining a Method in a Class


• A method is a function in an object that allows you to use and communicate with that object
• The syntax for a method is:

return‐type methodName(parameters) {
// locally defined variables
// code using parameters
}
• If a method is to return a result, return‐type is the data type of the result
‐ You must use a return statement to exit the method with a result of the correct type
• If a method doesn’t return a result, return‐type is void
‐ This indicates that a method doesn’t return a value
‐ In this case, you don’t need to use a return statement to exit the method

Property of Penn Engineering |

Defining a Method in a Class


• Sample class definition with a method:
public class ClassName {
// The fields (instance variables) of the object
String name; //declaration to store a String in the object, defaults to null
double health; //declaration to store a double in the object
int age = 0; //declaration to store an int in the object, initially set to 0

// The constructor for creating the object


public ClassName(parameters) {
//code using parameters to set up initial state of object
}

// A method for communicating with the object


String getName(parameters) {
//returns value of “name” instance variable
//”this” refers to this instance of the class (ClassName)
return this.name;
}
}

Property of Penn Engineering |

9
11/30/2020

Creating an Instance of a Class


• To use a class, you create an instance of the object by calling its constructor and using the keyword
new
• Here’s syntax to define a class and to create an instance:

public class ClassName {

public ClassName(par1, … parN) {


//code using parameters to set up initial state of object
}

public static void main(String[] args) {


//create instance of ClassName
ClassName c = new ClassName(arg1, …, argN);
}
}
• new creates a new instance of the object

Property of Penn Engineering |

10

Dog Project

Property of Penn Engineering |

11

Dog Project
• In Eclipse, go to “File”  “New”  “Project”

• Provide a Project name


‐ Project names should be capitalized

‐ Use the default location

Use the default JRE and project layout

• Click “Next”

Property of Penn Engineering |

12
11/30/2020

Dog Project
• Define the compilation/build settings

• Make sure Create module‐info.java file


IS NOT checked

• Use the default output folder

• Click “Finish”

Property of Penn Engineering |

13

Dog Class
• In Eclipse, go to “File”  “New”  “Class”
Provide a Package name
• ‐ Package names should not be capitalized

Provide a Class name


‐ Class names should be capitalized

Make sure public static void main(String[] args)


IS checked

Make sure Inherited abstract methods


IS NOT checked

Click “Finish”

Property of Penn Engineering |

14

Dog Class
• The entry point of your Dog program is the main method

Property of Penn Engineering |

15
11/30/2020

Dog Class – Instance Variables


• Add javadocs to your classes, variables, and methods as you write your program
• Create some attributes (instance variables) for your Dog

Property of Penn Engineering |

16

Dog Class – Instance Variables


• Add javadocs to your classes, variables, and methods as you write your program
• Create some attributes (instance variables) for your Dog
‐ The Owner data type doesn’t exist yet. We’ll create it as another class soon.

Property of Penn Engineering |

17

Dog Class ‐ Constructor


• Create a constructor for your object
‐ Again, the Owner data type doesn’t exist yet.

Property of Penn Engineering |

18
11/30/2020

Owner Class
• Create a new class Owner
‐ This will represent a Dog owner

Property of Penn Engineering |

19

Owner Class – Constructor & Methods


• Create a constructor for your object and other methods

Property of Penn Engineering |

20

Dog Class ‐ Methods


• The eat method

Property of Penn Engineering |

21
11/30/2020

Dog Class ‐ Methods


• The haveBirthday method

Property of Penn Engineering |

22

Dog Class ‐ Methods


• The bark method

Property of Penn Engineering |

23

Dog Class ‐ Methods


• The getDogInfo method

Property of Penn Engineering |

24
11/30/2020

Dog Class ‐ Methods


• Other methods

Property of Penn Engineering |

25

Dog Class – main Method


• Create instances of the Owner and Dog classes in your main method

Property of Penn Engineering |

26

Dog Class – main Method

Property of Penn Engineering |

27
11/30/2020

Dog Class – main Method

Property of Penn Engineering |

28

Dog Class – main Method

Property of Penn Engineering |

29

Banking Project

Property of Penn Engineering |

30
11/30/2020

Banking Project
• In Eclipse, create a new “Banking” project
• Create 3 classes:
‐ Bank
‐ Provide the package name “banking”
‐ Make sure public static void main(String[] args) IS checked
‐ BankAccount
‐ Provide the package name “banking”
‐ Make sure public static void main(String[] args) IS NOT checked
‐ Customer
‐ Provide the package name “banking”
‐ Make sure public static void main(String[ ] args) IS NOT checked

Property of Penn Engineering |

31

Bank Class

Property of Penn Engineering |

32

BankAccount Class

Property of Penn Engineering |

33
11/30/2020

Customer Class

Property of Penn Engineering |

34

Customer Class

Property of Penn Engineering |

35

Customer Class

Property of Penn Engineering |

36
11/30/2020

BankAccount Class

Property of Penn Engineering |

37

BankAccount Class

Property of Penn Engineering |

38

BankAccount Class

Property of Penn Engineering |

39
11/30/2020

Bank Class

Property of Penn Engineering |

40

Bank Class

Property of Penn Engineering |

41

Bank Class

Property of Penn Engineering |

42
11/30/2020

Bank Class

Property of Penn Engineering |

43

Bank Class

Property of Penn Engineering |

44

You might also like