Download presentation
Presentation is loading. Please wait.
1
OOP Basics Classes & Methods (c) IDMS/SQL News http://www.geocities.com/idmssql
2
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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 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!
4
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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.14f*radius*radius;}
6
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 8 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 An instance of MyClass myObj2
9
Object Oriented Programming (c)http://www.geocities.com/idmssql 9 -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 as a parameter
10
Object Oriented Programming (c)http://www.geocities.com/idmssql 10 OO Language must support the three very important concepts: - encapsulation, - inheritance, - polymorphism.
11
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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{ Everything else Everything else java.lang.Object
15
Object Oriented Programming (c)http://www.geocities.com/idmssql 15 Polymorphism ï polymorphic means “having many forms“. ñ P olymorphism will be discussed later in the course
16
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 17 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!
18
Object Oriented Programming (c)http://www.geocities.com/idmssql 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 let’s have another main class
19
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 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
Object Oriented Programming (c)http://www.geocities.com/idmssql 23 Constructor ï public class Greeting{ ï Greeting(){ } // default constructor Let’s modify the code and put a proper constructor with parameters NB: Same name !
24
Object Oriented Programming (c)http://www.geocities.com/idmssql 24 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); }} Complete Code with some extras Note: One can put the source in 2 files or 1 file
25
Object Oriented Programming (c)http://www.geocities.com/idmssql 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 }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.