What are Classes and objects?
Copy // Class Declaration
public class Dog {
// Instance Variables
String breed;
String name;
String size;
int age;
String color;
// non static method
public void sleep () {
System . out . println (name + " is sleeping" );
}
//static method
public static void whoIsSleeping ( Dog dog){
System . out . println ( dog . name + " is sleeping" );
}
public String toString () {
return ( "Name : " + name + "\nBreed is: " + breed + "\nSize is: " + size + "\nAge is: " + age + "\nColor is: " + color + "\n" );
}
public static void main ( String [] args) {
Dog maltese = new Dog() ;
maltese . breed = "Maltese" ;
maltese . size = "Small" ;
maltese . age = 2 ;
maltese . color = "white" ;
maltese . sleep ();
System . out . println ();
System . out . println (maltese);
maltese . name = "fluffy" ;
Dog . whoIsSleeping (maltese);
}
}
Copy null is sleeping
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
fluffy is sleeping
Attempt 2 - Adding constructors
Main.java Dog.java OneFile.java
Copy public class Main {
public static void main ( String [] args) {
Dog fluffy = new Dog( "Fluffy" , "Maltese" ,
"Small" , 2 , "white" ) ;
fluffy . sleep ();
System . out . println ();
System . out . println (fluffy);
Dog snowball = new Dog(fluffy) ; //call copy constructor
System . out . println ();
snowball . name = "Snowball" ;
System . out . println ( snowball . name );
}
}
Copy // Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
String size;
int age;
String color;
//constructor - ADDED THIS
Dog ( String my_name , String breed , String size , int age , String my_color) {
this . breed = breed;
name = my_name;
this . age = age;
this . size = size;
color = my_color;
}
//copy constructor - ADDED THIS
Dog ( Dog other) {
breed = other . breed ;
name = other . name ;
age = other . age ;
size = other . size ;
color = other . color ;
}
// non static method
public void sleep () {
System . out . println (name + " is sleeping" );
}
//static method
public static void whoIsSleeping ( Dog dog){
System . out . println ( dog . name + " is sleeping" );
}
public String toString () {
return ( "Name : " + name + "\nBreed is: " + breed + "\nSize is: " + size +
"\nAge is: " + age + "\nColor is: " + color + "\n" );
}
}
Copy // Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
String size;
int age;
String color;
//constructor - ADDED THIS
Dog ( String my_name , String breed , String size , int age , String my_color) {
this . breed = breed;
name = my_name;
this . age = age;
this . size = size;
color = my_color;
}
//copy constructor - ADDED THIS
Dog ( Dog other) {
breed = other . breed ;
name = other . name ;
age = other . age ;
size = other . size ;
color = other . color ;
}
// non static method
public void sleep () {
System . out . println (name + " is sleeping" );
}
//static method
public static void whoIsSleeping ( Dog dog){
System . out . println ( dog . name + " is sleeping" );
}
public String toString () {
return ( "Name : " + name + "\nBreed is: " + breed + "\nSize is: " + size + "\nAge is: " + age + "\nColor is: " + color + "\n" );
}
public static void main ( String [] args) {
Dog fluffy = new Dog( "Fluffy" , "Maltese" ,
"Small" , 2 , "white" ) ;
fluffy . sleep ();
System . out . println ();
System . out . println (fluffy);
Dog snowball = new Dog(fluffy) ; //call copy constructor
System . out . println ();
snowball . name = "Snowball" ;
System . out . println ( snowball . name );
}
}
Copy Fluffy is sleeping
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
Snowball
Attempt 3 - Adding encapsulation
we should not be able to directly change and get properties of the object for example here
Copy public static void main( String [] args) {
Dog fluffy = new Dog( "Fluffy" , "Maltese" , "Small" , 2 , "white" ) ;
fluffy . name = "Snowball" ;
}
An array of Dogs πΆπ©π
Copy public class Main {
public static void main ( String [] args) {
String [] names = { "fluffy" , "snowball" , "shugi" , "cookie" };
Dog [] dogs = new Dog [ 4 ];
for ( int i = 0 ; i < dogs . length ; i ++ )
dogs[i] = new Dog(names[i] , "Maltese" , "Small" , 2 , "white" ) ;
for ( int i = 0 ; i < dogs . length ; i ++ )
System . out . println (dogs[i]);
}
}
Copy Name : fluffy
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
Name : snowball
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white
...