Java Video Tutorial 16

Java Video Tutorial 16In 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


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
		// The methods of the Object class
		Vehicle superTruck = new Vehicle();
		// equals tells you if two objects are equal
		// hashCode returns a unique identifier for an object
		// 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
		// You can use the Class object method getName to get just the class name
		// 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 
		// the toString method is often overwritten for an object
		// toString is often used to convert primitives to strings
		int randNum = 100;
		// 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
		Vehicle superTruck2 = (Vehicle)superTruck.clone();
		// They are separate objects and don't have equal hashcodes
		// 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


public class Vehicle extends Crashable implements Drivable, Cloneable{
	int numOfWheels = 2;
	double theSpeed = 0;
	int carStrength = 0;
	public int getWheels(){
		return this.numOfWheels;
	public void setWheels(int numWheels){
		this.numOfWheels = numWheels;
	public double getSpeed(){
		return this.theSpeed;
	public void setSpeed(double speed){
		this.theSpeed = speed;
	public Vehicle(){
	public Vehicle(int wheels, double speed){
		this.numOfWheels = wheels;
		this.theSpeed = speed;
	public void setCarStrength(int carStrength){
		this.carStrength = carStrength;
	public int getCarStrength(){
		return this.carStrength;
	public String toString(){
		return "Num of Wheels: " + this.numOfWheels;
	public Object clone(){
		Vehicle car;
			car = (Vehicle) super.clone();
		catch(CloneNotSupportedException e){
			return null;
		return car;

23 Responses to “Java Video Tutorial 16”

  1. Ben T.W says:

    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

    • admin says:

      There has to be something else wrong because that code is fine. What error are you getting?

    • Eddie says:

      This happened to me too, but it was because in the code for lesson 15 the Vehicle class was missing a constructor. This is added in the code above, so it all works now. Phew!

      Amazing tutorials, thank you Derek!

      • Eddie says:

        Oops, disregard this comment! I don’t think I quite understand Java yet… ha. Was it because Line 07 in JavaLesson16 is missing the int and double arg?

      • Amuramesh says:

        thanx eddie. .me too got the same error,and created a costructor in Vehicle class. .it was gone. . Phew!!!!! 😉

    • Anonymous says:

      The constructor in the VEHICLE class defines two parameters:

      public Vehicle(int wheels, double speed) {
      this.numOfWheels = wheels;
      this.theSpeed = speed;

      Adding a blank constructor can fix this:

      public Vehicle(){


      In the JAVALESSONSIXTEEN class these parameters are undefined.

      Object superCar = new Vehicle();

      It should be:

      Object superCar = new Vehicle (4, 10);

      [or whatever int and double values you would choose in place of the 4 and the 10]

  2. Nicky says:

    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(

    • admin says:

      This seems to be a NetBeans error. This is the fix I found online

      Delete .class files from under project/build/classes.

      Click Run -> Clean & Build Project

      I hope that helps

  3. Nicky says:

    Hello thnaks , do we need to copy the crashable and drivable classes to ur code for this lesson? as they are not included in ur code for this lesson.

    another question I have: does it matter to name the clases with uppercase or lowercase? are Crashable different from CRASHABLE? THNAKS

  4. kapil says:

    would you plz. make video tutorial series on J2EE(servelet,jsp)??

  5. Will Mosley says:

    Hi Derek!

    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.

    William Mosley

    • Derek Banas says:

      Hi William,

      Thank you for the kind message. It is very much appreciated 🙂

      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.

      The java tutorial actually continues after the original 60 to Design Patterns, Java Algorithms, Object Oriented Design, Refactoring, UML, and now Android.

      After Android apps, I’ll cover Android game development. Then I’ll cover the final part being all J2EE related topics. I’ll also probably develop straight Java Games.

      If you go through all of them I’d say you will be a Java expert and an expert in programming in general. I hope that helps 🙂


  6. Bryan says:

    Hello Derek,

    Thank you very much for uploading this tutorial.

    I have one question about the code. In LESSONSIXTEEN.JAVA, line 53, System.out.println(superCar.toString()); why don’t you use superCar.getClasss().toString()?

  7. saquib says:

    From where to download eclipse for programming of java? and which version.

  8. jp says:

    Hi Derek,

    I’m trying to understand the syntax for the following statement:

    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.


  9. Edward says:

    excelent tuto, gracias Derek

Leave a Reply to Edward Cancel reply

Your email address will not be published.