OOP Basics Classes - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

OOP Basics Classes

Description:

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 ... – PowerPoint PPT presentation

Number of Views:236
Avg rating:3.0/5.0
Slides: 26
Provided by: DonM72
Category:
Tags: oop | basics | classes | idms

less

Transcript and Presenter's Notes

Title: OOP Basics Classes


1
OOP Basics Classes Methods
  • (c) IDMS/SQL News http//www.geocities.com/idmssq
    l

2
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!

3
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 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!

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

5
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.14fradiusradius

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

7
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

8
Variables 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
9
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.

10
OO Language must support
  • the three very important concepts
  • - encapsulation,
  • - inheritance,
  • - polymorphism.

11
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

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

14
Inheritance tree
  • Inheritance is created by the keyword extends..
  • class HelloWorld
  • really means
  • class HelloWorld
  • extends java.lang.Object
  • (default inheritance)
  • class Snowman extends Applet

15
Polymorphism
  • polymorphic means having many forms.
  • Polymorphism will be discussed later in the course

16
Simple 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
17
Where 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!

18
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 lets have another main class

19
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

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

21
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)

22
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

23
Constructor
  • public class Greeting
  • Greeting() // default constructor
  • Lets modify the code and put a proper
    constructor with parameters

NB Same name !
24
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 the source in 2 files or 1 file
25
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
Write a Comment
User Comments (0)
About PowerShow.com