Object-Oriented Programming in Java: Defining and Using Classes, Slides of Web Design and Development

The basics of defining and using classes in java, including the structure of classes, defining a class with instance variables and symbolic constants, creating and initializing objects, and manipulating those objects. It also includes a comparison with c++ and a task to define a student class with specific characteristics and methods. The document also touches upon the concepts of static variables, methods, and garbage collection in java.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

parasad
parasad 🇮🇳

4.5

(56)

131 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Web Design & Development
Lecture 4
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Object-Oriented Programming in Java: Defining and Using Classes and more Slides Web Design and Development in PDF only on Docsity!

Web Design & Development

Lecture 4

OOP in Java

OOP Vocabulary Review

 Classes

 Definition or a blueprint of a userdefined datatype  Prototypes for objects

 Objects

 Nouns, things in the world

 Constructor

 Given a Class, the way to create an Object (that is, an Instance of the Class) and initialize it

 Attributes

 Properties an object has

 Methods

 Actions that an object can do

Object

Anything we can put a thumb on

Defining Classes

Defining a Class

Comparison with C++

 Java gives you the ability to write classes or user-defined data types
similar to the way C++ does, with a few differences
 Points to consider when defining a class
 There are no global variables or functions. Everything resides
inside a class. Remember we wrote our main method inside a
class

 Specify access modifiers (public, private or protected ) for each member method or data members at every line.

 No semicolon (;) at the end of class
 All methods (functions) are written inline. There are no separate
header and implementation files.

The Point Class

class Point {

private int x;

private int y;

public Point (……) {……}

public void Display (……) {

……….

}

instance variables

and symbolic constants

how to create and

initialize objects

how to manipulate those

objects (may or may not

include its own “driver”,

i.e., main( ))

 Points to consider when defining a class (cont)
 Access Modifiers

 public : Accessible anywhere by anyone  Private : Only accessible within this class  Protected : Accessible only to the class itself and to it’s subclasses or other classes in the same “package”  Package : Default access if no access modifier is provided. Accessible to all classes in the same package

 Constructor

 Same name as class name  Does not have a return type  No initialization list  JVM provides a zero-argument constructor only if a class doesn’t define it’s own constructor

 Destructor

 Destructors are not required in a java class

Defining a Class

Comparison with C++ (cont)

Example

Student Implementation Code

// Student.java

/*

Demonstrates the most basic features of a class. A student is defined by their name and rollNo. There are standard get/set accessors for name and rollNo.

NOTE A well documented class should include an introductory comment like this. Don't get into all the details – just introduce the landscape.

*/

public class Student {

private String name;

private int rollNo;

// Standard Setters

public void setName (String name) { this.name = name; }

// Note the masking of class level variable rollNo public void setRollNo (int rollNo) { if (rollNo > 0) { this.rollNo = rollNo; }else { this.rollNo = 100; } }

Student Implementation Code cont.

// Constructor that uses a default value instead of taking an argument.

public Student() { name = “not set”; rollNo = 100; }

// parameterized Constructor for a new student

public Student(String name, int rollNo) { setName(name); //call to setter of name setRollNo(rollNo); //call to setter of rollNo }

// Copy Constructor for a new student public Student(Student s) { name = s.name; rollNo = s.rollNo; }

Student Implementation Code cont.

// method used to display method on console

public void print () {

System.out.println("Student name:" +name+ ", roll no:" +rollNo);

}

} // end of class

Student Implementation Code cont.

Using a Class

Comparison with C++

 Objects of a class are always created on heap using the “new”
operator followed by constructor

 Student s = new Student () // no pointer operator “*” between // Student and s

 Only String constant is an exception  String greet = “Hello” ; // No new operator

 However you can use  String greet2 = new String(“Hello”);

 Members of a class ( member variables and methods also known
as instance variables/methods ) are accessed using “.” operator.
There is no “” operator in java

 s.setName(“Ali”);  SsetName(“Ali”) is incorrect and will not compile in java

Using a class

Comparison with C++

 Differences from C++ (cont)

 Objects are always passed by reference whereas

primitive data types are passed by value.

 All methods use the run-time, not compile-time, types

(i.e. all Java methods are like C++ virtual functions)

 The types of all objects are known at run-time

 All objects are allocated on the heap (always safe to

return objects from methods)