You are on page 1of 22

Object and Class In Java

TYPES OF PROGRAMMING

There are two types of programming languages


1.  Low-level language
a)  Machine language
b)  Assembly language

2.  High-level language
a)  Procedural-Oriented language
b)  Problem-Oriented language
c)  Natural language
DISADVANTAGES OF FUNCTIONAL PROGRAMMING

Drawbacks of functional programming in general

o Writing pure functions is easy, but combining them into a complete application is where
things get hard
o For many people, recursion doesn’t feel natural
o Pure functions and I/O don’t mix
o Purely functional vocabularies work slower than the hash tables,
not suitable for algorithms in graphs (due to slow work)
CLASS & OBJECTS

What is Object?
o In real-world an entity that has state and its behavior is known as an object
For Example
o A Car is an object. It has states (name, color, model) and its behavior (changing gear,
applying brakes)

What is Class?
o A class is a template or blueprint that is used to create objects
o Class representation of objects and the sets of operations that can be applied to such
objects
o A class consists of data members and methods
DEFINING A CLASS IN JAVA

Syntax Example
public class class_name { public class Car {
Data Members; double color;
Methods; double model;
} }
CLASS MEMBERS

Example

public class Cube {


int length;
int breadth;
}

o A member cannot be redeclared within a class


o No member can be added elsewhere other than in the class definition
o Data and functions are members
DIFFERENT WAYS TO CREATE AN OBJECT
DIFFERENT WAYS TO CREATE AN OBJECT

o Using new keyword


MyObject object = new MyObject();

o Using Class.forName()
MyObject object = (MyObject)
Class.forName("subin.rnd.MyObject").newInstance();

o Using clone()
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

o Using object deserialization


ObjectInputStream inStream = new ObjectInputStream(anInputStream);
MyObject object = (MyObject) inStream.readObject();
CREATE AN OBJECT

Example:01 Example:02
public class Myclass { public class Main {
int x; int x;
} public static void main(String[] args) {
class Main { Main myObj1 = new Main();
public static void main(String[] Main myObj2 = new Main();
args) { myobj1.x = 24;
Myclass myObj = new Myclass(); myobj2.x = 55;
myobj.x = 40; System.out.println(myObj1.x);
System.out.println(myObj.x); System.out.println(myObj2.x);
} }
} }
CREATE AN OBJECT

Using Multiple Classes


o Create an object of a class and access it in another class.

Myclass.java Mainclass.java
public  class  Myclass { class  Main {
int  x =  5; public  static  void  main(String[] args) {
} MyClass  myObj  =  new  MyClass();
System.out.println(myObj.x);
}
}
3 WAYS TO INITIALIZE OBJECT

o By reference variable

o By method

o By constructor
3 WAYS TO INITIALIZE OBJECT

Object and Class Example: Initialization through reference


class  Student {   class  Main {  
int  id;   public  static  void  main(String  args[]) {  
String  name;   Student  s1 = new  Student();
} s1.id = 101;
s1.name = "Sonoo";
System.out.println(s1.id + " " + s1.name);
}  

3 WAYS TO INITIALIZE OBJECT

Object and Class Example: Initialization through method


class  Student {
int  rollno;  
String  name;  
void  insertRecord(int  r,  String  n) {  
rollno = r;  
name = n;  
}  
void  displayInformation() {
System.out.println(rollno + " " + name);
}  

3 WAYS TO INITIALIZE OBJECT

Object and Class Example: Initialization through method


class Main {  
public  static  void  main(String  args[]) {  
Student  s1 = new  Student();  
Student  s2 = new  Student();  
s1.insertRecord(111, "Karan");  
s2.insertRecord(222, "Aryan");  
s1.displayInformation();  
s2.displayInformation();  
}  
}
3 WAYS TO INITIALIZE OBJECT

Object and Class Example: Initialization through a constructor


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);
}
}
3 WAYS TO INITIALIZE OBJECT

Object and Class Example: Initialization through a constructor


public  class  Main  {  
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();  

}
JAVA CLASS ATTRIBUTES

class attributes are variables within a class


Example
class Myclass { class Myclass {
int x = 5; int x;
} }
class Main { class Main {
public static void main(String args[]) public static void main(String args[])
{ {
Main myobj = new Main(); Main myobj = new Main();
System.out.println(myobj.x); myobj.x = 40;
} System.out.println(myobj.x);
} }
}
JAVA CLASS ATTRIBUTES

Example:01 Example:02
public class Myclass { public class Main {
int x = 10; final int x = 10;
} public static void main(String[] args)
class Main { {
public static void main(String[] args) Main myObj = new Main();
{ myObj.x = 25; // error
Main myObj = new Main(); System.out.println(myObj.x);
myObj.x = 25; }
System.out.println(myObj.x); }
}
}
LOGICAL SNIPPETS

class Test {   class Test {  


int i; int i;
} }
class Main {    class Main {  
public static void main(String public static void main(String args[])
args[]) {      {      
Test t=null;      Test t = new Test();      
System.out.println(t.i); System.out.println(t.i);   
} }
}  }
LOGICAL SNIPPETS

class Test {
void start() {
String stra = "do";
String strb = method(stra);
System.out.print(": " + stra + strb);
}
String method(String stra) {
stra = stra + "good";
System.out.print(stra);
return " good";
}
}
class Main {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
}

You might also like