KEMBAR78
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
CLASSES, OBJECTS AND
METHODS
What is Class?
• A class is a blueprint for the object.
• It determines how an object will behave and what the
object will contain.
• In other words, it is a blueprint or a set of instruction to
build a specific type of object.
Syntax
class <class_name>
{
field;
method;
}
What is an Object?
• An object is a self-contained component which
consists of methods and properties to make a
particular type of data useful.
• Object determines the behavior of the class.
When you send a message to an object, you are
asking the object to invoke or execute one of its
methods.
Syntax
ClassName ReferenceVariable = new ClassName();
What is the Difference Between Object &
Class?
• A class is a blueprint or prototype that
defines the variables and the methods
(functions) common to all objects of a certain
kind.
• An object is a specimen of a class. Software
objects are often used to model real-world
objects you find in everyday life.
• Understand the concept of Java Classes and Objects with an
example.
• Let's take an example of developing a pet management
system, specially meant for dogs.
• You will need various information about the dogs like
different breeds of the dogs, the age, size, etc.
• You need to model real-life beings, i.e., dogs into software
entities.
• You can see the picture of three different breeds of
dogs below.
• Some of the differences you might have listed out
maybe breed, age, size, color, etc.
• If you think for a minute, these differences are also
some common characteristics shared by these dogs.
• These characteristics (breed, age, size, color) can form
a data members for your object.
• Next, list out the common behaviors of these
dogs like sleep, sit, eat, etc.
• So these will be the actions of our software
objects.
• Class - Dogs
• Data members - size, age, color, breed, etc.
• Methods- eat, sleep, sit and run.
• Now, for different values of data members (breed size, age, and
color) in Java class, you will get different dog objects.
• You can design any program using this OOPs approach.
Example Code: Class and Object
// Class Declaration
public class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); }
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo()); } }
OUTPUT
Breed is: Maltese Size is:Small Age is:2 color is: white
Object and Class Example: main outside class
In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in
another class. This is a better way than previous one.
// Class Declaration
class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } }
public class Execute{
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo()); } }
A class in Java can contain:
– fields
– methods
– constructors
– blocks
– nested class and interface
Declaration of Class
• A class is declared by use of the class keyword.
• The class body is enclosed between curly braces { and }.
• The data or variables, defined within a class are called instance variables.
• The code is contained within methods. Collectively, the methods and
variables defined within a class are called members of the class.
Declaration of Instance Variables
• Variables defined within a class are called instance variables
because each instance of the class (that is, each object of the
class) contains its own copy of these variables.
• An instance variable can be declared public or private or
default (no modifier).
• When we do not want our variable’s value to be changed
out-side our class we should declare them private.
• public variables can be accessed and changed from outside of
the class..
Java - Methods
• A Java method is a collection of statements that are grouped together to perform an
operation.
• Methods are bound to a class and they define the behavior of a class.
• When you call the System.out.println() method, for example, the system actually executes
several statements in order to display a message on the console.
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
public int addition(int a, int b)
{
return a+b;
}
Here,
public − modifier
int − return type
addition − name of the method
a, b − formal parameters
int a, int b − list of parameters
Types of Java methods
• Depending on whether a method is defined by the user, or available
in standard library, there are two types of methods:
– Standard Library Methods
– User-defined Methods
Standard Library Methods
• The standard library methods are built-in methods in Java that are
readily available for use.
• These standard libraries come along with the Java Class Library
(JCL) in a Java archive (*.jar) file with JVM and JRE.
For example,
• print() is a method of java.io.PrintSteam. The print("...") prints the
string inside quotation marks.
• sqrt() is a method of Math class. It returns square root of a number.
User-defined Method
• You can also define methods inside a class as per your
wish. Such methods are called user-defined methods.
The complete syntax for defining a Java method is:
modifier static returnType nameOfMethod (Parameter
List)
{
//
method body
}
How to call a Java Method?
• Now you defined a method, you need to use it. For that, you have to call the
method. Here's how:
myFunction();
• This statement calls the myFunction(); method that was declared earlier.
• While Java is executing the program code, it encounters myFunction(); in the code.
• The execution then branches to the myFunction() method, and executes code
inside the body of the method.
• After the codes execution inside the method body is completed, the program
returns to the original state and executes the next statement.
Complete Program of Java Method
class Main
{
public static void main(String[] args)
{
System.out.println("About to encounter a method.");
// method call
myMethod();
System.out.println("Method was executed successfully!");
}
// method definition
private static void myMethod()
{
System.out.println("Printing from inside myMethod()!");
}
}
Output:
About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!
• The method myMethod() in the above program doesn't accept any arguments. Also, the method
doesn't return any value (return type is void).
• Note that, we called the method without creating object of the class. It was possible because
myMethod() is static.
What are the advantages of using methods?
• The main advantage is code reusability. You
can write a method once, and use it multiple
times. You do not have to rewrite the entire
code each time. Think of it as, "write once,
reuse multiple times."
• Methods make code more readable and
easier to debug.
https://www.programiz.com/java-programming/methods

