M255 Object Oriented Programming with Java

Size: px
Start display at page:

Download "M255 Object Oriented Programming with Java"

Transcription

1 Week 4 M255 Object Oriented Programming with Java This lecture note covers unit 4. Introduction: Prepared by: Dr. Bayan Abu Shawar, AOU-Jordan This unit will cover: Main structure of a java program and class; Writing new class with its attributes and methods using BlueJ editor; Creating new behaviour for objects through writing new methods or changing existing once; Introducing instance variables (Attributes) and their accessor methods (setters & getters); Introducing some of access modifiers: public and private; Exploring more complex methods, including those with return values and arguments; Introducing the principles of reuse and encapsulation, and data hiding; 1. Main Structure of a class in Java In object-oriented programming classes are used to group related variables and functions. A class is a collection of a fixed number of components (members). The general syntax of defining a class is: public class class_name //indicate beginning of a class private attribute1; private attribute2; public method1; public method2; //indicate ending of a class The attributes and methods (messages) are known as a class member list. If a member of a class is an instance variable (Attribute), it is declared just like any other variable (i.e. int position;) but without initializing in declaration parts. Access modifier: tells the Java compiler what other objects can send the corresponding class member. public, private: are called access modifiers, it is related to accessing the members of the class, who is allowed to access that member: 1

2 private: class members are accessed in within the same class. So members are invisible to other classes, and instances of other classes cannot directly access those members, even the subclass will not be able to access private member class from parent class directly. public: class members are accessible outside the class, or visible to all classes. Let us define a class called JRectangle that has following member list: Attributes: x, and y as float; Methods: the accessor methods for x and y; and a method named area that returns the area of a rectangle object. So to define the class with these attributes: public class JRectangle private float x, y;.. You define the attributes as any other variable you determine datat_type then attribute_name followed by semicolon. Usually the attributes in a class are defined as private which means only these attributes are accessible inside this class only. Note that: you could not give initial value to attribute upon creation so for example: int x=0; is not a valid statement, the initializing should be done through constructor, as you learned in Unit 3. Methods: A method is the piece of code that is executed when an object receives a particular message. A method can be thought of as the way the behavior in response to a message is provided, which is the concrete implementation of a message. Now, it s time to define methods where objects of this class can respond to. The general syntax for defining a method in Java is: public Return_data_type method_name(arguments) // begin of message code S1; s2; // end of message code 2

3 Each method is composed of mainly two parts: Method header and method body a. Method header (method declaration): The method header has three parts: Public Return_value_type Method_name( parameter list) 1. Accessor modifier: public or private. (methods are usually public) 2. Return-value-type: states the type of the value returned as a message answer from this method. This type is either to be void (nothing returned) or any other type such as: int, char, or a class name such as Frog, etc. 3. Method_name(arguments): method name is the same as message name, and should be followed by parentheses to distinguish it from variable name, sometimes there are arguments inside parentheses or non. b. Method body The method body is the set of declarations and Java statements that constitutes the method, each statement is ended by semicolon. The method body is enclosed by braces (curly brackets). The syntax in Java is: public Return_value_type method_name( parameter list) //method header Declarations and statements //method body Return back to JRectnagle class, the methods of this class will be: public void setx(float); //method prototype public void sety(float); public float getx(); public float gety(); public float area(); Usually a method is preceded by comments that describe in plain English what this message do. 3

4 The general structure of writing these methods inside the class is as follows: public class JRectangle private float x, y; // attributes public void setx(float a) x = a; public void sety(float b) y = b; public float getx() return x; public float gety() return y; public float area() return (x*y); In order to use the JRectangle class, you need to open another class, which will have main method. Then inside the main method write the statements as you used to do in OUWorkspace, so declare instances of this class and call the methods as below: public class JRectangle_test public static void main( String args[]) //begin of main() JRectangle rectangle = new JRectangle(); float z; rectangle.setx((float)5.5); rectangle.sety(4); z = rectangle.area(); System.out.println( The area is +z); //output statement //end of main() //end of class 4

