Introduction To JAVA - 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

UNIVERSITY OF IRINGA

FACULTY OF SCIENCE AND EDUCATION


DEPARTMENT OF INFORMATION TECHNOLOGY

LECTURE THREE
INTRODUCTION TO JAVA

Reg, No. Course Year


DIT 206 Introduction to OOP 2018/ 2019

Ass. Lecturer: Thobius Joseph (M.Sc. In Telecom Eng.)


Date: Dec, 2018

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


CONTENTS

1. Naming convention (Review)


2. Constructor

3. Object

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Required Knowledge

Basic Knowledge of C Programming and C++ will


help you to understand Java Programming
quickly, and If you don't know programming and
you are studying Java, so it's quite complicated.

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Naming Convention(Review)
 It is the advised rule(not forced to follow) that you may use when naming your identifiers such as class, package,
variable, constant, method etc.
 By using standard Java naming conventions, your code is easier to read and less time is spent to figure out what the
code does.
Name Convention

class name should start with uppercase letter and be a noun e.g. String, Color, Button, System,
Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote,
ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(),
println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.

package name should be in lowercase letter e.g. java, lang, sql, util etc.

Constant name should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Constructor
 A constructor is a special method that is used to initialize (assign initial values to )a newly
created object. This method must have same name as the same of the class.
 A constructor in Java is the initiator of an object; anytime you create a new instance of a
class, a constructor is invoked. If you do not create a constructor, the
default constructor (no arguments, no other real code) is created for you by Java.
 It consist of access modifier + classname + (parameter-list) + { codes }

 class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;

/* constructor , a constructor that specifies how these fields are initialized when an Entry object is constructed*/
Entry(String n, String a, String p) {
this.name = n;
this.address = a;
this.phone = p;
}
}
02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724
Constructor overloading
 Like methods, constructors can be overloaded. In other words, you can provide more than
one constructor for a class if each constructor has a unique signature. Here’s another
constructor for the Actor class. A constructor in Java is the initiator of an object; anytime
you create a new instance of a class, a constructor is invoked. If you do not create
a constructor, the default constructor (no arguments, no other real code) is created for
you by Java.
 class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;

/* constructor , a constructor that specifies how these fields are initialized when an Entry object is constructed*/
Entry(String n, String a, String p) {
this.name = n;
this.address = a;
this.phone = p;
}
/* another constructor , a constructor that specifies how these fields are initialized when an Entry object is
constructed*/
Entry(String n, String a) {
this.name = n;
this.address = a;
}
}
02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724
Default Constructor
 If you do not create any constructor, the java will create constructor automatically for you.
This constructor is called default constructor . The default constructor is the constructor
with no arguments and no other real code.
 If you create any constructor inside your code then java will not create default constructor
for you. In this case if you need constructor which look like default you must write by
yourself.
Example if you write this class
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;

}
Then the java will create default constructor for you internal such that you wont see it
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;
Entry (){ // default constructor is created automatically inside your class therefore you wont see it
}
}

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Object creation
 An object is created from a class.
 In Java, there is many ways of creating object but the basic method is by using the new
keyword.
 With new keyword method objects of the class are created by using selected constructor
of that class.
 The syntax includes : className + Name of Obeject + = + new keyword+ selected
constructor + ;

There are three steps when creating an object from a class −


Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the
new object.

 Example from constructor overloading slide; two object will be

Entry ObjectName1 = new Entry (“JOhn”, “200 iringa”, “0717341960”);


Entry ObjectName1 = new Entry (“JOhn”, “200 iringa”);

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Object creation
public class Puppy { public Puppy(String name) {
// This constructor has one parameter, name. \
System.out.println("Passed Name is :" + name );
}

public static void main(String []args) {


// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" ); }
}

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724


Variables
A class can contain any of the following variable types.
Local variables − Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will
be destroyed when the method has completed.
Instance variables − Instance variables are variables within a class but outside any method.
These variables are initialized when the class is instantiated. Instance variables can be
accessed from inside any method, constructor or blocks of that particular class.
Class variables − Class variables are variables declared within a class, outside any method,
with the static keyword

02/21/2021 Thobius Joseph(Msc in Eng.), 0783758724

You might also like