Title: OOP Basics Classes
1OOP Basics Classes Methods
- (c) IDMS/SQL News http//www.geocities.com/idmssq
l
2OOP
- 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!
3OOP
- 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 programmers 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!
4Class
- 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 . . .
5Methods
- 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.14fradiusradius
6Methods . . .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.
7Variables... 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
-
8Variables of class type. . .
- A variable of a class type holds a reference to
an object. (unlike a variable of primitive
datatype) - It doesnt 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
An instance of MyClass
myObj1
myObj2
9Object 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.
-
10OO Language must support
- the three very important concepts
- - encapsulation,
- - inheritance,
- - polymorphism.
11Encapsulation
- 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
12Encapsulation . .
- 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
13Inheritance
- 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.
14Inheritance tree
- Inheritance is created by the keyword extends..
- class HelloWorld
- really means
- class HelloWorld
- extends java.lang.Object
- (default inheritance)
- class Snowman extends Applet
15Polymorphism
- polymorphic means having many forms.
- Polymorphism will be discussed later in the course
16Simple Application- Recap
/ HelloWorld.java / public class
HelloWorld public static void main(String
arguments) // Where the program starts
System.out.println(Hello World") // Print
out message
Class name (MUST match file name)
Main method (starting point)
Braces start end of class and methods
17Where is the object here?
- The above code is procedural ... Top to bottom
... We dont 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!
18Modified 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 lets have another main class
19The 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
20The 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 ..)
21Greeting 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)
22Constructor
- 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
23Constructor
- public class Greeting
- Greeting() // default constructor
- Lets modify the code and put a proper
constructor with parameters
NB Same name !
24Complete 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 the source in 2 files or 1 file
25A 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