5 Important Notices: The main program is a class by itself; you do not need to bother what is the meaning of main program header now. Each method has a method signature which is composed of method name, and type of arguments if found. So: o Method signature of: public void setx(float a) is setx(float); o Method signature of: public float area() is area() Two or more methods in the same class cannot have the same signature: public void dosomething(int m) public int dosomethong(int c) Both have the same signature dosomethong(int), so Java compiler would report an error. The argument in the message-send such as: rectangle.setx(5); which is 5, is known as the actual argument: that is the actual argument sent to receiver. To execute this message at run time, this message invokes the method setx(int a) that is found in the class, the argument that is found in the method header is known as formal argument (i.e. a). The actual and formal arguments should be the same in number of parameters, order, and data type. The type of actual argument should be compatible with type of formal ones. Return statements should be the last one in the method that returns an answer. Accessor methods: setters and getters methods. Accessor modifiers: public, and private. Current state of an object is determined by values of its attributes (instance variables). The object oriented language is used to achieve: Encapsulation: the ability to combine state and behaviour in a single unit object. Object encapsulates state and behaviour. Abstraction: separating the design details from the implementation Data hiding: an object becomes as a black box, with access to encapsulated data (Attributes) being possible only through a limited set of public method. Inheritance: create new classes from existing classes. Polymorphism: the ability to use the same expression to denote different operations 5

6 Exercise one: Launch BlueJ software, then: 1. Project New Project. Figure 1 will appear, enter project name and folder, let us name it as rectangle_1 Figure 1. saving new project in BlueJ 2. Press on new class from left hand pane. Figure 2 will appear where you need to insert the class name: JRectangle, be sure the option class is selected, then press OK. Figure 2. Create new class and give it a name 3. The JRectangle class will appear in BLueJ editor which is shaded in orange. Double click on it and write the class JRectnagle int it. 4. Compile and save the class 5. Click on New class again, name it as : JRectangle_test 6. Double click to open it then write the main method inside it. 6

7 7. Compile and save 8. Select JRectnagle_Test rightclick void main() OK 9. The result will appear in another window. 10. The BlueJ editor will look like as figure 3. Figure 3. BlueJ with the two classes we use Note that: even though, you did not write a constructor, an automatic constructor will be generated for any class if programmer did not do so. The initialization process this automatic constructor will do is based on attributes (instance variables) type, so any number will be initialized to 0 or 0.0 based if it is integer or real; the character and String will be initialised to, and consequently. To be sure: double click on JRectangle class, then on the right hand side of window there is view option implementation, open the arrow and select interface. You will see the documentation for this class where the constructor is generated. 7

8 Activity One: 1. Launch BlueJ, click Project Open project Unit4_Project_1. You will see three classes: Toad, Frog, and HoverFrog. 2. Double click on Frog class and see its implementation as in figure 4. Figure 4. Frog class implemtation 3. Notice the statement: public class Frog extends OUAnimatedObject The keyword extends means inherit, so Frog is a sub class of super class OUAnimatedObject. The OUAnimatedObject class is responsible of presenting Frog instance moving in Amphibian application. 8

9 4. You will notice that each method is preceded by comments, then method header, and method body. For example the Frog constructor is: * Constructor for objects of class Frog which initialises * colour to green and position to 1. public Frog() super(); this.colour = OUColour.GREEN; //colour = OUClolur.GREEN this.position = 1; super(): this statement invokes the constructor of upper class. this. : is known as a pseudo-variable, and it refers the receiver of the message who caused the execution of the method. And you cannot change the object to which this refers. colour and position: are two instance variables that are declared as private in Frog class so they can be accessed directly inside the class members, and they were having initial values. Any method that is written in Frog class is preceded by comments describing what this method do, then method heard and finally method body. o Comment: is enclosed by and which will be used later in documentation. It contains the description in plain English of what the method does. o Method header: tell the Java compiler important information about the method. o Method body: is the Java code that tells the compiler what should happen when the method is executed. 5. Add a new method to Frog class called: catchfly(),the new behaviour is that: an object receives catchfly() will perform jump(), then croak(), and then moves one position to the right. Solution: Before then end of the class add the following: /* * Performs jump, then croaks and finally moves one position to the right Public void catchfly() this.jump(); this.croak(); this.right(); 9

