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 } }
VEHICLE.JAVA
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; try{ car = (Vehicle) super.clone(); } catch(CloneNotSupportedException e){ return null; } return car; } }
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
There has to be something else wrong because that code is fine. What error are you getting?
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!
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?
thanx eddie. .me too got the same error,and created a costructor in Vehicle class. .it was gone. . Phew!!!!! 😉
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(){
}
OR:
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]
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)
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
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
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
Thanks for ur reply. 🙂
would you plz. make video tutorial series on J2EE(servelet,jsp)??
That series of tutorials is in the works now
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.
Sincerely,
William Mosley
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 🙂
Derek
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()?
You’re very welcome 🙂 I just used the code that I did to explain how toString works.
From where to download eclipse for programming of java? and which version.
I made a tutorial for that called Install Eclipse for Java. I hope it helps 🙂
Hi Derek,
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.
jp
Hi
superCar.getClass().getName() is getting the name of the class for the object superCar
excelent tuto, gracias Derek
Thank you 🙂