A class contains both data (referred to as attributes), and program code
Creating objects
Creating an object from a class requires the following steps :
Declaration : A variable (reference variable) name of type class is declared with syntax <class name> <variable name>
• Instantiation : Keyword new is used to create the object by allocating memory
• Initialization: Constructor (a special type of method) is called to initialize the newly created
Using new keyword, we can create an object. Operator new allocates the memory for an object and returns the address
When an object is created, in addition to allocating memory, a special method called 'constructor is executed to perform initial task.
rl = new Room(); Here, parentheses are important;
In object-oriented programming (OOP) languages, creating an object is also called object instantiation. An instance for an object is created by allocating memory to store the data for that object.
Instance variables define the attributes of an object. The class defines the kind of attribute. Each instance stores its own value for that attribute.
The new keyword returns a reference to an object that represents an instance of the class. All instances of class are allocated memory in data structure called heap.
Each object instance has its own set of data. Accessing instance variables and calling instance methods Instance variables and instance methods are accessed via objects. there is no need to use dot (.) operator.
Class variables and class methods :-Just like class variable, class method can be defined using static keyword in front of the method definition.
• Class variables and class methods can be accessed using a class name or reference variable.
To increase readability, it is advised to access with class name.
Class variables and class methods can be accessed from instance methods also.
Instance variables and instance methods can't be accessed from class methods, as class
Local variables : Variables defined inside methods or blocks are called local variables. Formal parameters of the methods are also local variables
Instance variables : Instance variables are variables defined within a class but outside any method.
Class variables : Class variables are variables defined with in a class, outside any method, with the static keyword. These variables are allocated memory only once per class and is shared
Constructors Constructor is a special kind of method that is invoked when a new object is created. Constructor can perform any action, but it is mainly designed to perform initializing actions.
Types of specifier
1. Public
2. Private
3. Protected
4. Default
The values of actual parameters are copied to formal parameters and then function is executed. Changes made to formal parameters are not affecting actual parameters.
Inheritance
Object-oriented programming languages provide reusability feature using inheritance. Inheritance allows us to build new class with added capabilities by extending existing class.
Parent class is also referred to as superclass or base class. In the same way; child class is also referred to as subclass, derived class or extended class.
When an object of subclass is instantiated, memory is allocated for all its attributes including inherited ones
Choose the correct Option
(1) Which of the following defines attributes and methods ?
(a) Class (b) Object (c) Instance (d) Variable
(2) Which of the following keyword is used to declare Class variables and class methods?
(a) static (b) private (c) public (d) package
(3) Which of the following operator creates an object and returns its reference ?
(a) dot() (b) new (c) colon (:) (d) assignment (=)
(4) Which of the following method can be called without creating an instance of a class ?
(a) Instance method (b) Class method (c) Constructor method (d) All of the above
(5) Which of the following refers more than one method having same name but different
parameters ?
(a) Overloaded methods (b) Overridden methods (c) Duplicate methods (d) All of the above
(6) Which method is invoked automatically with creation of an object ?
(a) Instance method (b) Constructor (c) Class method (d) All of the above
(7) Which of the following is the keyword used to refer a superclass constructor in subclass constructor ?
(a) extends (b) super (c) name of the superclass (d) new
(8) Which of the following is used to invoke an instance method in Java ?
(a) The name of the object, colon(t) and the name of the method
(b) The name of the object, dot(.) and the name of the method
(c) The name of the class, colon(:) and the name of the method
(d) The name of the class, dot(.) and the name of the method
(9) Which of the following is accessible by instance methods ?
(a) Only instance variables (b) Only class variables (c) Both instance variables and class variables (d) All of the above
(10) When methods in the superclass and subclass have same name and signature, what are they called ?
(a) Overloaded methods (b) Overridden methods (c) Inherited methods (d) All of the above