123 JAVA CLASSES, OBJECTS AND METHODS.ppt

  • 1.
  • 2.
    What is Class? •A class is a blueprint for the object. • It determines how an object will behave and what the object will contain. • In other words, it is a blueprint or a set of instruction to build a specific type of object. Syntax class <class_name> { field; method; }
  • 3.
    What is anObject? • An object is a self-contained component which consists of methods and properties to make a particular type of data useful. • Object determines the behavior of the class. When you send a message to an object, you are asking the object to invoke or execute one of its methods. Syntax ClassName ReferenceVariable = new ClassName();
  • 4.
    What is theDifference Between Object & Class? • A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind. • An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.
  • 5.
    • Understand theconcept of Java Classes and Objects with an example. • Let's take an example of developing a pet management system, specially meant for dogs. • You will need various information about the dogs like different breeds of the dogs, the age, size, etc. • You need to model real-life beings, i.e., dogs into software entities.
  • 6.
    • You cansee the picture of three different breeds of dogs below. • Some of the differences you might have listed out maybe breed, age, size, color, etc. • If you think for a minute, these differences are also some common characteristics shared by these dogs. • These characteristics (breed, age, size, color) can form a data members for your object.
  • 8.
    • Next, listout the common behaviors of these dogs like sleep, sit, eat, etc. • So these will be the actions of our software objects.
  • 9.
    • Class -Dogs • Data members - size, age, color, breed, etc. • Methods- eat, sleep, sit and run.
  • 10.
    • Now, fordifferent values of data members (breed size, age, and color) in Java class, you will get different dog objects. • You can design any program using this OOPs approach.
  • 11.
    Example Code: Classand Object // Class Declaration public class Dog { // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() { return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } public static void main(String[] args) { Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); } } OUTPUT Breed is: Maltese Size is:Small Age is:2 color is: white
  • 12.
    Object and ClassExample: main outside class In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in another class. This is a better way than previous one. // Class Declaration class Dog { // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() { return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } } public class Execute{ public static void main(String[] args) { Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); } }
  • 13.
    A class inJava can contain: – fields – methods – constructors – blocks – nested class and interface
  • 14.
    Declaration of Class •A class is declared by use of the class keyword. • The class body is enclosed between curly braces { and }. • The data or variables, defined within a class are called instance variables. • The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.
  • 15.
    Declaration of InstanceVariables • Variables defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. • An instance variable can be declared public or private or default (no modifier). • When we do not want our variable’s value to be changed out-side our class we should declare them private. • public variables can be accessed and changed from outside of the class..
  • 16.
    Java - Methods •A Java method is a collection of statements that are grouped together to perform an operation. • Methods are bound to a class and they define the behavior of a class. • When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Creating Method Considering the following example to explain the syntax of a method − Syntax public int addition(int a, int b) { return a+b; } Here, public − modifier int − return type addition − name of the method a, b − formal parameters int a, int b − list of parameters
  • 17.
    Types of Javamethods • Depending on whether a method is defined by the user, or available in standard library, there are two types of methods: – Standard Library Methods – User-defined Methods Standard Library Methods • The standard library methods are built-in methods in Java that are readily available for use. • These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE. For example, • print() is a method of java.io.PrintSteam. The print("...") prints the string inside quotation marks. • sqrt() is a method of Math class. It returns square root of a number.
  • 18.
    User-defined Method • Youcan also define methods inside a class as per your wish. Such methods are called user-defined methods. The complete syntax for defining a Java method is: modifier static returnType nameOfMethod (Parameter List) { // method body }
  • 19.
    How to calla Java Method? • Now you defined a method, you need to use it. For that, you have to call the method. Here's how: myFunction(); • This statement calls the myFunction(); method that was declared earlier. • While Java is executing the program code, it encounters myFunction(); in the code. • The execution then branches to the myFunction() method, and executes code inside the body of the method. • After the codes execution inside the method body is completed, the program returns to the original state and executes the next statement.
  • 20.
    Complete Program ofJava Method class Main { public static void main(String[] args) { System.out.println("About to encounter a method."); // method call myMethod(); System.out.println("Method was executed successfully!"); } // method definition private static void myMethod() { System.out.println("Printing from inside myMethod()!"); } } Output: About to encounter a method. Printing from inside myMethod(). Method was executed successfully! • The method myMethod() in the above program doesn't accept any arguments. Also, the method doesn't return any value (return type is void). • Note that, we called the method without creating object of the class. It was possible because myMethod() is static.
  • 21.
    What are theadvantages of using methods? • The main advantage is code reusability. You can write a method once, and use it multiple times. You do not have to rewrite the entire code each time. Think of it as, "write once, reuse multiple times." • Methods make code more readable and easier to debug. https://www.programiz.com/java-programming/methods