Class Object Methods This
Class Object Methods This
Class
Computer Engineering
Class
▪ Defines new data type (primitive ones are not enough).
▪ For Example : SmartPhone, person, student
▪ This new types can be used to create objects of that type
▪ So, a class is a template for an object and an object is an instance
of a class
Object Definitions:
•An object is a real-world entity.
•An object is a runtime entity.
•The object is an entity which has state and behavior.
•The object is an instance of a class.
▪ Syntax:
ClassName varName = new ClassName();
▪ Example:
SmartPhone iPhone = new SmartPhone();
iPhone.storage = 8000;
Public Access: Any variable or method is visible to the entire class in which it
is defined. But, to make a member accessible outside with objects, we
simply declare the variable or method as public. A variable or method
declared as public has the widest possible visibility and accessible
everywhere.
Private Access: private fields have the highest degree of protection. They are
accessible only with their own class. They cannot be inherited by
subclasses and therefore not accessible in subclasses. In the case of
overriding public methods cannot be redefined as private type.
Own class
✔ ✔ ✔ ✔ ✔
Sub class
in same
package ✔ ✔ ✔ ✔ 🗶
Other
classes
In same ✔ ✔ ✔ 🗶 🗶
package
Sub class
in other
package ✔ ✔ 🗶 ✔ 🗶
Other
classes
In other ✔ 🗶 🗶 🗶 🗶
package
Objectname.methodname
▪ Example:
Box b = new Box();
b.width = 10;
b.height = 15;
b.breadth = 20;
double volume = b.getVolume();
A1 A2
x=20 X=10
Y=30 y=20
A1.caluclatearea() return x*y=20*30 = 600
A2.caluclatearea() return x*y=10*20 = 200
String manufacturer;
String model;
double storage;
double screenSize;
22
}
}
▪ Syntax
▪ Example
public ClassName (parameter-list)
{
statements;
}
29
double noOfBreath;
double luck;
String name;
public Human()
{
noOfBreath = 1000;
luck = 0.5;
}
public Human(String n)
{
name = n;
}
public Human(double b, double l, String n){
noOfBreath = b; luck = l; name = n;
}
} 31
double noOfBreath;
String name;
String manufacturer;
String model;
double storage;
double screenSize;
double noOfBreath;
double luck;
String name;
public Human()
{
noOfBreath = 1000;
luck = 0.5;
}
public Human(String name)
{
this();
this.name = name;
}
}