Question
The Cosmic Superclass Every Java class youve ever written isautomatically a subclass of java.lang.Object. This means thatpublic methods from Object are inherited by your classes,
The Cosmic Superclass Every Java class you’ve ever written isautomatically a subclass of java.lang.Object. This means thatpublic methods from Object are inherited by your classes, and youmay override them. Several of these methods have signficantimplications, and we will concern ourselves with 2 of them:toString() and equals(Object other) To start Write a Student classthat has two instance variables: String name and int age. Write amain method that constructs such an object, sets the instancevariables, and then prints out the object. Something like this:Student s = new Student ( " Brian " , 22); System . out . println (" Student : " + s ); What does it print out? toString() WheneverJava needs to treat an object like a String, whether that be toprint out the object, or to use it to build up text (such as in theprevious example), it automatically calls the toString method toget the appropriate value. This works because every object has thismethod! You can override the method to change the Stringrepresentation for your custom objects. 1 Turn in a class thatloads an ArrayList with Student objects and prints out the entirelist: ArrayList < Student > students = new ArrayList
Step by Step Solution
3.42 Rating (152 Votes )
There are 3 Steps involved in it
Step: 1
Studentjava public class Student instance variables private String name private int age constructor ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started