C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It...

30
C#/Java Classes ISYS 350

Transcript of C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It...

Page 1: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

C#/Java Classes

ISYS 350

Page 2: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Introduction to Classes

• A class is the blueprint for an object. – It describes a particular type of object.– It specifies the properties (fields) and methods a

particular type of object can have.– One or more object can be created from the class.– Each object created from a class is called an instance of the class.

Page 3: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Adding a Class to a C# Project

• Project/Add Class– Assigna meaningful name.

• Steps:– Adding properties• Property procedures: Set / Get• Or declare Public variables

– Adding methods– Adding events, exceptions

Page 4: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Class Code Example:Properties defined using Public variables

class emp

{

public string eid;

public string ename;

public double salary;

public double empTax()

{

return salary * .1;

}

Page 5: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Using a Class:Creating an instance of the class using

new --- default constructor

emp myEmp = new emp();myEmp.eid = "e1";myEmp.ename = "peter";myEmp.salary = 5000.00; MessageBox.Show(myEmp.empTax().ToString());

Page 6: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Other Constructor• When a class is created, its constructor is called. A

constructor has the same name as the class, and usually initialize the properties of the new object.

class emp { public emp(string EID, string ENAME, double SALARY) { eid = EID; ename = ENAME; salary = SALARY; } public string eid; public string ename; public double salary; public double empTax() { return salary * .1; } }

Page 7: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Creating Property with Property Procedures

• Implementing a property with a public variable the property value cannot be validated by the class.

• We can create read-only, write-only, or write-once properties with property procedure.

• Steps:– Declaring a private class variable to hold the property

value.– Writing a property procedure to provide the interface to

the property value.

Page 8: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

class emp2 { private string pvEID; private string pvEname; private double pvSalary; public string EID { get { return pvEID; } set { pvEID = value; } } public string Ename { get {return pvEname;} set {pvEname = value;} } public double Salary { get {return pvSalary;} set {pvSalary = value;} } public double empTax() { return Salary * .1; } }

Page 9: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

How the Property Procedure Works?

• When the program sets the property, the set property procedure is called and procedure code is executed. The value assigned to the property is passed in the value variable and is assigned to the hidden private variable.

• When the program reads the property, the get property procedure is called.

Page 10: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Anatomy of a Class Module

Class Module

Public Variables & Property Procedures

Public Procedures & Functions

Exposed Part

Private Variables

Private Procedures & Functions

Hidden Part

•Private variables and procedures can be created for internal use.•Encapsulation

Page 11: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Encapsulation

• Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So methods like getter and setter access it and the other classes access it through property procedure.

Page 12: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Property Procedure Code Example:Enforcing a maximum value for salary

public double Salary { get {return pvSalary;} set { if (value > 150000) { pvSalary = 150000; } else { pvSalary = value; } } }

Page 13: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Implementing a Read-Only Property:Declare the property with only the get procedure

public DateTime DateHire { get { return pvDateHire; } set { pvDateHire = value; } } public int yearsEmployed { get { return DateTime.Today.Year - DateHire.Year; } }

Using the property: cannot assign a value to yearsEmployedmyEmp2.DateHire = DateTime.Parse("7/4/2005");MessageBox.Show(myEmp2.yearsEmployed.ToString());

Page 14: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Overloading

A class may have more than one methods with the same name but a different argument list (with a different number of parameters or with parameters of different data type), different parameter signature.

Page 15: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Method Overloading Example

public double empTax()

{

return Salary * .1;

}

public double empTax(double sal)

{

return sal * .1;

}

Page 16: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Inheritance

• The process in which a new class can be based on an existing class, and will inherit that class’s interface and behaviors. The original class is known as the base class, super class, or parent class. The inherited class is called a subclass, a derived class, or a child class.

Page 17: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Employee Super Class with Three SubClasses

All employee subtypes will have emp nbr, name, address, and date-hired

Each employee subtype will also have its own attributes

Page 18: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Inheritance Exampleclass emp

{

public string eid;

public string ename;

public double salary;

public double empTax()

{

return salary * .1;

}

}

class secretary : emp

{

public double wordsPerMinute;

}

Page 19: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Method Override class emp { public string eid; public string ename; public double salary; public virtual double empTax() { return salary * .1; } } class secretary : emp { public double wordsPerMinute; public override double empTax() { return salary * .15; } }

Note: The keyword virtual allows a parent class method to be overridden.

Page 20: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Static Classes and methods

• Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.

• A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword.

• Example: Math class

Page 21: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example static class MyFunctions { static public double myPayment(double loan, double rate, double term) { double payment; payment = (loan * rate / 12) / (1 - Math.Pow(1 + rate / 12, -12 * term)); return payment; } static public Boolean myIsNumeric(string inputString) { try { double.Parse(inputString); return true; } catch { return false; } } }

Page 22: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Adding Class to a Java Web Project• Java classes used with a web application must be stored as a

“Package”.• Step 1: Create a new package

– Right click the Source Packages folder and select New/Java Package– Name the new package

• For example, myPackage

• Step 2: Creating new class in the package– Right click the package folder and select New/Java Class– Name the class

Page 23: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

empClass Example• Use a private variable to store a property’s value.

private String pvEID, pvEname, pvSalary;• Use set and get method to define a property:

public void setEID(String eid){pvEID=eid;

} public String getEID(){

return pvEID;}

Note: “void” indicates a method will not return a value.

Page 24: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Code Example: empClass

package MyPackage;public class empClass { private String pvEID; private String pvEname; private double pvSalary; public void setEID(String eid){

pvEID=eid;}

public String getEID(){return pvEID;}

public void setEname(String ename){pvEname=ename;}

public String getEname(){return pvEname;}

public void setEname(double salary){pvSalary=salary;}

public double getSalary(){return pvSalary;}

public double empTax() { return pvSalary * .1; } }

Page 25: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Constructor

• A java constructor has the same name as the name of the class to which it belongs.

• A constructor can be loverloaded• Example:

public empClass(){ } public empClass(String EID,String ENAME, double SALARY) { pvEID=EID; pvEname=ENAME; pvSalary=SALARY; }

Page 26: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

public class empClass { private String pvEID; private String pvEname; private double pvSalary; public empClass() { } public empClass(String EID,String ENAME, double SALARY) { pvEID=EID; pvEname=ENAME; pvSalary=SALARY; } public void setEID(String eid){

pvEID=eid;}public String getEID(){return pvEID;}

public void setEname(String ename){pvEname=ename;}

public String getEname(){return pvEname;}

public void setSalary(double salary){pvSalary=salary;}

public double getSalary(){}

public double empTax() { return pvSalary * .1; } }

Page 27: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Using Class

• Must import the package:• <%@page import="myPackage.*" %>

• Define a class variable:• empClassr E1 = new empClass();

Page 28: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example

<body> <% empClass E1 = new empClass(); E1.setEID("E1"); E1.setEname("Peter"); E1.setSalary(5000); out.println(E1.empTax()); empClass E2=new empClass("E2","Paul",6000); out.println(E2.empTax()); %> </body>

Page 29: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Java Class Inheritance:extends

public class secretary extends empClass{ private double pvWPM; public void setWPM(double wpm){

pvWPM=wpm;}

public double getWPM(){return pvWPM;}

}

Page 30: C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example

secretary S1 = new secretary(); S1.setEID("E3"); S1.setEname("Mary"); S1.setSalary(4500); out.println(S1.empTax());