lesson week1

Upload: cristy-nazareno

Post on 08-Mar-2016

217 views

Category:

Documents


0 download

DESCRIPTION

Java programming

TRANSCRIPT

CLASSES & OBJECTS

CLASSES & OBJECTSby: maria cristina b. nazarenoJanuary 12, 2016created by: ma. cristina b. nazareno1INTRODUCTIONJava is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:PolymorphismInheritanceEncapsulationAbstractionClasses

ObjectsInstanceMethodMessage Parsing

January 12, 2016created by: ma. cristina b. nazareno2WHAT ARE OBJECTS?What are objects and what are not objects?Objects: bicycle, book, lamp, song, meetingNon-objects: green, 30% of all pencils, largeIf you can touch it, name it, or talk about it, it is likely to be an object.Objects can be physical things or conceptual.Humans seem to want to think in terms of objects and their relationships with each other.In Java, an object is a specific, concrete instance of a class.January 12, 2016created by: ma. cristina b. nazareno3INTRODUCTION: In OOP subject you learned that a class is the template for an object and that a class is a way to encapsulate both data (called fields in Java) and the functions (called methods) that operate on that data. You also learned about inheritance, which enables a class (called the subclass) to inherit the capabilities of a base class (called a superclass in Java). Finally, you discovered that polymorphism enables you to create virtual methods that can be implemented differently in derived classes. In this lesson, you'll apply what you know about object-oriented programming towards creating Java classes.3WHAT IS A CLASS?A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.

January 12, 2016created by: ma. cristina b. nazareno4In object-oriented programming we describe types of objects by defining classes.4CLASS DEFINITIONA Java class definition containsFields - what properties an object hasThe values assigned to the fields define the state of the object. (e.g., the car is painted silver, has a half a tank of gas, and is stopped.)Methods - what behaviors (actions) an object can performTypically these actions supply or modify its state.January 12, 2016created by: ma. cristina b. nazareno5CLASS DEFINITIONYou define a class by using the class keyword along with the class name, like this:

class MyClass{

}.January 12, 2016created by: ma. cristina b. nazareno6As I said, a class is sort of a template for an object. In this way, a class is equivalent to a data type such as int. The main difference is that Java already knows what an integer is. However, when you create a class, you must tell Java about the class's characteristics. Believe it or not, these are a complete Java class. As you can see, the class definition begins with the keyword class followed by the name of the class. The body of the class is marked off by curly braces just like any other program block. In this case, the class's body is empty. Because its body is empty, this example class doesn't do anything. You can, however, compile the class and even create an object from it.

6CLASS DEFINITIONTo create an object from a class, you type the class's name followed by the name of the object. For example, the line below creates an object from the MyClass class:MyClass myObject = new MyClass();Class NameName of an objectDynamically create object using newAutomatically calls the constructorJanuary 12, 2016created by: ma. cristina b. nazareno77DECLARING MEMBER VARIABLESA class can contain any of the following variable types.LOCAL VARIABLES: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.INSTANCE VARIABLES: Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. CLASS VARIABLES: Class variables are variables declared with in a class, outside any method, with the static keyword.PARAMETER VARIABLES: A parameter variable is used to store information that is being passed from the location of the method call into the method that is called.January 12, 2016created by: ma. cristina b. nazareno88ASSIGNMENTWhat is/are the differences between static and non-static variables?January 12, 2016created by: ma. cristina b. nazareno9INSTANCE METHODS VS STATIC METHODSSTATIC METHODSNON-STATIC (INSTANCE) METHODSdeclared with the keyword staticare declared without the keyword staticWhen you use a static field or method, you do not use an objectWhen you use a nonstatic field or method, you must use an objectStatic methods in a class are called class methods.Nonstatic methods in a class are called instance methods.When you create a class with a static field and instantiate 100 objects, only one copy of that field exists in memoryWhen you create a class with a nonstatic field and instantiate 100 objects, then 100 copies of that field exist in memory.When you create a static method in a class and instantiate 100 objects, only one copy of the method exists in memory and the method does not receive a this reference.When you create a nonstatic method in a class and instantiate 100 objects, only one copy of the method exists in memory, but the method receives a this reference that contains the address of the object currently using it.Static class variables are not instance variables. The system allocates memory to hold class variables once per class, no matter how many instances of the class you instantiate. The system allocates memory for class variables the first time it encounters a class, and every instance of a class shares the same copy of any static class variables.Instance fields and methods are nonstatic. The system allocates a separate memory location for each nonstatic field in each instance.January 12, 2016created by: ma. cristina b. nazareno10ACCESSORS & CONSTRUCTORSTo achieve encapsulation in JavaDeclare the variables of a class as private.Provide public setter and getter methods (this is also known as accessors or mutators) to modify and view the variables values.

January 12, 2016created by: ma. cristina b. nazareno11ACCESSORSExample: Instance ClassJanuary 12, 2016created by: ma. cristina b. nazareno12 public void setAge( int newAge){ age = newAge; }

public void setName(String newName){ name = newName; }

public void setIdNum( String newId){ idNum = newId; }}

/* File name : EncapTest.java */public class EncapTest{ private String name; private String idNum; private int age;

public int getAge(){ return age; } public String getName(){ return name; } public String getIdNum(){ return idNum; }Example: Application ClassJanuary 12, 2016created by: ma. cristina b. nazareno13/* File name : RunEncap.java */public class RunEncap{ public static void main(String args[]){ EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms");

System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); }}Name : James Age : 20Benefits of EncapsulationThe fields of a class can be made read-only or write-only.A class can have total control over what is stored in its fields.The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.January 12, 2016created by: ma. cristina b. nazareno1414