Chapter – II Class and Object
Chapter – II Class and Object
o Many objects are of the same kind but have different identity.
₋ Class name
the object and the behaviors are the verbs or actions that shows action on
the object.
}
Dog class definition
Example
public class Dog {
₋ This class Dog has 3 methods and instance variables.
private String breed;
₋ Methods like:- barking(), hungry(), sleeping()
private int age
₋ States like:- breed, age, color.
private String color;
void barking() { //body …………………. }
void hungry() {//body ……………………}
void sleeping() {//body ................ }
}
Extended Class
Definition
[Accesses Modifier ] <class> <ClassName> [extends] [Superclass]
[implements ] [interface1, Inteface2] {
[Access Modifier] <Data Type> <DataMember1>;
[Access Modifier] <Data Type> <Data Member2 >;
[Access Modifier] <Return Type> <MrthodName1> ( [Para1, Para2 …]) {
//method body
}
[Access Modifier] <ClassName> ([optional Parameter lists]) {
// Constructors Body
}
o An object must be created using the class name and new keyword
o Syntax of Object creation
ClassName ObjectName = new CLassName();
o "new" is a keyword used to return the memory of the object.
Create an Object
o There are three steps when creating an object from a class
3. Initialization- the ‘new’ key word is followed by a constructor call. This call
initializes the new object.
Object Creation Example
Example Create an object
of Car
o Crate an object of car class named tFord
Car TFord = new Car();
Instantiating and using
objects
o Instance variables and methods are accessed via created objects.
o To access an instance variable the fully qualified path should be .
Accessing Instance
Members
Access Modifiers
o Access modifiers are keywords used as a prefix with class, method and
variables.
o Access modifies will change the meaning of certain statement
o That provides restriction of access /privilege
o In java the following access modifiers are supported
₋ Package private
₋ Public
₋ Private
₋ Protected
Package private
o These subclasses are much closer to a parent class than to any other "outside"
classes for the following reasons:
₋ Subclasses are usually more intimately aware of the internals of a parent class
₋ Subclasses are often written by you or by someone to whom you’ve given
your source code.
₋ Subclasses frequently need to modify or enhance the representation of the
data within a parent class.
Private
public Y Y Y Y
protected Y Y Y N
Package private Y Y N N
private Y N N N
Constructors
o Constructors are a special methods that are used to
initialize the class and instance variables automatically
at the time of object is creation
o Constructors have the same name as the class name.
o No return type is specified for the constructor
o By default constructors are public
o Constructors can be invoked only during object creation or from
other constructors using this keyword.
Defining constructor
o General Syntax
[public] <ClassName><([parameters]) >{
//body of the constructor
}
Types of Constructor
o Default constructor
₋ If you declare any constructors for a class, the Java compiler will not
create a default constructor for that class.
o The value must passed from the user when the object is created
Constructor: Example
public Car(String make, String model, int year, double mpg, String
color) {
this(); // this line will invoke the no argument constructor
}
Example
public class Student{ Invoking the
String firstName, lastName, sex;
int age; paramconstructor using
public Student() { this() method from other
this("Dawit", "Tesfaye", 27, "Male"); constructor
}
public Student(String firstName, String lastName, int age, String
sex) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.sex = sex; Invoking the no argument
} constructor when the
public static void main(String[] args) {
Student stud1 = new Student(); object is created
}
}
Method
o A method is behavior of any object
o A method is a block of code that performs a specific
task.
o Suppose you need to create a program to create a
circle and color it. You can create two methods to
solve this problem:
₋ a method to draw the circle
₋ a method to color the circle
o It uses to divide a complex problem into smaller chunks
makes your program easy to understand and reusable.
Types of Methods
o Types of Methods
o concat(), str1.concat(str2)
o Number Methods
o Character Methods
o Array Methods etc…
User Defined Method :
Syntax
ReturnType MethodName(DataType para1, DataType
para2, ...) {
//Method body
}
o ReturnType - It specifies what type of value a method returns
₋ void return type: a method that returns nothing
₋ None void return type: a method that returns a value of its
return type
o methodName - It is an identifier that is used to refer to the
particular method in a program.
Method
o calling Syntax for the None static method inside static method is
o Calling a none static method inside instance method of the same class and
o Calling a static method in any method of the same class
₋ is possible using its signature without object reference
₋ Syntax : methodName(arguments list..);
o In both the above cases, if the call is from different classes it is must to use
object reference.
₋ Syntax : objectReference.methodName(arguments list..);
Reading assignment
o Parameter Passing
o enum (Enumerator)
o Java Garbage collector
Destroying Object
reclaimed.
o Java garbage collection is an automatic process.
o The programmer does not need to explicitly mark objects to be
deleted.
Enumerated Types
o Instance Fields
o Encapsulation
End of Chapter –II Class and
Object
Class and object
Define a class Compiled By: Daniel Tesfay
Hawassa University
Instantiating an object
CHAPTER III