3.classes and Obj
3.classes and Obj
What is Class?
● A class is a template or blueprint that For Example:
is used to create objects. A car is an object. It has states
● A class consists of data members and (name, color, model) and its
methods. behavior (changing gear, applying
brakes).
What is Object?
● Any real-world entity that has state
and its behavior.
Class & object
Class Name
Class Variables
Class Methods
Defining a class
Heap area
Create an object
Cricketer - code snippet
Example:01 Example:02
public class Demo{ public class Demo {
int x; int x;
} public static void main(String[] args) {
Demo myObj1 = new Demo();
class TestDemo{ Demo myObj2 = new Demo();
public static void main(String[] myobj1.x = 24;
args) { myobj2.x = 55;
Demo myObj = new Demo(); System.out.println(myObj1.x);
myobj.x = 40; System.out.println(myObj2.x);
System.out.println(myObj.x); }
} }
}
Ways to initialize the instance variables
● By reference variable
● By method
● By constructor
Through reference variable
By reference variable
class Student {
int rollno;
String name;
void insertRecord(int r, String n) {
rollno = r;
name = n;
}
void displayInformation() {
System.out.println(rollno + " " + name);
}
}
By method - continued.,
class Main {
A special method
constructs/initializes the values at the time of object creation.
No return type
Has same name as class name
Automatically invoked when the object is created
name = null
Cricketer c = new Cricketer(); object creation & age = 0
color = null
constructor
role = null
invocation nationality =
null
C
Types of constructors
● Default:
1. java compiler creates a default constructor if your class
doesn't have any explicit constructor
2. Always no-argument constructor
● User defined
1. Is explicitly written in the class
To initialize the instance members with user-defined values
1. Zero-parameterized / No-argument constructor
2. Parameterized constructor
By User defined No-arg constructor
class Employee {
int id;
String name;
float salary;
Employee() {
System.out.println(“user defined no-argument
constructor executed”);
}
void display() {
System.out.println(id + " " + name + " " + salary);
}
}
Through constructor
By user defined no-argument constructor
e1.display();
e2.display();
}
}
By User defined parameterized constructor
class Employee {
int id;
String name;
float salary;
Employee(int id, String name, float salary) {
id = id;
name = name;
salary = salary;
}
void display() {
System.out.println(id + " " + name + " " +
salary);
}
}
Through constructor
Passing values for parameterized constructor
e1.display();
e2.display();
e3.display();
}
}
Through constructor
“this” keyword
class Company {
String name;
public static void main(String[] args) {
System.out.println(new Company().name);
}
}
Through constructor
Knowledge check
class Company {
String name;
public static void main(String[] args) {
Company c = new Company();
Company c1 = c;
c1.name = “Ethnus”;
c = null;
System.out. println(c.name);
}
}
Through constructor
Knowledge check
}
Through constructor
Knowledge check
}
Through constructor
Knowledge check
class Company {
String name;
void changeRef(Company that) {
that.name = this.name;
}
public static void main(String[] args) {
Company c1 = new Company();
c1.name = "Ethnus";
Company c2 = new Company();
c2.name = "Aptimithra";
c1.changeRef(c2);
System.out.println(c1.name);
System.out.println(c2.name);}
Through constructor
Knowledge check
class Company {
String name;
void changeRef(Company that) {
that.name = this.name;
that = null;
}
public static void main(String[] args) {
Company c1 = new Company();
c1.name = "Ethnus";
Company c2 = new Company();
c2.name = "Aptimithra";
c1.changeRef(c2);
System.out.println(c1.name);
System.out.println(c2.name);}
}
}
Through constructor
Code snippet
DEMO
Question: 01
/* X */ /* Y */ A. X only
class Student { CLASS Student { B. Y only
String String C. X and Y both are correct
name; name;
D. X and Y both are incorrect
int marks; int marks;
char char
section; section;
} }
Question: 03
class Car {
int Wheels;
void swap(Car other) {
other = this; } }
public class Main { A. 48
public static void main(String[] args) { B. 44
Car C1 = new Car(); C. 84
C1.Wheels = 4; D. 88
Car C2 = new Car();
C2.Wheels = 8;
C2.swap(C1);
System.out.print(C1.Wheels + "");
System.out.print(C2.Wheels);
}
}
Question: 06
class Car {
int Wheels;
void swap(Car other) {
other.Wheels = this.Wheels;
}} A. 48
public class Main { B. 44
public static void main(String[] args) { C. 84
Car C1 = new Car(); D. 88
C1.Wheels = 4;
Car C2 = new Car();
C2.Wheels = 8;
C1.swap(C2);
System.out.println(C1.Wheels);
System.out.println(C2.Wheels);
}}
Question: 07
class Test {
int i;
}
class Main { A. 0
public static void main(String args[]) { B. Garbage Value
Test t; C. Compilation Error
System.out.println(t.i); D. Runtime Error
}
Question: 09
System.out.println(f.process(result));
}
int process(int a) {
Question: 10
class Test {
void start() {
String stra = "do";
String strb = method(stra);
System.out.print(": " + stra + strb);
A. dogood : dogoodgood
}
B. dogood : gooddogood
String method(String stra) {
C. dogood : dodogood
stra = stra + "good";
D. dogood : dogood
System.out.print(stra);
return " good";
} }
class Main {
public static void main(String[] args) {
Test obj = new Test();
obj.start(); } }
Question: 12
class User{
int age;
String name;
} A. Error
public class Main{ B. Null pointer Exception
public static void main(String[] args) { C. 100
User u1=new User(); D. null
u1.name=100;
u1=null;
System.out.println(u1.name);
}
}
Question: 17
class User{
int age;
String name; }
public class Main{
public static void main(String[] args) { A. A
User u1=new User(); B. Null
u1.name="A"; C. Error
User u2=u1; D. C
u2=null;
u2=new User();
u1.name = "c";
u2.name=u1.name;
u1=null;
System.out.println(u2.name);
} }
Question: 18
class User{
int age;
String name;
} A. A
public class Main{ B. B
public static void main(String[] args) { C. Error
User u1=new User(); D. Cannot execute
u1.name="A";
User u2=u1;
u1.name = "B";
System.out.println(u1.name);
}
Question: 19
class User{
int age;
String name;
} A. A
public class Main{ B. Null pointer Exception
public static void main(String[] args) { C. Cannot execute
User u1=new User(); D. null
u1.name="A";
u1=null;
System.out.println(u1.name);
}
}
Question: 20
class User{
int age;
String name;
} A. A
public class Main{ B. Null pointer Exception
public static void main(String[] args) { C. Cannot execute
User u1=new User(); D. null
u1.name="A";
User u2=new User();
u2=u1;
u1=null;
System.out.println(u1.name);
Question: 21
class User{
int age;
String name;
} A. A
public class Main{ B. B
public static void main(String[] args) { C. Error
User u1=new User(); D. u1 address
u1.name="A";
User u2=u1;
u2=new User();
u2.name=”B”;
System.out.println(u1.name);
Question: 22
class Human{
String name;
int age;
Human(String name, int age){
A. Null 0
this.name=name;
B. Adam 32000
this.age=age;
C. Error
} }
D. 32000 Adam
class Main{
public static void main(String args[]){
Human adam = new Human("Adam",32000);
System.out.println(adam.name);
System.out.println(adam.age);
} }
Question: 23
class Human{
String name;
int age; A. method foo
void foo() { method boo
this.boo(); A. method boo
System.out.println("method foo");} method boo
void boo() { A. method foo
System.out.println("method boo"); method foo
A. method boo
} }
method foo
class Main{
public static void main(String args[]){
Human adam = new Human();
adam.foo();
} }
Question: 24
class User{
String name;
void sayHello(User randomUser){
randomUser.name = "HiddenGuy";
A. HiddenGuy
randomUser=null;
B. AwesomeGuy
} }
C. AwesomeGuy HiddenGuy
public class Main{
D. Error
public static void main(String[] args) {
User u1=new User();
u1.name="Awesome Guy";
u1.sayHello(u1);
System.out.println(u1.name);
}
Question: 25
class User{
String name;
String address;
User(String x,String y){ A. X,Y
name=x; B. null
address=y; null
System.out.println(name); A. Error
System.out.println(address); B. name,address
} }
public class Main{
public static void main(String[] args) {
User u1=new User();
}
Question: 26
class Temp{
Temp() {
this(5); A. 15
System.out.println("Inside Temp"); } 5
Temp(int x) { Inside temp
this(5,15); A. 75
System.out.println(x); } 5
Temp(int x,int y) { Inside temp
System.out.println(x * y); A. Error in line number 5. No
}} method found named "this"
public class Main{ B. Inside temp
public static void main(String[] args) {
new Temp();
} }
Question: 27
class User{
String name;
void sayHello(User randomUser){ A. BadGuy
this.name = randomUser.name; BadGuy
} } A. BadGuy
public class Main{ AwesomeGuy
public static void main(String[] args) { A. AwesomeGuy
User u1=new User(); BadGuy
u1.name="Awesome Guy"; A. AwesomeGuy
User u2=new User(); AwesomeGuy
u1.name="Bad Guy";
u1.sayHello(u2);
System.out.println(u1.name);
System.out.println(u2.name);
Question: 28
class Fruit {
String name;
Fruit(String name) {
this.name = name;
} A. null Orange
} B. null null
class Main { C. Error
public static void main(String args[]) { D. Orange
Fruit orange = new Fruit("Orange");
Fruit mango = new Fruit();
System.out.println(mango.name + " " +
orange.name);
}
}
Question: 29
class User {
String name;
public User callMe(User randomUser) {
randomUser = this;
return null;
A. Random Technologies
}}
public class Main { B. Null
public static void main(String[] args) { C. Null pointer Exception
User u1 = new User(); D. Random null
u1.name = "Random";
User u2 = new User();
u2.name = "Technologies";
u2 = u1.callMe(u2);
System.out.println(u1.name + " " + u2.name);
}
}
Question: 30
class User {
int id;
void generateId(int id) {
this.id = id * 5;
}} A. 25 25
public class Main { B. 50 50
public static void main(String[] args) { C. 00
User u1 = new User(); D. 50 0
u1.generateId(5);
User u2 = u1;
u2.generateId(10);
u1 = u2;
System.out.println(u1.id + " " + u2.id);
}}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://learn.codemithra.com