OOP Basics Classes & Methods (c) IDMS/SQL News

Download Report

Transcript OOP Basics Classes & Methods (c) IDMS/SQL News

OOP Basics
Classes & Methods
(c) IDMS/SQL News
http://www.geocities.com/idmssql
OOP
 In many OOP courses and books, as soon as
they start OOP discussion, they get out of
EDP world and jump to some ‘real world’!
 - Sending flowers to another person (Budd 2000),
taking an elevator (Java in 21 Days)...
 And you see a variety of confusing
definitions – right from Booch (1994)
 We stick to the EDP world and terms!
Object Oriented Programming
(c)http://www.geocities.com/idmssql
2
OOP
 OOP is the design and implementation of
programs in terms of classes and objects.
 A class defines the methods and the types of data
(variables) associated with the object
 An object is an instance of a class
 From a programmer’s viewpoint, what we get is
an abstract data type
ie: class can be used as if it is a datatype! This new
‘datatype’ has methods and variables associated
with it!
Object Oriented Programming
(c)http://www.geocities.com/idmssql
3
Class
 A class consists of
– a collection of methods representing the
behavior of an object
– a collection of instance variables to represent
the state of an object
 scope class ClassName [extends OldClass] { // Class
implementation }
eg: - public class Greeting{ . . .}
- public class HelloWorld2 extends Applet { . . .}
Object Oriented Programming
(c)http://www.geocities.com/idmssql
4
Methods
 A class contains methods
 All methods follow similar syntax:
scope return-type method-name(arguments)
{
statements
}
eg: public static void main (String args[]) {... }
public float FindArea(int radius){
return 3.14f*radius*radius;}
Object Oriented Programming
(c)http://www.geocities.com/idmssql
5
Methods . . .scope
• - public - the method is accessible by any system
•
•
•
•
•
object.
- protected - is only accessible by subclasses and
the class in which it is declared.
- private - accessible only within current class.
- fina1 - cannot be overridden by any subclass.
- static - is shared by all instances of the class.
If a method is not given a scope, it is only
accessible within the scope of the current file.
Object Oriented Programming
(c)http://www.geocities.com/idmssql
6
Variables... a recap
• double salary = 2534.50 ;// instance variable
unique to each object created
usage is obj1.name syntax
• static int counter1; // class variable
usage is class.name syntax
Object Oriented Programming
(c)http://www.geocities.com/idmssql
7
Variables of class type. . .
 A variable of a class type holds a reference to an
object. (unlike a variable of primitive datatype)
 It doesn’t hold the object itself.
 Instance objects can be created using:
MyClass myObj1 = new MyClass();
 Assignment means storing a reference to a different
object.
MyClass myObj2 = myObj1;
myObj1
myObj2
An instance
of MyClass
Object Oriented Programming
(c)http://www.geocities.com/idmssql
8
Object as a parameter
-Parameters to methods are primitive datatypes ...
-Parameters can also be objects
Suppose you want to pass a MyClass object as a parameter
to a method:
void someMethod(MyClass obj1) {
........
}
A parameter variable can be declared as normal.
Object Oriented Programming
(c)http://www.geocities.com/idmssql
9
OO Language must support
the three very important concepts:
- encapsulation,
- inheritance,
- polymorphism.
Object Oriented Programming
(c)http://www.geocities.com/idmssql
10
Encapsulation
 Encapsulation is the process of defining the
class and its implementation using methods
 Data in an object must be accessed via
methods defined on that object
 Interface is the external view of a method
 Actual implementation is hidden and can be
changed, but the interface is unchanged
 From the external view, an object is an
encapsulated
Object Oriented Programming
(c)http://www.geocities.com/idmssql
11
Encapsulation . .
 The access modifiers* public and private
are how encapsulation is enforced.
 Good practice states:
– Instance variables are always private.
– Only a minimal number of methods should be
made public.
* Some documents use the term “visibility modifiers”
Object Oriented Programming
(c)http://www.geocities.com/idmssql
12
Inheritance
 A class can normally inherit the state and behavior
of another class
 The original class is often called the superclass,
and the new class is often called the subclass.
Inheritance is often referred to as extending the
superclass.
 In Java all classes inherit from a root class called
Object. Methods defined on this Object class can
be used or be overridden by user classes.
Object Oriented Programming
(c)http://www.geocities.com/idmssql
13
Inheritance tree
 Inheritance is created by the keyword
