Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Object-Oriented Programming: Understanding Objects, Classes, and Methods, Slides of Object Oriented Programming

An introduction to object-oriented programming (oop) in java, focusing on objects, classes, and their methods. The creation and use of objects, class definitions, function and procedure calls, and indirect referencing. It also explains the difference between functions and procedures, and the concept of static procedures.

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(9)

110 documents

1 / 17

Toggle sidebar

Related documents


Partial preview of the text

Download Java Object-Oriented Programming: Understanding Objects, Classes, and Methods and more Slides Object Oriented Programming in PDF only on Docsity! Objects And Classes In Java docsity.com Java OO (Object Orientation) Python and Matlab have objects and classes. Strong-typing nature of Java changes how OO is done and how useful it is. Put aside your previous experience with OO (if any). This lecture: First: describe objects, demoing their creation and use. Second: Show you a class definition and how it defines functions, and procedures that appear in each object of the class. Third (if there is time). Show you a Java application, a class with a “static” procedure with a certain parameter. docsity.com Evaluation of new-expression creates an object Evaluation of new javax.swing.JFrame() creates an object and gives as its value the name of the object JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame If evaluation creates this object, value of expression is JFrame@25c7f37d JFrame@25c7f37d 2 + 3 + 4 9 docsity.com Class variable contains the name of an object Type JFrame: Names of objects of class JFrame JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame h ? JFrame h= new javax.swing.JFrame(); If evaluation of new-exp creates the object shown, name of object is stored in h JFrame@25c7f37d Consequence: a class variable contains not an object but the name of an object. Objects are referenced indirectly. docsity.com Class variable contains the name of an object If variable h contains the name of an object, call methods of the object using dot-notation: JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame h ? JFrame Procedure calls: h.show(); h.setTitle(“this is a title”); Function calls: h.getX() h.getX() + h.getWidth() JFrame@25c7f37d docsity.com Class extends (is a subclass of) JFrame /** An instance is a subclass of JFrame*/ public class C extends javax.swing.JFrame { } C: subclass of JFrame JFrame: superclass of C C inherits all methods that are in a JFrame C@6667f34e hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() … JFrame C Object has 2 partitions: one for JFrame methods, one for C methods Easy re-use of program part! docsity.com Class definition with a function definition /** An instance is a subclass of JFrame with an area function */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } } C@6667f34e … getWidth() getHeight() area() JFrame C Spec, as a comment You know it is a function because it has a return type Function calls automatically call functions that are in the object docsity.com Inside-out rule for finding declaration /** An instance … */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } } C@6667f34e getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C The whole method is in the object To what declaration does a name refer? Use inside-out rule: Look first in method body, starting from name and moving out; then look at parameters; then look outside method in the object. docsity.com Using an object of class Date /** An instance is a JFrame with more methods */ public class C extends javax.swing.JFrame { … /** Put the date and time in the title */ public void setTitleToDate() { } } C@6667f34e … setSize(int, int) setTitle(String) area() { } setWtoH() setTitleToDate JFrame C An object of class java.util.Date contains date and time at which created. It has a function toString(), which yields the data as a String. setTitle((new java.util.Date()).toString()); docsity.com About null v1 C@16 v2 null C@16 getName() C null denotes the absence of a name. v2.getName() is a mistake! Program stops with a NullPointerException You can write things like: v1= null; docsity.com Hello World! /** A simple program that prints Hello, world! */ public class myClass { /** Called to start program. */ public static void main(String[ ] args) { System.out.println("Hello, world!"); } } args is an array of String elements We explain static next week. Briefly: there is only one copy of procedure main, and it is not in any object docsity.com