10 6. Save the class Frog and close it. What did you notice? You will notice that class Frog and HoverForg are shaded now, this means there is some modification and the class should be recompiled, this modification could be: adding new method as we did in step 6, or modifying an existing one. The Sub-class usually needs to recompile if any modification happened to super class. 7. Click on Frog class then press on compile button at left hand Pane, or from Tools menu. If no errors appear, then Frog class is ready to be used. 8. Open OUWorkspace from Tools to write Java statements in it, or make a new class name it as Test_Frog, which includes the main method, then write Java statements inside it. public class Test_Frog public static void main( String args[]) //begin of main()... //end of main() //end of class Write the following statements: Declare an object of Frog class called it Kermit. Send message left() to Kermit object Send message catchfly() to Kermit object Return position of kermit object When you finish, compile class, then close the file. Select Test_Frog class and from right click select void main then enter. Solution: public class Test_Frog public static void main( String args[]) //begin of main() Frog kermit = new Frog(); kermit.left(); kermit.catchfly(); System.out.println(kermit.getPosition()); //end of main() //end of class 10

11 OR simply in OUWorkspace you can directly write: Frog kermit = new Frog(); Kermit.left(); Kermit.catchFly(); kermit.getpositon(); Open Graphical display to see effect. 9. Add a new method to class Frog name it as yellow() which sets the colour of the receiver to yellow. * Sets the colour of the receiver to yellow public void yellow() this.setcolour(oucolour.yellow); 10. In the OUWorkspace, declare an object of type Frog name it frog1, and call yellow() message. See effect on graphical representation. Frog frog1 = new Frog(); Frog1.yellow(); Activity Two: Write a new method called: doubleleft(), for the class Toad. This method will decrement the value of instance variable position by 4. (use the same Unit4_Project_1). There are three ways to write this method as appear in forma, formb, and FormC, inside Toad class. Form A: * Move receiver left twice Public void doubleleft() this.setposition(this.getposition()- 4 ); 11

12 Form B: Form C: * Move receiver left twice Public void doubleleft() this.left(); this.left(); * Move receiver left twice Public void doubleleft() this.position = this.position-4; this.update( position ); The differences between these forms are in the reusability issue as follows: When you add a new method to a class, you can reduce some of the complexity of the new method, and also make new method easier to maintain, by reusing methods that have already been developed and tested. All previous forms will give the right answer now. But if Toad class designers change their mind, and decreed the toads move left 3 stones at a time instead of 2, so forma will not work correctly, you need to replace 4 by 6. If you have more than one method whose result depends on move left written in the same way as forma, you need to change it all and recompile. However, formb, will behave correctly; It will always produce the same effect as two moves left. So designer could change the meaning of left() as they want, but FormB will always produce the correct behaviour. In this case only body of left() method is changed but any other method that reuse left() will not require any changes at all. The difference between formc, and others is that in fromc you used the instance variable (attribute) position directly, where froma, and formb uses accessor methods. Actually the left() of Toad classmethod body is based on accessor methods as follows: public void left() this.setposition(this.getposition()-2); 12

13 Direct access to instance variables can take less processing time than using accessor methods. However, o Using accessor methods is more economical in terms of coding: if you change the behaviour of any setters or getters, the modifications done only once, then other methods that reuse the setters and getters will not effect o So accessor method aid reuse. Another benefit of using form B or accessor methods, as you know, the OUWorkspace is connected with a graphical display that shows the graphical effects on Amphibian window, in order to refresh the Amphibian window in case any change happened to current state of an object, the method update() is used for this purpose: to alert Amphibian window to refresh. update() is used in case of formc, where in formb it will be automatically called within left() method that invokes setposition() which in turn invokes update() in its body, and in forma, it will be called from setter method setposition(), no need to write it in specific. The same in case of setcolour(), setposition(), and setheight(), all setters invoke update() in their methods body. If you forgot to add update() in case of formc, the position will change but no effect will be shown in Amphibian window. As a result the best form of the above that will save time and effort in case of any change happen will be formb. Always try to reuse existing methods, especially the methods that based on setters and getters ones. Add formb to class Toad, then recompile the class. Using OUWorkspace: declare an object of toad class name it toad1, and send it left() message then send it doubleleft()message and see Amphibian effects. Toad toad1=new Toad(); toad1.left(); toad1.doubleleft(); In general, as a programmer you can reuse some of the existing methods, or the Java libraries: such as String class we used before. 13

