Sie sind auf Seite 1von 30

Chapter Four:

Java Objects and Classes

Mulugeta G.

1
Introduction

• Java is an object-oriented
programming language
• In object-oriented programming
(OOP), programs are organized
into objects
• The properties of objects are
determined by their class
• Objects act on each other by
passing messages
2
OOP Concepts
• Object • Encapsulation

• Class • Abstraction

• Polymorphism • Method

• Inheritance • Message

3
Object
• A programming entity that contains state (data) and
behavior (methods).
– State: A set of values (internal data) stored in an
object.
– Behavior: A set of actions an object can perform,
often reporting or modifying its internal state.
• Objects can be used as part of larger programs to solve
problems.
• Example 1: Dogs
– States: name, color, breed, and “is hungry?”
– Behaviors: bark, run, and wag tail

• Example 2: Cars
– States: color, model, speed, direction
– Behaviors: accelerate, turn, change gears
4
What is an Object?
• An Object has two primary components:
– state – properties of the object
– behavior – operations the object can
perform

• Examples
object state behavior
dog breed, isHungry eat, bark
grade book grades mean, median
light on/off switch

State = Properties Behavior = Method 5


Constructing Objects
o We use the new keyword to construct a
new instance of an Object.
o We can assign this instance to a variable
with the same type of the Object

LightSwitch ls = new LightSwitch();


o Note: classes define new datatypes !

6
Class
• A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.

Circle

centre
radius
circumference()
area()

• Class= fields + methods


7
Java Class
• A class is the blueprint from which
individual objects are created.
• A class defines a new data type which
can be used to create objects of that
type.
• Thus, a class is a template for an object,
and an object is an instance of a class.

8
Classes and Objects
• A Java program consists of one or more
classes.
• A class is an abstract description of objects.
• Here is an example class:
class Dog {
...
description of a dog goes here
...
}
• Here are some objects of that class:

9
Creating objects of a class
• Object – block of memory that contains
space to store all instance variables.
• Objects are created dynamically using the
new keyword. It’s called instantiating an
object.
• Example:
class_name object_name;
object_name=new class_name();
or
class_name object_name=new
class_name();
Object’s created:
ObjectName.VariableName 10
Constructor in Java
• Constructor is a special type of method
that is used to initialize the object.

• Constructor is invoked at the time of object


creation.

• Rules for creating constructor


• Constructor name must be same as its
class name
• Constructor Name=Class Name

• Constructor must have no explicit return11