extends..
 class HelloWorld {
java.lang.Object
really means
class HelloWorld
extends java.lang.Object {
Everything
else
(default inheritance)
class Snowman extends
Applet{
Object Oriented Programming
(c)http://www.geocities.com/idmssql
14
Polymorphism
 polymorphic means “having many forms“.
 Polymorphism will be discussed later in the
course
Object Oriented Programming
(c)http://www.geocities.com/idmssql
15
Simple Application- Recap
/**
* HelloWorld.java
*/
Class name (MUST match file name)
Main method (starting point)
public class HelloWorld {
public static void main(String arguments[]) // Where the program starts
{
System.out.println(“Hello World");
// Print out message
}
}
Braces ‘{}’start & end of class and methods
Object Oriented Programming
(c)http://www.geocities.com/idmssql
16
Where is the object here?
 The above code is procedural ... Top to
bottom ... We don’t get to see any instances
of objects here...
 Java HelloWorld - will start executing the
main method always.
 To see the real objects we need to split the
HelloWorld into 2 programs!
Object Oriented Programming
(c)http://www.geocities.com/idmssql
17
Modified Program
We create two programs 1) Greeting 2)TestGreeting
public class Greeting{
Greeting(){ } // default constructor, does nothing here
public void greet(String whom)
{System.out.println("Hello " + " " + whom);}
}
Note: - there is no main method here
- This method has to be called from another program
- It takes one argument = whom to greet
You cannot run this as Java Greeting
So let’s have another main class
Object Oriented Programming
(c)http://www.geocities.com/idmssql
18
The Main ..
public class TestGreeting{
/** the main method follows */
public static void main(String[] args) {
Greeting hello1 = new Greeting(); // instantiate
hello1.greet("World"); // call the method with an argument
}//end of method
}
Note that several important concepts are present here
Try Java TestGreeting
Object Oriented Programming
(c)http://www.geocities.com/idmssql
19
The main method
 The main method gets control when we start the
program
 Every ‘main’ program must have a main method...
(callable from JVM)
 The main method is declared static – means it
belongs to the class and no instance of the class is
needed to execute this.
 arguments to main is String array
 main method is usually short (as a sort of hoved
section in our COBOL ..)
Object Oriented Programming
(c)http://www.geocities.com/idmssql
20
Greeting hello1 = new Greeting();
 What happens here?
 Tells the JVM to construct an object ‘hello1’ of
the class Greeting.
 Greeting hello1 ; // just like defining variables
 hello1 = new Greeting(); // this is also a valid
way
 It will execute the default code in Greeting to do
any initialization needed
 It will not execute any method within Greeting
 Greeting could have taken some parameters (we
have none)
Object Oriented Programming
(c)http://www.geocities.com/idmssql
21
Constructor
 Constructor has the same name as the Class
 But is defined like a method ... Well, almost
 Constructor returns nothing, not even void
 If not coded, a default constructor is assumed
 Constructor code is executed when an object is
instantiated...
 ie when we say Greeting x1 = new Greeting();
 A class can have more than one Constructor
Object Oriented Programming
(c)http://www.geocities.com/idmssql
22
Constructor
 public class Greeting{
 Greeting(){ } // default constructor
NB: Same name !
Let’s modify the code and put a proper
constructor with parameters
Object Oriented Programming
(c)http://www.geocities.com/idmssql
23
Complete Code with some extras
public class TestGreeting{
/** the main method follows */
public static void main(String[] args) {
Greeting hello1 = new Greeting("Hello"); // instantiate
hello1.greet("World"); // call the method with an argument
Greeting hello2 = new Greeting("God Dag");
hello2.greet("Verden"); } }
// definition of the other class follows
class Greeting{
private String hilsen ; // instance variable is declared private
Greeting(String arg1) {
hilsen = arg1;}
//System.out.println( "in Constructor "); }
// real method follows
public void greet(String whom)
{System.out.println (hilsen + " " + whom);
}}
Note: One can put theObject
source
2 files or 1 file
Orientedin
Programming
(c)http://www.geocities.com/idmssql
24
A simpler example of true oop
public class Greeting3{
String hilsen ="Hello";
public void greet(String whom)
{System.out.println (hilsen + " " + whom);}
/** the main method follows */
public static void main(String[] args) {
Greeting3 hello1 = new Greeting3(); // instantiate
hello1.greet("World"); // call the method
}}
Object Oriented Programming
(c)http://www.geocities.com/idmssql
25