14 Notice the import statements at the beginning of Frog class: this is an example of reusing some Java libraries.(i.e import ou.*; ) This is a special library that has all the projects that you will deal with in this module, in addition to Amphibian graphical representation. Activity Three: Open project: Unit4_Project_2. 1. Declare another instance variable (Attribute) flycount which will be used to record number of times the message catchfly() has been sent to a Frog object. Since it will count number of times, it should be integer. 2. Initialise this flycount to be zero. (This should be done in constructor). 3. Declare setters and getters of attribute flycount. 4. Update method catchfly() so this flycount attribute will be incremented inside catchfly(). Solution: 1. Search in Frog class for private attribute, add: private int flycount; 2. Go to Frog() constructor an add: flycount = 0; so Frog() constructor becomes: public Frog() super(); this.colour = OUColour.GREEN; this.position = 1; flycount = 0; 3. * set flycount of receiver to argument value a * public void setflycount(int a) flycount = a ; * return value of flycount public int getflycount() return this.flycount; 14

15 4. public void catchfly() Activity Four this.jump(); this.croak(); this.right(); this.setflycount(this.getflycount()+1); Open project: Uni4_Project_3 from block 1 1. Add new method positionreport() that returns position of a Frog object as report not as integer. public String positionreport() return "The position is " + this.getposition(); 2. Define a new object of Frog class name it kermit, then call getposition(), and name it as positionreport(). String frogposition; Frog kermit = new Frog(); frogposition = kermit.positionreport(); 3. Define a similar method heightreport() in HoverFrog class that returns a report about height. *Returns the String "The height is " concatenated with *the value of the height of the receiver. public String heightreport() return "The height is " + this.getheight(); 4. Define an object named Wezzy of HoverFrog class, and call up(), then call heighreport(). String frogheight; HoverFrog wizzy = new HoverFrog(); wizzy.up(); frogheight = wizzy.heightreport(); 15

16 Activity Five: Open project Unit4_Project_4. Save it as Unit4_Project_4_Act15. Note that Frog class has twomethods samecolouras(): * Sets the colour of the receiver to the argument's colour. public void samecolouras (Frog afrog) this.setcolour(afrog.getcolour()); * Sets the colour of the receiver to the argument's colour. public void samecolouras (Toad atoad) this.setcolour(atoad.getcolour()); 1. Execute the following statements: Frog kermit = new Frog(); Frog gribbit = new Frog(); Toad toad1 = new Toad(); kermit.setcolour(oucolour.red); gribbit.samecolouras(kermit); gribbit.samecolouras(toad1); HoverFrog wizzy = new HoverFrog(); gribbit.samecolouras(wizzy); 2. Open Frog class, and delete samecolouras(toad toad1) from it. The try the statements in (1) again. You should have found that the message gribbit.samecolouras(toad1); no longer works and you get the error message Semantic error: Message samecolouras(toad) not understood by class 'Frog' However, the message gribbit.samecolouras(wizzy); should still work correctly. This is because wizzy is a HoverFrog object which could be assigned to a parent class one. So in case of message-send: gribbit.samecolouras(wizzy) It will invoke method: samecolouras(frog afrog) 16

17 3. Write a new method setpositionandcolour() in Frog class which takes two arguments, colour, and integer. *Sets the position of the receiver to the argument * aposition and the colour of the receiver to the *argument acolour. public void setpositionandcolour(int aposition, OUColour acolour) this.setposition(aposition); this.setcolour(acolour); 4. Try to execute: (a) frog1.setpositionandcolour(5, OUColour.BLUE); (b) frog3.setpositionandcolour(oucolour.magenta, 4); (a) Will be executed correctly, but (b) will produce an error as type mismatch. Documenting Methods In the previous methods we developed, and the way the methods are written in provided classes, we found that comments has special format that it should be enclosed within and. This comment in such format is used by a program called javadoc. The javadoc program picks up information from specially formatted comments and other parts of the class code such constructor and method header to create an HTML file, which described the class in a standard way. This documentation is useful for human readers to know what is the class and its methods, and interface of these methods. You can vie documentation of whole project by: Tools Project documentation And if you want to see it in a class level, each class alone: 1. Double click on class 2. When it is opened, select interface from view menu on the top right side. Important Notice: Try the activities in section 7 of unit 4 by yourself. Also read glossary, summary, and try all SAQs, and exercises in this unit. 17