Types of Constructors
• Default constructor (No-argument
constructor)
• A constructor that have no parameter
• Default values to the object like 0, null
etc. depending on the type.
<class_name>()
{

• Parameterized constructor
• A constructor that have parameters
• Used to provide different values to the
distinct objects.
<class_name>(parameter list)
{
} 12
Example
class Student {
int id;
String name;
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]) {
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
}
}
class_name object_name=new
class_name(); 13
Example:
class Student }  
{   public static void m
int id;   ain(String args[])
String name;   {  
Student(int i,String n) Student s1 = new S
{   tudent(111,“Abay")
id = i;   ;  
name = n;  
}   Student s2 = new S
tudent(222,“Gebiss
void display(){ a"); 
 
System.out.println(id s1.display();  
+" "+name); s2.display();  
14
}  
Constructor Overloading
• Constructor overloading is a technique in
Java in which a class can have any
number of constructors that differ in
parameter lists.
• The compiler differentiates these
constructors by taking into account the
number of parameters in the list and
their type.
Method Overloading
• class have multiple methods by same
15
Constructor Overloading

class Student void display()


{ {
int id; System.out.println(id+"
String name; "+name+" "+age);
int age; }
Student(int i,String public static void main(String
n) args[])
{ {
id = i; Student s1 = new
name = n; Student(111,“yemane");
} Student s2 = new
Student(int i,String Student(222,"Aryan",25);
n,int a) s1.display();
{ s2.display();
id = i; }
name = n; }
age=a; 16
}
class Calculation
{
void sum(int a,int b)
{
 Method Overloading System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
public static void main(String args[
{
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
} 17
Difference between constructor and method ?

Constructor Method
• Constructor is used to • Method is used to
initialize the state of an expose behaviour of
object. an object.
• Constructor must not • Method must have
have return type. return type.
• Constructor is invoked • Method is invoked
implicitly. explicitly.
• The java compiler
• Method is not
provides a default
provided by compiler
constructor if you don't
in any case.
have any constructor.
• Constructor name must • Method name may or
be same as the class may not be same as18
name. class name.
STATIC KEYWORD

class class_name
{
public static void main(String args[])
{
data / field / instance variables;
methods / instance methods;
}
}
Object creation – class_name object_name=new
class_name();
object_name.variables;
object_name.methods();
Every time class instantiated / object created a new
copy of each of variable and methods are created.
Then accessed with (.) operator.
19
• Define a member that is common to all object
not to specific object.
• Static variables – called as class variable
• Static methods – called as class methods
 static variable:
 declare any variable as static, it is known
static variable.
 static method:
 static keyword with any method, it is known
as static method.
 A static method belongs to the class rather
than object of a class.
 invoked without the need for creating an
instance of a class. 20
• Final Keyword In Java
• The final keyword in java is used to restrict the
user.
• Generally all the variables / methods by default
overridden in subclasses
• Final – to prevent the subclass from overriding
the members of superclass.
• The final keyword can be used in many context.
Final can be:
• Variable - make any variable as final, you
cannot change the value of final variable(It will
be constant). 21
class Bike class Bike

{ {
final int final void run()
speedlimit=90;//final{
variable System.out.println("running");
void run() }}
{ class Honda extends Bike
speedlimit=400; {
} void run()
public static void {
main(String args[]) System.out.println("running safely
{ }
Bike obj=new Bike();public static void main(String args
obj.run(); {
} Honda honda= new Honda();
}//end of class honda.run();
}} 22
final class Bike
{

}
class Honda extends Bike
{
void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}

23
Finalizer methods:
• Constructor – used to initialize an object when
it’s declared – initialization

• Automatic garbage collection run time, It’s


frees up the memory resources used by objects.

o But objects still hold:– non object resources


o Garbage collector:– can’t free those resources
use finalizer method like desctructor.

Syntax:
finalize()
24
 Abstract keyword
• Opposite to final – make use of the same
method name in subclass
• Overridden is compulsory.
 Abstract class in Java
• class that is declared with abstract
keyword, is known as abstract class
abstract class <class_name>
{

 abstract method
• method that is declared as abstract -
abstract method
• does not have implementation, it’s in
derived class
25
Syntax:
abstract class Bike // abstract class
{
abstract void run(); // abstract
method
}
class Honda extends Bike
{
void run()
{
System.out.println("running
safely..");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run(); 26
Summary on Objects and Classes

• When writing an object-oriented


program, we define classes, which
describe categories of objects, and
the states and behaviors that they
have in common.
• We then create objects which belong
to classes, and share the common
features of their class.
• Objects interact with each other by
passing messages.

27
Con’t
• Object is the physical as well as logical
entity whereas class is the logical entity
only.
• An entity that has state and behavior is
known as an object 
• Object is an instance of a class. Class is
a template or blueprint from which objects
are created. So object is the instance(result)
of a class.

28
Con’t
• Object Definitions:
– Object is a real world entity.
– Object is a run time entity.
– Object is an entity which has state and behavior.
– Object is an instance of a class.
• Calss in java
– A class is a group of objects which have common properties.
– It is a template or blueprint from which objects are created.
– It is a logical entity. It can't be physical.
A class in Java can contain:
– fields
– methods
– constructors
– blocks
– nested class and interface 29
Thank You

?
30

Das könnte Ihnen auch gefallen