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.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

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.
Written by: Dr. JJ Shepherd
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Road Map Introduction to object oriented programming. Classes
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java Classes ISYS 350. Introduction to Classes Two basic uses of class: – 1. A class is a way to organize functions (methods) to perform calculations.
Database Handling Classes ISYS 475. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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,
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Inheritance Basics Programming with Inheritance
Java Programming Language
Interfaces.
Computer Programming with JAVA
Java Inheritance.
CIS 199 Final Review.
VB Classes ISYS 512.
Chapter 5 Classes.
Presentation transcript:

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 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.

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

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; }

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 = ; MessageBox.Show(myEmp.empTax().ToString());

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; }

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.

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; }

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.

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

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.

Property Procedure Code Example: Enforcing a maximum value for salary public double Salary { get {return pvSalary;} set { if (value > ) { pvSalary = ; } else { pvSalary = value; }

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 yearsEmployed myEmp2.DateHire = DateTime.Parse("7/4/2005"); MessageBox.Show(myEmp2.yearsEmployed.ToString());

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.

Method Overloading Example public double empTax() { return Salary *.1; } public double empTax(double sal) { return sal *.1; }

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.

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

Inheritance Example class emp { public string eid; public string ename; public double salary; public double empTax() { return salary *.1; } class secretary : emp { public double wordsPerMinute; }

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.

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

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; }

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

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.

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; }

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; }

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; }

Using Class Must import the package: import="myPackage.*" % Define a class variable: empClassr E1 = new empClass();

Example <% 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()); %>

Java Class Inheritance: extends public class secretary extends empClass{ private double pvWPM; public void setWPM(double wpm){ pvWPM=wpm; } public double getWPM(){ return pvWPM; }

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