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)

104 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objects And Classes In Java
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

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

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.

Drawing an object of class javax.swing.JFrame

Object is associated with a window on your computer monitor JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame Name of object, giving class name and its memory location (hexadecimal). Java creates name when it creates object Function: returns a value; call is an expression Procedure: does not return a value; call is a statement to do something Object contains methods (functions and procedures), which can be called to operate on the object

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

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

Class definition

Class definition : Describes format of an object (instance) of the class. /** description of what the class is for */ public class C { } This is a comment declarations of methods (in any order) Access modifier public means C can be used anywhere Class definition C goes in its own file named C.java On your hard drive, have separate directory for each Java program you write; put all class definitions for program in that directory. You’ll see this when we demo Eclipse

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!

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

Inside-out rule for finding declaration

/** An instance … / public class C extends …JFrame { /* Return area of window */ public int area() { return getWidth() * getHeight(); } } C@6667f34e getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C C@2abcde getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C Function area: in each object. getWidth() calls function getWidth in the object in which it appears.

Class definition with a procedure definition

/** An instance is a JFrame with more methods / public class C extends javax.swing.JFrame { public int area() { return getWidth() * getHeight(); } /* Set width of window to its height */ public void setWtoH() { setSize(getHeight(), getHeight()); } } C@6667f34e … setSize(int, int) getWidth() getHeight() area() setWtoH() JFrame C It is a procedure because it has void instead of return type Call on procedure setSize

About null

v1 C@ v2 null C@ 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 ;

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