In this part of my Java Video Tutorial, I continue to provide you with a complete understanding of Java.

Today I’m covering the Object and Class class, along with clone. We will explore all of the methods that every object gets by default.

Use the code that follows the video to help you learn. If you missed the previous parts make sure you check them out here Java Video Tutorial.

If you like videos like this share it

Code From the Video

LESSONSIXTEEN.JAVA

public class LessonSixteen{
public static void main(String[] args){
// Every object inherits all the methods in the Object class
Object superCar = new Vehicle();
// superCar inherits all of the Object methods, but an object
// of class Object can't access the Vehicle methods
// System.out.println(superCar.getSpeed()); * Throws an error
// You can cast from type Object to Vehicle to access those methods
System.out.println(((Vehicle)superCar).getSpeed());
// The methods of the Object class
Vehicle superTruck = new Vehicle();
// equals tells you if two objects are equal
System.out.println(superCar.equals(superTruck));
// hashCode returns a unique identifier for an object
System.out.println(superCar.hashCode());
// finalize is called by the java garbage collector when an object
// is no longer of use. If you call it there is no guarantee it will
// do anything though
// getClass returns the class of the object
System.out.println(superCar.getClass());
// THE CLASS OBJECT
// You can use the Class object method getName to get just the class name
System.out.println(superCar.getClass().getName());
// You can check if 2 objects are of the same class with getClass()
if(superCar.getClass() == superTruck.getClass()){
System.out.println("They are in the same class");
}
// getSuperclass returns the super class of the class
System.out.println(superCar.getClass().getSuperclass());
// the toString method is often overwritten for an object
System.out.println(superCar.toString());
// toString is often used to convert primitives to strings
int randNum = 100;
System.out.println(Integer.toString(randNum));
// THE CLONE METHOD
// clone copies the current values of the object and assigns
// them to another. If changes are made after the clone both
// objects aren't effected though
superTruck.setWheels(6);
Vehicle superTruck2 = (Vehicle)superTruck.clone();
System.out.println(superTruck.getWheels());
System.out.println(superTruck2.getWheels());
// They are separate objects and don't have equal hashcodes
System.out.println(superTruck.hashCode());
System.out.println(superTruck2.hashCode());
// There are subobjects defined in an object clone won't
// also clone them. You'd have to do that manually, but this
// topic will be covered in the future because of complexity
}
}

23 Responses to “Java Video Tutorial 16”

I am taking your tutorial at this moment, and have noticed that the ” Object superCar = new Vehicle(); ” and ” Vehicle superTruck = new Vehicle(); ” line within the L16 code both come up as errors. It would be very helpful to have some feed back on how to fix the error.
Thank You
Sincerely Ben T.W

hi there, I copied ur code to netbeans and I tried to run it, but no luck. do you think it is becouse I use netbeans insted of Eclipise? thanks for your advice.
this is the error I get:
Exception in thread “main” java.lang.VerifyError: (class: lessonsixteen/Vehicle, method: signature: (ID)V) Constructor must call super() or this()
at lessonsixteen.LessonSixteen.main(LessonSixteen.java:19)

Yes you need those classes and they are available here Java Video Tutorial 15. You can name your classes using different cases if you’d like, but it is common to name them per word like this ThisIsAClassName

As many of your students have already stated, and I reiterate, thank you from the bottom of my heart. What you are doing here is giving of your time and from your skill set to help others, and there is no greater gift or deed than to lend a hand to your fellow man. I am grateful for what you do and I feel a bit obligated to learn as much from you as I can, since you’re spending your time teaching us.

Now that I’ve spit-shined your sitting muscle, I have a few questions that I’d like to ask.

I love programming and I would enjoy working with a firm developing Java based software. How far off base am I to believe once I finish all 60 of your Java tutorials and I have a firm handle on the language, that I would be able to land a job using this new skill? It would be awesome, but I feel there is a lot more that I don’t know about this and I am hoping you could help me understand what employer would be looking for from me and my skill set so that I could be better prepared and enter the workforce as a competent Java programmer.

I tried to do something with this Java tutorial that has never been done. I tried to cover everything imaginable. I wanted to make a person willing to go through everything to be an expert on programming java, but also programming in general.

I’m trying to understand the syntax for the following statement:
superCar.getClass().getName()

Is such a statement does it say that there is a function (method) as part of the object (superCar) and inside the getClass() method there is another method defined as getName()?
Thanks for the explanation.