Looking for some quick and useful notes on this: • Using Classes and Objects o C
ID: 3836791 • Letter: L
Question
Looking for some quick and useful notes on this:
• Using Classes and Objects
o Creating Objects: understand how to create objects, assign them, invoke methods on them
and re-assign references.
o The String Class: understanding and using strings and String methods
o Packages: use import statement when needed
o Wrapper Classes: use Wrapper classes to parse strings.
• Writing classes
o Anatomy of a Class: understand the concept of a class, define its instance variables,
constructor and methods.
o Anatomy of a Method: understand class methods, be able to write methods.
o Encapsulation: understand visibility modifiers, write accessors and mutators, write the
toString method.
Explanation / Answer
how to create objects, assign them, invoke methods on them and re-assign references.
We can create the objects by using “new” operator.
Classname reference ; //here we are creating the class reference
reference=new Classname(); //Creating a class object and assigning to the class reference.
Then we can call the methods of that class by using the reference.methodname();
How to re-assign the references?
Employee emp1=new Employee();
In the above step we are creating an Object to the Employee class pointing by the reference emp1.
Employee emp2;
Now we are creating another reference to the Same Employee class.
Now we can re-assign the reference of Employee object pointed by emp1 to emp2;
emp2=emp1;
Now emp2 is Pointing to the Employee object.
The String Class: understanding and using strings and String methods
String: A String represents a group of characters .
Ex: beautiful
Any string is an object of String class to java.
In java a String is String object its not character array.
There are Three ways of creating Strings.
1)We ac declare a string variable and directly assign group of characters to the string variable.
Ex:String str= ”Hello”;
Here Jvm Create object internally .Object means memory .that memoryname is str.
2)We can create String class object and store a string into that Object.
String s1=new String(“Hello”);
3) We can create a String object by converting a character array into string object.
Char arr[]={‘h’,’e’,’l’,’l’,’o’};
String s2= new String(arr);
String methods:
Some of the important String class methods are :
It concatenates the calling string with str.
Ex:
String s1=”beauiti”;
String s2=”ful”;
s1.concat(s2); //here we are joining string s2 at the end of s1.
So the if we print s1 as System.out.println(s1); it will display “beautiful”
int Length():
This method returns the length of the String.
String s1=”beautiful”;
Int n=s1.length();
Now the integer variable ‘n’ contains 9.
Packages: use import statement when needed
A package represents a subdirectory that contains a group of related classes and interfaces,enumerations and annotations.
Suppose if we want to use all the classes inside the package ,we have to use ‘*’
Import java.io.*;
Hare java.io is the package name inside that package if we want to use all the classes we have to use ‘*’
Advantages of packages:
1)A package protects classes and interfaces by hiding them.
2)Packages provide encapsulation ,that means same names can be used for the classes of two different packages.
Wrapper Classes: use Wrapper classes to parse strings
In java all wrapper classes have method to parse string into corresponding datatype ,but the thing is the string should be parse-able.
Suppose String str1=”25”;
We want to convert the String str1 to integer .
Int a=Integer.parseInt(str1);
Now the variable ‘a’ contains the value 25 which is of Integer type.
Like this we have Double.parseDouble(); etc.
o Anatomy of a Class: understand the concept of a class, define its instance variables,
constructor and methods.
Class: A class is group name to which a group of objects belong. (OR) a class is model or plan for creating objects.
A class contains properties and actions which means a class contains variables and methods.
Class doesn’t exist physically but object exists physically.
Instance variables: When we create an object a copy of variables is created in that object so it is called instance variables.
But instance variables are not available to static methods because static methods are executed prior to the creation of object.
Constructor:
It is similar to the method that is used to initialize the instance variables.The main purpose of constructor is to initialize the instance variables.
We have to follow some rules while writing a constructor.
Methods: Which contains a group of statements to perform some task or activity.
Encapsulation: understand visibility modifiers, write accessors and mutators, write the
toString method.
Encapsulation: holding the variables and the methods which acts on those variables as a single unit is called Encapsulation.
The best example for this is a Class.
Which contains instance variables ,methods which acts on those variables.
Its always Recommended to write the instance variables as private.As the scope of the private access specifier is limited to that class in which they have been declared.we can’t access them from outside the class.So we have to accessor and mutator methods.
Accessor methods area used to just to get the values of the instance variables.They will not modify the contents of the instance variables.
If we want to modify the contents of the instance variables we have to use mutator methods.
There are some access modifiers in java
Private,public,protected,default.
When a method or variable is declared as public ,it means all other classes regardless of package they belong to can access themember.
Private members :Members are marked as private cant be accessed by code in any classotehr than the class in which the private members was declared.
Protected and default members:
The protected and default members are almost identical ,but with one critical difference .
A default member may be accessed only if the class accessing the members belongs to the same package ,where as a protected member can be accessed by a sub class even if the sub class is in a different package.
toString method is used to display the contents of an object inside it.if we provided the toString()method then it checks whether the toString() is available in its super class.If is is available it will be executed,there also if is was not there then ultimate super class(Obejct) toString() will be called and executed.
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.