0% found this document useful (0 votes)
0 views25 pages

Classes & Objects-1

The document provides an overview of classes and objects in Java, detailing the structure and functionality of classes, constructors, methods, and the use of the 'this' keyword. It explains how to create and manipulate objects, including instance and static members, and the rules governing constructors and methods. Additionally, it highlights the difference between instance and static members, emphasizing their accessibility and behavior in Java programming.

Uploaded by

BIRUK GEBRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views25 pages

Classes & Objects-1

The document provides an overview of classes and objects in Java, detailing the structure and functionality of classes, constructors, methods, and the use of the 'this' keyword. It explains how to create and manipulate objects, including instance and static members, and the rules governing constructors and methods. Additionally, it highlights the difference between instance and static members, emphasizing their accessibility and behavior in Java programming.

Uploaded by

BIRUK GEBRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

CLASSES & OBJECTS

OUTLINE
Class
Constructor
Method
Object
This keyword
Instance and static
members
OVERCOMING
NERVOUSNESS
Confidence-building strategies
CLASS 5

class <Class Name> { // Constructors


Car(String type, String model,
field;
String color) {
method; this.type = type;
this.model = model;
}
this.color = color;
class Car {
}
// fields or properties
String type; }
String model;
String color;
int speed;
class 6

CAR CLASS — looks like a template or blueprint


object 7

OBJECT — implementation of its class


CONSTRUCTOR 8

 Is a function called during the creation (instantiation)


of objects
 Its name must be similar to class name that contain
it
 Has no return type
 If you don't define a constructor for a class, a default
parameter less constructor is automatically created
by the compiler
 The default constructor calls the default parent
constructor (super()) and initializes all instance
variables to default value
CONSTRUCTOR 9

 Objects are constructed in java


 At least one constructor is invoked if an object
is created
 Every class has its own constructor
 Constructors
 can't be marked static
 can't be marked final or abstract(can’t overridden)
 is either legal or illegal
 have all of the normal access modifiers
 Syntax: ClassName() {…}
EXAMPLE 10

Public class Student {


// legal constructor
Student () {} Constructor
Student (int id) {…} overloadin
Student (int id, string name) {…} g

//illegal constructor
Void Student () {} // it is method not constructor
StudentReg (int id) {…} // neither method nor constructor
Student (int id, string name); // look like an abstract
method
Static Student (int id) {…}
Final Student (int id, string name) {…} //can’t be
static, final, & abstract
abstract Student (string name) {…}
}
11
METHOD

 Is a function called during setting a value to fields


or getting a value of fields and performing d/t
operation

 May contain an argument or not

 Represent the behavior of the object

 Name is must be mixedCase if contain more than


one word
 Syntax :-
Modifier return-Type method-Name (){
12

METHOD

–modifier − It defines the access type of the method and it is


optional to use.

–returnType − Method may return a value.

–nameOfMethod − This is the method name. The method


signature consists of the method name and the parameter list.

–Parameter List − The list of parameters, it is the type, order, and


number of parameters of a method. These are optional, method may
contain zero parameters.
13

METHOD
–Passing parameter
–Java passes arguments by value

– The arguments should be in the same order as


their respective parameters in the method
specification.
– Parameters can be passed by value or by
reference.
– Passing Parameters by Value means calling a
14

OBJECT

 Is an instance of a class/type

 Has a (current) state and behavior

 Example :-

 a dog has state (name, color, breed, hungry)


and behavior (barking, fetching, and
wagging tail).

 Students have state (name, age, sex) and


behavior (eating, studying, learning)
15

CONT.…

 In java, object can be created by instantiating a


class
 The syntax for creating object is

 ClassName reference_variable = new ClassName();

 e.g. Car c = new Car();

 The object creation code above does three actions at a


time. Declaration, instantiation and initialization
CONT.… 16

 Declaring an object
 Declaring a variable to hold an object is just like
declaring a variable to hold a value of primitive
type. Car c;
 Instantiating an object
 The new operator instantiate a class by
allocating memory for a new object of that type

new car ();


 Initializing an object

c = new Car ();


17
CONT.…

 Referencing an object fields

– Inside a class fields are accessed using their


name

– Outside of a class fields are accessed by using


the object reference, the dot(.) operator and the
name of the field
 Calling an objects method

– Use an object reference, the dot(.) operator and


18

OBJECT AS UML
Class name
e.g. Student

List of fields
e.g. name, age, sex,…

List of methods
getName()
setName(“Abel”)
19

THIS KEYWORD

 Within an instance method or a


constructor, this is a reference to the
current object
 You can refer to any member of the current
object from within an instance method or a
constructor by using this
EXAMPLE 20

public class Point {


public int x = 0;
public int y = 0;
//constructor
public Point(int x, int
y) {
this.x = x;
this.y = y;
}
}
EXAMPLE 21

The this key word is also used to height);


call constructors within the same }
class
public Rectangle(int x, int y, int
E.g. public class Rectangle { width, int height) {
this.x = x;
private int x, y;
this.y = y;
private int width, height;
this.width = width;
public Rectangle() {
this.height =
this(0, 0, 1, 1); height;
} }
}
public Rectangle(int
width, int height) {
this(0, 0, width,
22

INSTANCE MEMBERS

• Fields and methods public class Car{

String carName;
without static keyword
public void setName(String
• Hold Different value
name){
for different objects
carName = name;

• Depend on the object }

}
23

STATIC MEMBERS

• Is common to all the public class Car{

Static String carName;


objects
Public static void setName(String
• Accessed without using
name){
a particular object carName = name;

• Defined with static }

keyword }
24
STATIC MEMBERS
 Static member can be accessed before any objects of its class are
created, and without reference to any object.
 The most common example of static member is Main(). main() is
declared as static because it must be called before any objects exist.
 Static variables are, essentially global variables.

 Restrictions to static

– They can only call other static methods

– They must only access static data

– They can’t refer to this or super in any way


THANK
YOU
Asamnew Gizaw
+25122342190
[email protected]
[email protected]

You might also like