You are on page 1of 20

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

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


CONTENTS
1. Naming convention (Review)
2. Constructor
3. Object
4. Inheritance

1/17/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.

1/17/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.

1/17/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;
}
}
1/17/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;
}
}
1/17/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
}
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Characteristics of Constructors
• Constructors cannot be private
• Constructors cannot be inherited but cab be
access in subclass
• Constructors cannot have return type or cant
return value
• Constructors are automatically called when an
object is created
Types of Java Constructors
1. Default Constructor (no –arg constructor)
2. Parameterized Constructor

1/17/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”);

1/17/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" ); }
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Variables
A class can contain any of the following variable types.
 Local variables
- are variables which 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.
- You can use this variable only within that method and the other methods in the class aren't
even aware that the variable exists.
- A local variable cannot be defined with "static" keyword.

 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.
- A variable declared inside the class but outside the body of the method, is called instance
variable. It is not declared as static. It is called instance variable because its value is instance
specific and is not shared among instances.
 Class (or static) variables
- Class variables are variables declared within a class, outside any method, with the static
keyword
- Unlike instance variables, we can only have one copy of a static variable per class
irrespective of how many objects
1/17/2021
we create
Thobius Joseph(Msc in Eng.), 0783758724
Variables
 Examples
class A {
int data=50;//instance variable

static int m=100;//static variable

void method() {
int n=90;//local variable
}
}//end of class

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Variables
 Examples
public class StudentDetails {
public void StudentAge()
{
// local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}

public static void main(String args[])


{
StudentDetails obj = new StudentDetails();
obj.StudentAge();
}
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Variables
 Examples
public class StudentDetails {
public void StudentAge()
{ // local variable age
int age = 0;
age = age + 5;
}

public static void main(String args[])


{
// using local variable age outside it's scope
System.out.println("Student age is : " + age);
}
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Variables
 Examples
To access static variables, we need not create an object of that class, we can simply access
the variable as::
class_name.variable_name;
import java.io.*;
class Emp {

// static variable salary


public static double salary;
public static String name = "Harsh";
}

public class EmpDemo {


public static void main(String args[])
{

// accessing static variable without object


Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}}
1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724
Variables
 Examples
Instance variable Vs Static variable
 Each object will have its own copy of instance variable whereas We can only have one
copy of a static variable per class irrespective of how many objects we create.

 Changes made in an instance variable using one object will not be reflected in other
objects as each object has its own copy of instance variable. In case of static, changes will
be reflected in other objects as static variables are common to all object of a class.

 We can access instance variables through object references and Static Variables can be
accessed directly using class name.

Syntax for static and instance variables:

class Example {
static int a; //static variable
int b; //instance variable
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


The need of Inheritance
• You cant use variable from another class by using object until you
inherit that class
class Emp{ //first class
public static String emName= "John";// static variable from class Emp
}
public class EmpDemo{ // second class
public static void main (String [] args){
System.out.println(Emp.emName);
EmpDemo obj1 = new EmpDemo();
System.out.println(obj1.emName); // cant work need inheritence to
access variable of the first class
}
}

1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724


Inheritance
• Inheritance can be defined as the process where one class acquires the properties (methods and fields) of
another. By this technique we can create hierarchy of classes.
• The class which inherits the properties of other is known as subclass (derived class, child class) and the
class whose properties are inherited is known as superclass (base class, parent class).
 How we can create inheritance?
• By using extends keyword. extends is the keyword used to inherit the properties of a class. Following is the
syntax of extends keyword.
Inheritance syntax Inheritance example

class SuperClassName { class Calculation {


..... ..... int z;
} public void addition(int x, int y) {
class SubClassName extends SuperClassName { z = x + y; System. out.println ("The sum of the given numbers:"+z);
..... ..... }
} public void Subtraction(int x, int y) {
z = x - y; System.out.println ("The difference between the given numbers:"+z); } }
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given
numbers:"+z); }
public static void main(String args[]) {
int a = 20, b = 10; My_Calculation demo = new My_Calculation(); demo.addition(a, b);
demo.Subtraction(a, b); demo.multiplication(a, b); }
}

1/17/2021 Thobius Joseph(Msc in TE Eng.)0783758724 18


Final keyword
 What is final in Java?
- Final is a keyword in Java that is used to restrict the user and can be used in many respects. Final can be used
with:
• Class
• Methods
• Variables

Class declared as final :: Class declared Method declared as final :: A method declared as final
as final cannot be overridden; this means even when a child
When a class is declared as final, it class can call the final method of parent class without
cannot be extended further. Provide any issues, but the overriding will not be possible.
Here to a class an example what Here is a sample program showing what is not valid
happens within a program within a Java program when a method is declared as
final.
Once a variable is assigned with the keyword final, it always contains the same exact value. Again things may
happen like this; if a final variable holds a reference to an object then the state of the object can be altered if
programmers perform certain operations on those objects, but the variable will always refer to the same object.
A final variable that is not initialized at the time of declaration is known as a blank final variable. If you are
declaring a final variable in a constructor, then you must initialize the blank final variable within the constructor
of the class. Otherwise, the program might show a compilation error.

1/17/2021 Thobius Joseph(Msc in TE Eng.)0783758724 19


Polymorphism
 The word polymorphism means having multiple forms.
 Polymorphism can be achieved in two of the following ways:
• Method Overloading(Compile time Polymorphism) (Static Polymorphism)
• Method Overriding(Run time Polymorphism) (Dynamic Polymorphism.)
 Method Overloading(Compile time Polymorphism)
- A method which is overloaded can contain different access modifiers.
- It used when you want to avoid fill all information at the same time
- Overloading method's argument lists might differ in:
• Number of parameters passed
• Data type of actual parameters
• Sequence of data type of actual parameters
 Method Overriding(Run time Polymorphism)
- Occurs between super class and sub class and when the method is not declared final. Rules for method overriding;
1. Argument list: The argument list and data types at the time of overriding method need to be same as that of the method of
the parent class.
2. Access Modifier: The Access Modifier present in the overriding method (method of subclass) cannot be more restrictive
than that of an overridden method of the parent class. The private, static and final methods can't be overridden as they are
local Overloading
Method to the class. Method Overriding
class Mltply { class parent { public void work() { System.out.println("Parent is under
void mul(int a, int b) { System.out.println("Sum of two=" + (a * retirement from work."); } }
b)); } class child extends parent { public void work() { System.out.println("Child has
void mul(int a, int b, int c) { System.out.println("Sum of three=" a job"); System.out.println(" He is doing it well"); }
+ (a * b * c)); } } public static void main(String argu[])
class Polymorphism { public static void main(String args[]) { { child c1 = new child(); c1.work(); } }
Mltply m = new Mltply(); m.mul(6, 10); m.mul(10, 6, 5); } }

1/17/2021 Thobius Joseph(Msc in TE Eng.)0783758724 20

You might also like