Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

INTRODUCTION TO

SYNTAX OF JAVA FOR


OBJECT ORIENTED
PROGRAMMING
Lecture 03

MR. AFRASIAB KAIKOBAD


BARANI INSTITUTE OF INFORMATION TECHNOLOGY, PMAS ARID AGRICULTURE UNIVERSITY
4th floor Umair Plaza, 6th road, Rawalpindi, Pakistan
Contents
Class:......................................................................................................................................................2
Declaring Classes...............................................................................................................................2
Declaring Attributes...............................................................................................................................2
Declaring Methods................................................................................................................................3
Accessing Object Members...................................................................................................................3
Declaring Constructors..........................................................................................................................4
The Default Constructors...................................................................................................................5

1|Page
Class-Object-UML (Afrasiab Kaikobad)
Concepts and Syntax of Class and Object
in Object-Oriented Program
Class:
Is a user-defined type

− Describes the data (attributes)

− Defines the behavior (methods)

● Instances of a class are objects

Declaring Classes
● Syntax

<modifier>* class <class_name>{

<attribute_declaration>*

<constructor_declaration>*

<method_declaration>*

● Example
class Bicycle {
//data members
//methods
}

● Example

public class Counter{

private int value;

public void inc(){

++value;

public int getValue(){

return value;

● Example
class Bicycle {
// Data Member
private String ownerName;

2|Page
Class-Object-UML (Afrasiab Kaikobad)
//Constructor: Initialzes the data member
public Bicycle( ) {
ownerName = "Unknown";
}
//Returns the name of this bicycle's owner
public String getOwnerName( ) {
return ownerName;
}
//Assigns the name of this bicycle's owner
public void setOwnerName(String name) {
ownerName = name:
}
}

class BicycleRegistration {
public static void main(String[] args) {
Bicycle bike1, bike2;
String owner1, owner2;
bike1 = new Bicycle( ); //Create and assign values to bike1
bike1.setOwnerName("Adam Smith");
bike2 = new Bicycle( ); //Create and assign values to bike2
bike2.setOwnerName("Ben Jones");
//Output the information
owner1 = bike1.getOwnerName( );
owner2 = bike2.getOwnerName( );
System.out.println(owner1 + " owns a bicycle.");
System.out.println(owner2 + " also owns a bicycle.");
}
}

Declaring Attributes
● Syntax

<modifier>* <type> <attribute_name>[= <initial_value>];

● Examples
class Bicycle {
private String ownerName;
//definitions for the constructor,
//getOwnerName, and setOwnerName methods come here
}

● Examples

public class Foo{

private int x;

private float f = 0.0;

private String name =”Anonymous”;

3|Page
Class-Object-UML (Afrasiab Kaikobad)
Declaring Methods
● Syntax

<modifier>* <return_type> <method_name>( <argument>* ){

<statement>*

● Examples
The following diagram shows how the components in the general syntax correspond
to the actual elements in the setOwnerName method:
public void setOwnerName(String name) {
ownerName = name;
}

public String getOwnerName( ) {


return ownerName;
}

● Examples

public class Counter{

public static final int MAX = 100;

private int value;

public void inc(){

if( value < MAX ){

++value;

public int getValue(){

4|Page
Class-Object-UML (Afrasiab Kaikobad)
return value;

Accessing Object Members


● Syntax

<object>.<member>

● Examples

public class Counter{

public static final int MAX = 100;

private int value = 0;

public void inc(){

if( value < MAX ){

++value;

public int getValue(){

return value;

Counter c = new Counter();

c.inc();

int i = c.getValue();

5|Page
Class-Object-UML (Afrasiab Kaikobad)
Declaring Constructors
● Syntax:

[<modifier>]<class_name>( <argument>*){

<statement>*

● Example:
public Bicycle( ) {
ownerName = "Unassigned";
}

● Example:

public class Date {

private int year, month, day;

public Date( int y, int m, int d) {

if( verify(y, m, d) ){

year = y; month = m; day = d;

private boolean verify(int y, int m, int d){

//...

6|Page
Class-Object-UML (Afrasiab Kaikobad)
}

 Role: object initialization


 Name of the constructor must be the same as that of class name.
 Must not have return type.
 Every class should have at least one constructor.
 If you don't write constructor, compiler will generate the default constructor.
 Constructors are usually declared public.
 Constructor can be declared as private → You can't use it outside the class.
 One class can have more than one constructor.
 Constructor overloading.

The Default Constructors


- There is always at least one constructor in every class.

- If the programmer does not supply any constructors, the

default constructor is generated by the compiler

− The default constructor takes no argument

− The default constructor's body is empty

public class Date {

private int year, month, day;

public Date( ){

7|Page
Class-Object-UML (Afrasiab Kaikobad)

You might also like