Java Classes and Objects Lesson 2025
Java Classes and Objects Lesson 2025
Classes in Java
• A class is a blueprint from which individual objects are created.
• Here we are going to see how we can create our own classes in Java and
define parameters within those classes.
• Previously we have seen that the parameters defined within a class defines
what the objects will be made of.
• In that case the state and behavior of objects expected to emanate from a
class are what we define as parameters within a class.
• Take an example of a class known as Dog.
• In this class we can have objects such as MyDog.
• MyDog can have state such as color, name, age
• It can also have behavior such as eating, barking, sleeping etc
• The class would be created as shown below
Java Code to Create A Class Known as Dog
public class Dog{ • All states are declared just like ordinary
String breed; variables in java.
int age; • Behavior is declared as a method in java.
String color; • A method in Java follows the following
syntax.
void barking(){ Datatype MethodName () {
} }
void hungry(){ • Inside the braces one can put what the
} method is expected to return or output.
void sleeping(){ • So in this case we have methods such as
Void barking () { }
}
}
Example two
• Lets create a class called car with Public Class car {
the following features. String color;
• State- color, brand, Model String Brand;
• Behavior- Move, stop, hoot. String Model;
Void Move (){ }
Void Stop() { }
Void hoot () { }
}
Cont.
• A class can have any of the types of variables. i.e Local variable,
Instance variable, etc.
• A class can have any number of methods to access the value of
various kinds of methods. In the example 1, barking(), hungry() and
sleeping() are methods.
• In order to make it possible for objects to be created from classes,
Java depends heavily on constructors.
• Lets look at methods and constructors more deeply.
Methods in Java
• A method is a block of code or collection of statements or a set of
code grouped together to perform a certain task or operation.
• They are also referred to as functions.
• It is used to achieve the reusability of code. We write a method once
and use it many times.
• We do not require to write code again and again. It also provides
the easy modification and readability of code, just by adding or
removing a chunk of code.
• The method is executed only when we call or invoke it.
• In classes and objects, methods are used to implement behaviors.
Creating A method
• A method must be declared within a class. It is defined with the name of
the method, followed by parentheses ().
• A method in Java follows the following syntax.
Public class ClassName{
Modifier Datatype methodName () {
}}
• Example think of a class known as cat with method sound.
public class Cat {
static void sound(){
System.out.println("Meow");
}
Cont.
• Modifiers include the following:
• Static
• Public
• Private
• If you omit the modifier (i.e fail to specify the modifier it takes public
modifier by default)
• For example sound(){} is the same as public sound (){}
• Note that it is advisable to use public only when you want to create
an instance (object of a class) where such is not the case always use
static.
Cont.
• This is so because static allows you to output the value of the method
without necessarily having to create an object of a class unlike public
and private in which you first have to create the object of a class.
• Note that when you use static, private or public together with the
void datatype you don’t need to have a return statement in the body
of the method to specify what the output will be. You can just use
Syestem.out.println()
• On the other hand if you use static, public, or private modifiers with
any other datatype you must have a return statement in the body.
Check the two examples in the next page.
Cont. Example to compare how we output from void datatype
and other datatypes
MyClass(int i ) {
x = i;
}
}
• In this constructor the value of i is not fixed since a user can pass any
value of x.
• Thus if you have two objects one you assign it the value of int x to be
20 and the other to be 50 this constructor will accept that.
Objects
• An 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 and an
object type.
• Instantiation: The 'new' keyword is used to instantiate or create the
object.
• Initialization: The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.
Example 1
public class Puppy{
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}