Code.org Objects Hacks
In java, there are things known as objects. These objects can be defined with the use of classes.
public class Krish{
}
Here we have created the class "Krish". We can give this object characteristics, as shown below.
public class Krish {
String height;
int age;
public Krish(){
this.height = "68 inches";
this.age = 15;
}
public void printInformation(){
System.out.println("Krish is " + height + " tall.");
System.out.println("He is also " + age + " years old.");
}
}
public class Sister extends Krish{
String name;
int age;
public Sister(){
this.name = "Ishna";
this.age = 7;
}
public void printInformation(){
System.out.println(name + " is Krish's sister.");
System.out.println("She is " + age + " years old.");
}
}
public class Main{
public static void main(String []args) {
Krish krish = new Krish();
Sister sister = new Sister();
krish.printInformation();
sister.printInformation();
}
}
Main.main(null);
Here we have given the object Krish some characteristics. We can use this object by doing .height or .age and getting the height and age of the object.
Here we can also see that the sister class is extending the Krish class, as Krish's sister extends himself. This is basically a extension of Krish and all of the variables from Krish.
public static String rearrange(String str)
{
String temp = "";
for (int i = str.length() - 1; i > 0; i--)
{
temp += str.substring(i - 1, i);
}
return temp;
}
System.out.println(rearrange("apple"));