The document provides an overview of classes and objects in Java, explaining that a class is a blueprint for creating objects, which are instances of that class. It details the characteristics of objects, the purpose of constructors, and the differences between various types of constructors, including default and parameterized constructors. Additionally, it discusses keywords such as 'this' and 'static', and introduces the concept of constructor overloading.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views55 pages
OOP_CH2
The document provides an overview of classes and objects in Java, explaining that a class is a blueprint for creating objects, which are instances of that class. It details the characteristics of objects, the purpose of constructors, and the differences between various types of constructors, including default and parameterized constructors. Additionally, it discusses keywords such as 'this' and 'static', and introduces the concept of constructor overloading.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55
Chapter-1I
Classes and objects in java
Prepared by: Marta G. (MSc.)
What is class?
Class is an OOP construct that consists of data members and
member functions. ◦ Data members are variables that we declare inside the class ◦ Member functions are the function or method which we declare inside the class Class is a new user defined/programmer defined data type. Once defined, this new data type can be used to create objects of that type. Cont… Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably. ◦ The data, or variables, defined within a class are called instance variables. A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind A class is a group of objects which have common properties. Before we create an object, we first need to define the class. ◦ It is a logical entity ◦ It can’t be physical Cont… We can create a class in Java using the class keyword. Class is a group of variables of different data types and group of methods. ◦Syntax: Cont…
Fields (variables) and methods represent the state and
behavior of the object respectively. ◦ Fields are used to store data ◦ Methods are used to perform some operations Cont..
The data, or variables, defined within a class are called
instance variables. The methods and variables defined within a class are called members of the class. What is object?
An object is an identifiable entity with some characteristics,
state and behavior. An object is called an instance of a class. It is a basic unit of Object-Oriented Programming and represents real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. Cont…
An object has three characteristics:
1 State: represents the data (value) of an object. What does it look like? 2 Behavior: represents the behavior (functionality) of an object What does it do? 3 Identity: An object identity is typically implemented using a unique ID. What do we call it? To create: Cont… The new operator dynamically allocates (that is, allocates at run time) memory for an object. For Example: Box mybox = new Box(); This statement combines the two steps 1. The first part declares mybox as a reference to an object of type Box. mybox contains the value null 2. The next line allocates an actual object and assigns a reference to it to mybox. Example Cont… We can use the name of objects along with the dot (.) operator to access members of a class. The dot operator links the name of the object with the name of an instance variable. Difference between java class and objects Example I An object is any entity that has a state and behavior. For example, a bicycle is an object. It has • States: idle, first gear, etc • Behaviors: braking, accelerating, etc. Cont.. An object is called an instance of a class. ◦ For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle, TouringBicycle, etc can be considered as objects of the class. Cont… We have used the new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. ◦ For example, Bicycle() is the constructor of the Bicycle class. Here, sportsBicycle and touringBicycle are the names of objects. We can use them to access fields and methods of the class. Cont..
To access members of a class (. Operator ) is used
Example II Example III Example IV Adding a method that takes parameters
Some methods don’t need parameters, most do.
Parameters allow a method to be generalized Cont… Constructors
A constructor in Java is a special method that is used to
initialize objects. The constructor is called when an object of a class is created. Constructor is a block of code that initializes the newly created object. It is called when an instance of the class is created. ◦ At the time of calling constructor, memory for the object is allocated in the memory. Cont…
Every time an object is created using the new() keyword, at
least one constructor is called. It is called constructor because it constructs the values at the time of object creation. Following is the syntax of a constructor Rules for creating java constructor
1. A constructor must have the same name as the class
itself. 2. Constructors do not have a return type and not even void. This is because they return the instance of the class itself. A constructor is automatically called when an object is created. Types of Java constructors
If you do not implement any constructor in your class, Java compiler inserts a default constructor into your code A default constructor is invisible constructor. if we write a constructor with arguments or no arguments then the compiler does not create a default constructor. Cont… 1. What will be the output of the following code?
2. What is the purpose of a default constructor?
Answer
1. Error because the variable are not initialized
2. The purpose of the default constructor is to initialize the attributes of the object with their default values. Example of default constructor No-Args constructors
If a constructor does not accept any parameters, it is
known as a no-argument constructor. It is constructor without any argument Example Example II Parameterized constructors A constructor which has a specific number of parameters is called a parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor. Why we use parameterized constructor? Answer is: ◦The parameterized constructor is used to provide different values to distinct objects. ◦However, you can provide the same values also. Example Example II Example III Difference between constructor and method Few regularly used keywords: new, this, static, super, final The super and final keywords will demonstrate in further chapter. New The new keyword dynamically allocates memory for an object. Syntax: class_name object _name = new class_name(); Box b1 = new Box(); Box b2 = new Box(); this Usage of this keyword this can be used to refer current class instance variable. this can be used to invoke current class method (implicitly) this() can be used to invoke current class constructor. this can be passed as an argument in the method call. this can be passed as argument in the constructor call. Cont.. The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Solution for the above problem is this keyword Static keyword A class member must be accessed with the use of an object of its class but sometimes we want to define a class member that will be used independently without creating any object of that class. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. One can declare both methods and variables to be static. Instance variables declared as static are actually, global variables. Cont…
The static keyword in Java is mainly used for memory
management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class. It is applicable in: 1.Blocks 2.Variables 3.Methods 4.Classes Static blocks If you need to do the computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded. Example: Static variable When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. Important points for static variables: • We can create static variables at the class level only. • Static block and static variables are executed in the order they are present in a program. Example Constructor overloading
Constructor overloading is a concept of having more than
one constructor with different parameters list ◦ In such a way so that each constructor performs a different task. Two or more constructors with the same name but with different signatures is called constructor overloading. If two constructors of a class have the same signature, it represents ambiguity. ◦ In this case, Java compiler will generate an error message because Java compiler will unable to differentiate which form to execute. Cont… Java compiler decides which constructor has to be called depending on the number of arguments passing with objects. Constructor overloading example What is method overriding? What is its difference with method overloading? Part II