Download presentation
Presentation is loading. Please wait.
1
Written by: Dr. JJ Shepherd
CSCE 146 Hey Remember Java? Written by: Dr. JJ Shepherd
2
Classes Blueprints to create objects Remember the steps
Define the class Instance variables Constructors Accessors and Mutators Additional Methods
3
Classes Defining the class Concept public class <<name>> {
<<body of the class>> }
4
Classes Instance variables are the attributes of the class Concept
public class <<name>> { private <<type>> <<name>>; … }
5
Classes Constructors constructs an instance of a class in memory and sets the instance variables to a value Default constructors have no parameters and set everything to a default value public <<name of the class>>() { <<set all instance variables>> }
6
Classes Parameterized Constructors have parameters that set one or all of the instance variables to a value Concept public <<name of the class>>(<<parameters>>) { <<set instance variables to parameters>> }
7
Classes Mutators are used to modify instance variables in a class
Adds a layer of protection by validating values Concept public void <<set variable name>>(<<parameter>>) { this.<<variable>> = <<parameter>>; }
8
Classes Accessors are used to get values of instance variables outside of the class Concept public <<type>> <<get variable name>>() { return this.<<variable>>; }
9
Classes Methods are behaviors of classes
Used internally and externally Accessors and mutators are methods Concept <<scope>> <<return value>> <<name>> (<<parameters>>) { <<body of the method>> }
10
Classes Static methods are static in memory
Used as helper methods that exist outside of one particular instance <<scope>> static <<return value>> <<name>> (<<parameters>>) { <<body of the static method>> }
11
Inheritance Used to take the attributes and methods from a parent (super) class Concept public class <<name>> extends <<parent name>> { <<body of the class>> }
12
Inheritance In inherited constructors need to all the parent’s constructor by calling “super” Concept To call inherited methods use “super.” super(<<parameters>>); super.<<method name>>;
13
Interfaces Used as a blueprint to create classes. Classes are a blueprint to create instance of objects Only has method definitions Concept public interface <<name>> { <<method definition>>; … }
14
Interfaces Interfaces are the building concept of polymorphism
To use an interface used “implements” Concept public class <<name>> implements <<interface name>> { <<body of the class>> }
15
Exceptions Gracefully allows programs to crash and an exceptional event happens To create exceptions extend the/an exception class Then call the parent’s constructors
16
Exceptions Concept public <<name>> extends <<Exception>> { public <<name>>(<<parameters>>) super(<<parameters>>) }
17
Exceptions Methods that can throw exceptions need to check for those cases Concept <<scope>> <<return>> <<name>> (<<parameters>>) throws <<Exceptions>> { … throw new <<exception name>>(); }
18
Exceptions If a method throws an exception it must be called in a try catch block that handles the exception Concept try { <<method that throws exception>>; } catch(<<exception name>> e) System.out.println(e.getMessage());
19
Example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.