1 of 40

Java Programming - Classes

2 of 40

Classes: Class Fundamentals - objects – Assigning Object Reference Variables

3 of 40

Class Fundamentals

2 november

  • Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods
  • Class is a collection of similar objects
  • Object is an instance of a class
  • A class is declared by use of the class keyword

4 of 40

General Form of a Class

Simple class

2 november

5 of 40

Object Creation of a Class

Class

Object creation

Reference Variable-declare reference to object

2 november

Box mybox = new Box();

mybox.width = 100;

Box mybox;

6 of 40

7 of 40

Create a Class

Object Creation

2 november

public class Main

{

int x = 5;

}

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

}

}

8 of 40

Multiple Object Creation

2 november

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

System.out.println(myObj1.x);

System.out.println(myObj2.x);

} }

9 of 40

Using Multiple Classes

2 november

public class Main {

int x = 5;

}

class Second {

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

}

}

10 of 40

Java Class Attributes

Accessing Attributes

2 november

public class Main

{

int x = 5;

int y = 3;

}

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

}

}

11 of 40

Modify Attributes

Final Keyword

2 november

public class Main {

int x=10;

public static void main(String[] args) {

Main myObj = new Main();

myObj.x = 40;

System.out.println(myObj.x);

} }

public class Main {

final int x = 10;

public static void main(String[] args) {

Main myObj = new Main();

myObj.x = 25; // will generate an error: cannot assign a value to a final variable

System.out.println(myObj.x);

} }

12 of 40

Object Creation of a Class

Object creation

Output

2 november

Box mybox = new Box();

mybox.width = 100;

Volume is 3000.0

13 of 40

Output

Volume is 3000.0

Volume is 162.0

14 of 40

Assigning Object Reference Variables

Box b1 = new Box();

Box b2 = b1;

b1 and b2 both refer to the same object, they are not linked in any other way.

15 of 40

16 of 40

Introducing Methods –Constructors –this keyword –Garbage Collection–Stack Class.

17 of 40

Methods

  • A method is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a method.
  • Methods are used to perform certain actions, and they are also known as functions.

18 of 40

Methods

  • Type specifies the type of data returned by the method
  • Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:

return value;

19 of 40

Adding a Method to the Class

20 of 40

Volume is 3000.0

Volume is 162.0

21 of 40

Returning a Value

22 of 40

23 of 40

Method with Parameters

public class Main {

static void myMethod(String fname) {

System.out.println(fname + " Refsnes");

}

public static void main(String[] args) {

myMethod("Liam");

myMethod("Jenny");

myMethod("Anja");

}

}

24 of 40

Multiple Parameters

public class Main {

static void myMethod(String fname, int age) {

System.out.println(fname + " is " + age);

}

public static void main(String[] args) {

myMethod("Liam", 5);

myMethod("Jenny", 8);

myMethod("Anja", 31);

}

}

25 of 40

Static vs. Non-Static Methods

public class Main {

static void myStaticMethod() {

System.out.println("Static methods can be called without creating objects");

}

public void myPublicMethod() {

System.out.println("Public methods must be called by creating objects");

}

public static void main(String[] args) {

myStaticMethod();

Main myObj = new Main();

myObj.myPublicMethod();

}

}

Note: static method, which means that it can be accessed without creating an object of the class

26 of 40

Using Multiple Classes

public class Main {

public void fullThrottle() {

System.out.println("The car is going as fast as it can!");

}

public void speed(int maxSpeed) {

System.out.println("Max speed is: " + maxSpeed);

} }

class Second {

public static void main(String[] args) {

Main myCar = new Main();

myCar.fullThrottle();

myCar.speed(200);

} }

27 of 40

Example : Student

class Student

{

int rollno;

String name;

void insertRecord(int r, String n)

{

rollno=r;

name=n;

}

void displayInformation(){System.out.println(rollno+" "+name);

}

28 of 40

class TestStudent{

public static void main(String args[]){

Student s1=new Student();

Student s2=new Student();

s1.insertRecord(111,"Kavin");

s2.insertRecord(222,"Aryan");

s1.displayInformation();

s2.displayInformation();

} }

29 of 40

Java Constructors

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes

public class Main {

int x;

public Main()

{

x = 5;

}

public static void main(String[] args)

{

Main myObj = new Main();

System.out.println(myObj.x); } }

30 of 40

Constructor Parameters

public class Main {

int x;

public Main(int y) {

x = y;

}

public static void main(String[] args) {

Main myObj = new Main(5);

System.out.println(myObj.x);

}

}

31 of 40

Many Parameters

public class Main {

int modelYear;

String modelName;

public Main(int year, String name)

{

modelYear = year;

modelName = name;

}

public static void main(String[] args) {

Main myCar = new Main(1969, "Mustang");

System.out.println(myCar.modelYear + " " + myCar.modelName); } }

32 of 40

Example : Employee

class Employee{

int id;

String name;

float salary;

void insert(int i, String n, float s) {

id=i;

name=n;

salary=s;

}

void display(){System.out.println(id+" "+name+" "+salary);}

}

33 of 40

public class TestEmployee {

public static void main(String[] args) {

Employee e1=new Employee();

Employee e2=new Employee();

Employee e3=new Employee();

e1.insert(101,"ajeet",45000);

e2.insert(102,"irfan",25000);

e3.insert(103,"nakul",55000);

e1.display();

e2.display();

e3.display();

} }

34 of 40

this Keyword

Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.

35 of 40

this Keyword

36 of 40

this Keyword

class Student{

int rollno;

String name;

float fee;

Student(int rollno,String name,float fee){

this.rollno=rollno;

this.name=name;

this.fee=fee;

}

void display(){System.out.println(rollno+" "+name+" "+fee);}

}

37 of 40

class TestThis{

public static void main(String args[]){

Student s1=new Student(111,"ankit",5000f);

Student s2=new Student(112,"sumit",6000f);

s1.display();

s2.display();

}}

38 of 40

class Student{

int rollno;

String name;

float fee;

Student(int r,String n,float f){

rollno=r;

name=n;

fee=f;

}

void display(){System.out.println(rollno+" "+name+" "+fee);}

}

class TestThis3{

public static void main(String args[]){

Student s1=new Student(111,"ankit",5000f);

Student s2=new Student(112,"sumit",6000f);

s1.display();

s2.display(); }}

Without this Keyword

39 of 40

Garbage Collection

In traditional C++, dynamically allocated objects must be manually released by use of a delete operator. Java it handles deallocation automatically.

object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no need to explicitly destroy objects

40 of 40

Thank You