18 Class Work: Question 1: The following is the method home() in the class Frog. *Resets the receiver to its "home" position of 1. public void home() this.setposition(1); Write down: (a) the comment; (b) the method header; (c) the method body. ANSWER... (a) The comment is: *Resets the receiver to its "home" position of 1. (b) The method header is: public void home() (c) The method body is: this.setposition(1); Question 2: What is the difference between a message and a method? ANSWER... A method is the piece of code (in the case of M255, Java code) that is executed when an object receives a particular message. In other words, when you send a message to an object, it is the corresponding method code that is executed at run-time. Question 3: What do you need to do to add a new message to the protocol of a class? ANSWER... Adding a message to a protocol requires writing a new method for the class. The editor is used to do this. 18

19 Question 4: How can an object send a message to itself? ANSWER... By using the special reference variable this as the receiver of the message. Question 5: What is the purpose of the each statement in the following constructor body? The constructor is as follows. *Constructor for objects of class Frog which initialises colour *to green and position to 1. public Frog() super(); this.colour = OUColour.GREEN; this.position = 1; Solution... super(): call constructor of super class. this.colour = OUColour.GREEN; This statement sets the instance variable colour to the initial colour OUColour.GREEN. this.position = 1; This statement sets the instance variable position to the initial home position, 1. The effect of these two statements is to initialise the two instance variables, colour and position. Initialising instance variables is commonly done by the constructor. 19

20 Question 5: Explain what happen when the message left() from Frog class is sent to Kermit object which is of Frog class. Solution... The message-send kermit.left() causes the code of method left(), which is shown below, to be executed. public void left() this.setposition(this.getposition() 1); This entails three steps: The body of the method getposition() is executed first. The code for the method is given below. public void getposition() return this.position; Notice the use of this to reference the object that was sent the getposition() message, which enables the value of the instance variable position stored for that object to be accessed. The value of position is then returned as the message answer. The next step is to take 1 away from the value that has just been returned, and use this value as the argument to this.setposition(). The final step is to execute the method setposition(). The code for the method is given below. public void setposition(int aposition) this.position = aposition; Again this is used, so that the instance variable position for kermit will be set to the value just calculated in the previous step. Question 6: Write down the method header for a method that has the name mymethod, is accessible to any object, has no arguments, and returns an integer value. ANSWER... public int mymethod() 20

21 Question 7: Write a new method called distancefromhome() for the Frog class, which will return the value of the position of the receiver minus 1 (1 being the home position). Solution... *Return the position of the receiver minus 1 (the home position). public int distancefromhome() return (this.getposition() 1); Question 8: What do you think will happen when you execute the following statements in the OUWorkspace? Frog frog1 = new Frog(); frog1.setposition(oucolour.red); Solution... OUColour.RED is not an integer, so an error message will be displayed in the Display Pane of the OUWorkspace. In fact, the actual error message would be: Semantic error: Message setposition(ou.oucolour) not understood by class 'Frog'. Question 9: Write down the receivers and the arguments of the following message-sends. (a) frog1.samecolouras(frog3); (b) frog4.setposition(3); ANSWER... (a) frog1 is the receiver and frog3 is the argument. (b) frog4 is the receiver and 3 is the argument. Question 10: Write a method increaseposition() in Frog class, such that it increases the value of position attribute by amount defined in method argument step. Solution *Increases the value of the position of the receiver by the value of the argument step. public void increaseposition(int step) this.setposition(this.getposition() + step); 21

22 Question 11: Describe what happens when the following message is sent to the Frog object referenced by frog2, whose instance variable position has the value 6. frog2.increaseposition(4); Solution... On receiving the message increaseposition(), the object referenced by frog2 causes the method increaseposition() to execute. The method s formal argument, step, is set to the value of the message s actual argument, which is 4. The message getposition( ) produces an answer that is the value of the current position of the receiver, frog2, i.e. the integer 6. The integer 6 is then added to the actual argument, 4, by means of the arithmetic operator +, and results in the integer 10. The integer 10 becomes the actual argument for the setposition() message sent to the receiver this, so the instance variable position of the object referenced by frog2 is set to 10. Question 12: What is the signature of: public void setpositionandcolour(int aposition, OUColour acolour) Solution: The signature is: setpositionandcolour(int, OUColour) Question 13: How is encapsulation implemented in Java? ANSWER... In Java you can define classes that are templates for the creation of objects. These objects then encapsulate both state and behaviour. Question 14: How do you implement data hiding in Java? ANSWER... Data hiding is implemented by declaring instance variables as private. Question 15: Write the header of a class Circle that inherits class Shape. Solution: public class Circle extends Shape 22

