0% found this document useful (0 votes)
3 views28 pages

Understanding Java Object-Oriented Concepts

The document provides an overview of Object-Oriented Programming in Java, focusing on concepts such as classes, objects, instance fields, methods, constructors, and dot notation. It includes code examples demonstrating how to create and manipulate objects, use constructors, and define methods with parameters. Additionally, it explains the significance of null values and the scope of variables within methods.

Uploaded by

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

Understanding Java Object-Oriented Concepts

The document provides an overview of Object-Oriented Programming in Java, focusing on concepts such as classes, objects, instance fields, methods, constructors, and dot notation. It includes code examples demonstrating how to create and manipulate objects, use constructors, and define methods with parameters. Additionally, it explains the significance of null values and the scope of variables within methods.

Uploaded by

zarif.alam88
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object-Oriented Java

By Jimmy | 5727 | Lead Programmer


Java objects’ state and behavior
● In Java, instances of a class are known as objects. Every object has state and
behavior in the form of instance fields and methods respectively.
Java objects’ state and behavior
1. public class Person {
2. // state of an object
3. int age;
4. String name;
5. // behavior of an object
6. public void set_value() {
7. age = 20;
8. name = "Robin";
9. }
10. public void get_value() {
11. [Link]("Age is " + age);
12. [Link]("Name is " + name);
13. }
14. // main method
15. public static void main(String [] args) {
16. // creates a new Person object
17. Person p = new Person();
18.
19. // changes state through behavior
20. p.set_value();
21. }
22. }
23.
Java instance
● Java instances are objects that are based on classes. For example, Bob may
be an instance of the class Person.
● Every instance has access to its own set of variables which are known as
instance fields, which are variables declared within the scope of the instance.
Values for instance fields are assigned within the constructor method.
Java instance
1. public class Person {
2. int age;
3. String name;
4. // Constructor method
5. public Person(int age, String name) {
6. [Link] = age;
7. [Link] = name;
8. }
9. public static void main(String[] args) {
10. Person Bob = new Person(31, "Bob");
11. Person Alice = new Person(27, "Alice");
12. }
13. }
14.
Java dot notation
● In Java programming language, we use . to access the variables and
methods of an object or a Class.
● This is known as dot notation and the structure looks like this-
● instanceOrClassName.fieldOrMethodName


Java dot notation
1. public class Person {
2. int age;
3. public static void main(String [] args) {
4. Person p = new Person();
5.
6. // here we use dot notation to set age
7. [Link] = 20;
8.
9. // here we use dot notation to access age and print
10. [Link]("Age is " + [Link]);
11. // Output: Age is 20
12. }
13. }
14.
Constructor Method in Java
● Java classes contain a constructor method which is used to create instances
of the class.
● The constructor is named after the class. If no constructor is defined, a default
empty constructor is used.

Constructor Method in Java
1. public class Maths {
2. public Maths() {
3. [Link]("I am constructor");
4. }
5. public static void main(String [] args) {
6. [Link]("I am main");
7. Maths obj1 = new Maths();
8. }
9. }
10.
Creating a new Class instance in Java
● In Java, we use the new keyword followed by a call to the class constructor in
order to create a new instance of a class.
● The constructor can be used to provide initial values to instance fields.

Creating a new Class instance in Java
1. public class Person {
2. int age;
3. // Constructor:
4. public Person(int a) {
5. age = a;
6. }
7. public static void main(String [] args) {
8. // Here, we create a new instance of the Person class:
9. Person p = new Person(20);
10. [Link]("Age is " + [Link]); // Prints: Age is 20
11. }
12. }
13.
Reference Data Types
● A variable with a reference data type has a value that references the memory
address of an instance. During variable declaration, the class name is used
as the variable’s type.
Reference Data Types
1. public class Cat {
2. public Cat() {
3. // instructions for creating a Cat instance
4. }
5.
6.
7. public static void main(String[] args) {
8. // garfield is declared with reference data type `Cat`
9. Cat garfield = new Cat();
10. [Link](garfield); // Prints: Cat@76ed5528
11. }
12. }
13.
Constructor Signatures
● A class can contain multiple constructors as long as they have different
parameter values. A signature helps the compiler differentiate between the
different constructors.
● A signature is made up of the constructor’s name and a list of its parameters.

Constructor Signatures
1. // The signature is `Cat(String furLength, boolean hasClaws)`.
2. public class Cat {
3. String furType;
4. boolean containsClaws;
5.
6.
7. public Cat(String furLength, boolean hasClaws) {
8. furType = furLength;
9. containsClaws = hasClaws;
10. }
11. public static void main(String[] args) {
12. Cat garfield = new Cat("Long-hair", true);
13. }
14. }
15.
null Values
● null is a special value that denotes that an object has a void reference.
null Values
1. public class Bear {
2. String species;
3. public Bear(String speciesOfBear;) {
4. species = speciesOfBear;
5. }
6. public static void main(String[] args) {
7. Bear baloo = new Bear("Sloth bear");
8. [Link](baloo); // Prints: Bear@4517d9a3
9. // set object to null
10. baloo = null;
11. [Link](baloo); // Prints: null
12. }
13. }
14.
The body of a Java method
● In Java, we use curly brackets {} to enclose the body of a method.
● The statements written inside the {} are executed when a method is called.

The body of a Java method
1. public class Maths {
2. public static void sum(int a, int b) { // Start of sum
3. int result = a + b;
4. [Link]("Sum is " + result);
5. } // End of sum
6.
7. public static void main(String [] args) {
8. // Here, we call the sum method
9. sum(10, 20);
10. // Output: Sum is 30
11. }
12. }
13.
Method parameters in Java
● In java, parameters are declared in a method definition. The parameters act
as variables inside the method and hold the value that was passed in. They
can be used inside a method for printing or calculation purposes.
● In the example, a and b are two parameters which, when the method is
called, hold the value 10 and 20 respectively.
Method parameters in Java
1. public class Maths {
2. public int sum(int a, int b) {
3. int k = a + b;
4. return k;
5. }
6. public static void main(String [] args) {
7. Maths m = new Maths();
8. int result = [Link](10, 20);
9. [Link]("sum is " + result);
10. // prints - sum is 30
11. }
12. }
13.
Java Variables Inside a Method
● Java variables defined inside a method cannot be used outside the scope of
that method.
Java Variables Inside a Method
1.
2. //For example, `i` and `j` variables are available in the `main` method only:
3.
4.
5. public class Maths {
6. public static void main(String [] args) {
7. int i, j;
8. [Link]("These two variables are available in main
method only");
9. }
10. }
11.
Returning info from a Java method
● A Java method can return any value that can be saved in a variable. The
value returned must match with the return type specified in the method
signature.
● The value is returned using the return keyword.

Returning info from a Java method
1. public class Maths {
2. // return type is int
3. public int sum(int a, int b) {
4. int k;
5. k = a + b;
6.
7. // sum is returned using the return keyword
8. return k;
9. }
10. public static void main(String [] args) {
11. Maths m = new Maths();
12. int result;
13. result = [Link](10, 20);
14. [Link]("Sum is " + result);
15. // Output: Sum is 30
16. }
17. }
18.
Declaring a Method
● Method declarations should define the following method information: scope
(private or public), return type, method name, and any parameters it receives.
Declaring a Method
1. // Here is a public method named sum whose return type is int and has two int
parameters a and b
2. public int sum(int a, int b) {
3. return(a + b);
4. }
5.
Any Questions?

You might also like