👉 Click here to
open chapters key point underline
👉 Click here 30 IMP MCQ
👉 Click here Textbook Exercise solution
Introduction
- 👉 Object-Oriented Programming (OOP) principles
- 👉 Core concepts: Encapsulation, Inheritance, Polymorphism
Class in Java
- 👉 A class is a blueprint for objects
- 👉 Syntax: `class ClassName { }`
Objects
- 👉 Creating an object from a class requires the following steps:
- Declaration: A variable (reference variable) name of type class is declared
with the syntax:
<class name> <variable name>
- Instantiation: The keyword
new
is used to create the object by allocating memory. - Initialization: A constructor (a special type of method) is called to initialize the newly created object.
- 👉 In Java, a class is a type, similar to the built-in types such as int and Boolean.
Instance Variable and Instance Method
- 👉 Instance variables and instance methods are accessed via objects.
- 👉 They can be referred by using dot (.) operator as follows:
- 👉 <object reference>.<instance variable or method>
- 👉 It should be remembered that data should be protected from such direct access from anywhere in the program. Such protection is possible with the use of access modifiers.
- 👉 When the instance variables are referred within the methods of the same class, there is no need to use dot (.) operator.
- 👉 When the programmer declares an object class, object is not created. In this case, reference variable does not refer to any object and its initial value is null by default. Use of such null reference or null pointer is illegal and may raise an exception.
- 👉 So, referring instance variable or invoking method with null reference will give an error.
- 👉 Try the following code:
Room rl; // null value assigned to reference variable by default System.out.println(rl.length); // illegal
Comments
Post a Comment