23 Home Work: Assume you have the following classes: Class: Square float x Class: Rectangle float x,y Class: Triangle float x,y area(), setx(), getx() area(), sety(), setx(), gety(), getx() area(), setx(), getx(), Sety(), gety() 1. Find common attributes and behavior, and insert them in a super class named: Shape. Draw the new class diagram representing inheritance relationship. Note that the area() has different behaviour in each class as follows: Square area = x * x Triangle area = 0.5 * x * y Rectangle area = x * y 2. Represent the super class Shape with its sub-classes Rectangle, Square, and Triangle in Java language. 3. Define three objects as follows: asquare of Square class arectangle of Rectangle class atriangle of Triangle class Then invoke setters for each object, and area of each. Use System.out.println() to print area of each shape. Write those statements inside the following main method: public class Test_Shape public static void main( String args[]) // end of main // end of class 23

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 17 January 2019 SP1-Lab1-2018-19.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1 Module Information Lectures: Afternoon

More information

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 11 January 2018 SP1-Lab1-2017-18.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1 Module Information Lectures: Afternoon

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation Outline Problem: Create, read in and print out four sets of student grades Setting up the problem Breaking

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

Introduction Welcome! Before you start Course Assessments The course at a glance How to pass M257

Introduction Welcome! Before you start Course Assessments The course at a glance How to pass M257 Introduction Unit 1: Java Everywhere Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 Introduction Welcome! Before you start Course Assessments The course at a glance How to pass M257 1. Java background 2.

More information

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer Introduce Eclipse Create objects in Java Introduce variables as object references Aleksandar Stefanovski CSCI 053 Department of Computer Science The George Washington University Spring, 2010 Invoke methods

More information

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5 Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public,

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Industrial Programming

Industrial Programming Industrial Programming Lecture 4: C# Objects & Classes Industrial Programming 1 What is an Object Central to the object-oriented programming paradigm is the notion of an object. Objects are the nouns a

More information

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes What is an Object Industrial Programming Lecture 4: C# Objects & Classes Central to the object-oriented programming paradigm is the notion of an object. Objects are the nouns a person called John Objects

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

More information

Reviewing OO Concepts

Reviewing OO Concepts Reviewing OO Concepts Users want to draw circles onto the display canvas. public class Circle { // more code here SWEN-261 Introduc2on to So3ware Engineering Department of So3ware Engineering Rochester

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance? INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation

More information

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09 Objectives Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

Reviewing OO Concepts

Reviewing OO Concepts Reviewing OO Concepts Users want to draw circles onto the display canvas. public class Circle { // more code here SWEN-261 Introduction to Software Engineering Department of Software Engineering Rochester

More information

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

index.pdf January 21,

index.pdf January 21, index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Structured Programming

Structured Programming CS 170 Java Programming 1 Objects and Variables A Little More History, Variables and Assignment, Objects, Classes, and Methods Structured Programming Ideas about how programs should be organized Functionally

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes: Designing Classes part 2 CSC 1051 Data Structures and Algorithms I Getting to know classes so far Predefined classes from the Java API. Defining classes of our own: Driver classes: Account Transactions

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe concepts of class, class

More information

One of these "compartments" is more correctly referred to as an element of the array

One of these compartments is more correctly referred to as an element of the array An array is a special type of variable in that it can contain many values If a standard variable is like a box, think of an array as being like a box with compartments: One of these "compartments" is more

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use

More information

Objects and Classes: Working with the State and Behavior of Objects

Objects and Classes: Working with the State and Behavior of Objects Objects and Classes: Working with the State and Behavior of Objects 1 The Core Object-Oriented Programming Concepts CLASS TYPE FACTORY OBJECT DATA IDENTIFIER Classes contain data members types of variables

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! Describe and define concepts of class and object! Describe use

More information

Computer Science AP 2017 Summer Assignment Mrs. McFarland

Computer Science AP 2017 Summer Assignment Mrs. McFarland Computer Science AP 2017 Summer Assignment Mrs. McFarland Read Chapter 1 from the book Think Java: How to Think Like a Computer Scientist by Allen B. Downey. I have included Chapter 1 in this pdf. If you

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 10 Classes Continued. Fundamentals of Java Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture IX: Methods Introduction method: construct for grouping statements together to

More information