Class, Object & Creating an object
Hello I'm going to explain what is, and how to use , A sample code on calling methods & giving values to objects.
Object Human
Object
- Objects are common features and behaviours.
- We can classify anything as an object.
When we take a human,
The common features\properties. Behaviors\Functions.
height run
weight think
hands walk
legs speak
hair jump
talents eat
color sing
eyes dance
nose
age
- The red color words are values they are changeable.
- When we take features, the values will only change but the feature is the same.
- When we take an object the features are common but the behaviors would be changed.
Class
- When we're making a template by considering an object using features & behaviors its called a class.
Human block ------- is a Template
Features-------- are Variables
Behaviors -------are Methods
Object--------is a Class
Consider this code sample:
class Human{
height;
weight;
eyes;
legs;
hands;
name;
void walk(){
}
void eat(){
}
void drive(){
Creating an object
1. Human amal = new Human();
Indications: RED: Class
GREEN: Object Name
BLACK: Keyword (new is a keyword)
D.GREEN: Semicolon (code end)
BLUE: Parameter (Data In\Out)
We can write that code line also as:
2. Human amal;
amal = new Human();
Giving values to an object and calling methods
- calling means running call = run
Consider this sample code:
class Human{
Human sanija;
Human thisum;
Human jared;
sanija = new Human();
thisum = new Human();
jared = new Human();
height;
weight;
void walk() {
method of walking inside brackets
}
void drive() {
method of driving inside brackets
}
sanija.walk();
jared.drive();
thisum.height = "6 feets"
thisum.weight = "76 kg"
- You can see that the line "thisum.height = "6 feets" is connected with the variable called "height;" & you can see the line "thisum.weight = "76 kg" is connected with the variable called "weight;"


Comments
Post a Comment