0% found this document useful (0 votes)
37 views16 pages

Java Programming Lesson 2

Uploaded by

rajeev0305
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)
37 views16 pages

Java Programming Lesson 2

Uploaded by

rajeev0305
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/ 16

2

JAVA Object and Classes

 KIIT 2014
Objectives
After completing this lesson, you should be able
to do the following:
• Define Object-Oriented Programming
• Define Objects
• Define Classes
• Use of Constructors
• Create an Object
• Explain accessing instance variable and method
• Define JAVA packages

 KIIT 2014
Object-Oriented
Programming
• Java is an Object-Oriented Language. As a
language that has the Object Oriented feature,
Java supports the following fundamental
concepts:
– Polymorphism
– Inheritance
– Encapsulation
– Abstraction
– Classes
– Objects
– Instance
– Method
– Message Parsing

 KIIT 2014
What is an Object?
• An object is a software bundle of related
state and behaviour. Software objects are
often used to model the real-world objects
that you find in everyday life.

 KIIT 2014
Software Objects
• Bundling code into individual software objects
provides a number of benefits, including:
– Modularity: The source code for an object can be
written and maintained independently of the source
code for other objects. Once created, an object can be
easily passed around inside the system.
– Information-hiding: By interacting only with an
object's methods, the details of its internal
implementation remain hidden from the outside world.
– Code re-use: If an object already exists (perhaps
written by another software developer), you can use that
object in your program. This allows specialists to
implement/test/debug complex, task-specific objects,
which you can then trust to run in your own code.

 KIIT 2014
Software Objects
– Pluggability and debugging ease: If a particular
object turns out to be problematic, you can simply
remove it from your application and plug in a different
object as its replacement.

 KIIT 2014
What is a Class?
• A class is a blueprint or prototype from
which objects are created.
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}

 KIIT 2014
Variable Types
• A class can contain any of the following
variable types.
– Local variables: Variables defined inside
methods, constructors or blocks are called
local variables.
– Instance variables: Instance variables are
variables within a class but outside any
method.
– Class variables: Class variables are variables
declared within a class, outside any method,
with the static keyword.

 KIIT 2014
Constructors
• Every class has a constructor. If we do not
explicitly write a constructor for a class the Java
compiler builds a default constructor for that
class.
• Each time a new object is created, at least one
constructor will be invoked. The main rule of
constructors is that they should have the same
name as the class. A class can have more than
one constructor.

 KIIT 2014
Creating an Object
• Object is created from a class. In Java the
new keyword is used to create new
objects.
• 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.
 KIIT 2014
Creating an Object
• Example:
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");
}
}

 KIIT 2014
Accessing Methods
• Instance variables and methods are
accessed via created objects. To access an
instance variable the fully qualified path
should be as follows.

/* First create an object */


ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();
}

 KIIT 2014
• Example:
Accessing Methods
public class Puppy{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :"+ name );
}
public void setAge(int age ){
puppyAge = age;
}
public int getAge(){
System.out.println("Puppy's age is :"+
puppyAge );
return puppyAge;
}

 KIIT 2014
Accessing Methods
• Example…
public static void main(String[]args){
/* Object creation */
Puppy myPuppy =newPuppy("tommy");
/* Call class method to set puppy's age */
myPuppy.setAge(2);
/* Call another class method to get puppy's age
*/
myPuppy.getAge();
/* You can access instance variable as follows
as well */
System.out.println("Variable Value :"+
myPuppy.puppyAge );
}
}

 KIIT 2014
Java Package
• In simple, it is a way of categorizing the
classes and interfaces. When developing
applications in Java, hundreds of classes
and interfaces will be written, therefore
categorizing these classes is a must as
well as makes life much easier.

 KIIT 2014
Summary
In this lesson, you should have learned how to:
• Define Object-Oriented Programming
• Define Objects
• Define Classes
• Use of Constructors
• Create an Object
• Explain accessing instance variable and method
• Define JAVA packages

 KIIT 2014

You might also like