Page 1 :
Java Classes and Objects, Java is an Object-Oriented Language. As a language that has the Object-Oriented, feature, Java supports the following fundamental concepts −, •, •, •, •, •, •, •, •, •, , Polymorphism, Inheritance, Encapsulation, Abstraction, Classes, Objects, Instance, Method, Message Passing, , In this chapter, we will look into the concepts - Classes and Objects., •, , Object − Objects have states and behaviors. Example: A dog has states color, name, breed as well as behaviors – wagging the tail, barking, eating., An object is an instance of a class., , •, , Class − A class can be defined as a template/blueprint that describes the, behavior/state that the object of its type support., , Classes in Java, A class is a blueprint from which individual objects are created., Following is a sample of a class., , Example, public class Dog {, String breed;, int age;, String color;, void barking() {, }, void hungry() {, }, void sleeping() {, }, }
Page 2 :
Creating an Object, As mentioned previously, a class provides the blueprints for objects. So basically,, an object is created from a class. In Java, the new keyword is used to create new, objects., There are three steps when creating an object from a class −, •, , Declaration − A variable declaration with a variable name with an object, type., , •, , Instantiation − The 'new' keyword is used to create the object., , •, , Initialization − The 'new' keyword is followed by a call to a constructor. This, call initializes the new object., , What is an object in Java, , An entity that has state and behavior is known as an object e.g., chair, bike, marker,, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example, of an intangible object is the banking system., An object has three characteristics:
Page 3 :
o, , State: represents the data (value) of an object., , o, , Behavior: represents the behavior (functionality) of an object such as deposit,, withdraw, etc., , o, , Identity: An object identity is typically implemented via a unique ID. The value of the, ID is not visible to the external user. However, it is used internally by the JVM to, identify each object uniquely., , For Example, Pen is an object. Its name is Reynolds; color is white, known as its state., It is used to write, so writing is its behavior., An object is an instance of a class. A class is a template or blueprint from which, objects are created. So, an object is the instance(result) of a class., Object Definitions:, o, , An object is a real-world entity., , o, , An object is a runtime entity.
Page 4 :
o, , The object is an entity which has state and behavior., , o, , The object is an instance of a class., , What is a class in Java, A class is a group of objects which have common properties. It is a template or, blueprint from which objects are created. It is a logical entity. It can't be physical., A class in Java can contain:, o, , Fields, , o, , Methods, , o, , Constructors, , o, , Blocks, , o, , Nested class and interface
Page 5 :
Syntax to declare a class:, 1. class <class_name>{, 2., , field;, , 3., , method;, , 4. }, , Instance variable in Java, A variable which is created inside the class but outside the method is known as an, instance variable. Instance variable doesn't get memory at compile time. It gets, memory at runtime when an object or instance is created. That is why it is known as, an instance variable.
Page 6 :
Method in Java, In Java, a method is like a function which is used to expose the behavior of an object., , Advantage of Method, o, , Code Reusability, , o, , Code Optimization, , new keyword in Java, The new keyword is used to allocate memory at runtime. All objects get memory in, Heap memory area., , Object and Class Example: main within the class, In this example, we have created a Student class which has two data members id and, name. We are creating the object of the Student class by new keyword and printing, the object's value., Here, we are creating a main() method inside the class., File: Student.java, , //Java Program to illustrate how to define a class and fields, //Defining a Student class., , class Student{, //defining fields, , int id;//field or data member or instance variable, String name;, , //creating main method inside the Student class, public static void main(String args[]){, , //Creating an object or instance, Student s1=new Student();//creating an object of Student, , //Printing values of the object, System.out.println(s1.id);//accessing member through reference variable, , System.out.println(s1.name);, }
Page 7 :
}, Test it Now, , Output:, 0, null, , Object and Class Example: main outside the class, In real time development, we create classes and use it from another class. It is a, better approach than previous one. Let's see a simple example, where we are having, main() method in another class., We can have multiple classes in different Java files or single Java file. If you define, multiple classes in a single Java source file, it is a good idea to save the file name, with the class name which has main() method., File: TestStudent1.java, , 1. //Java Program to demonstrate having the main method in, 2. //another class, , 3. //Creating Student class., 4. class Student{, , 5., , int id;, , 6., , String name;, , 7. }, 8. //Creating another class TestStudent1 which contains the main method, , 9. class TestStudent1{, 10. public static void main(String args[]){, , 11. Student s1=new Student();, 12. System.out.println(s1.id);, , 13. System.out.println(s1.name);, 14. }, , 15. }, Test it Now, , Output:, 0, null
Page 8 :
Access Modifiers in Java, 1. Private access modifier, , 2. Role of private constructor, , 3. Default access modifier, , 4. Protected access modifier, , 5. Public access modifier, , 6. Access Modifier with Method Overriding, , There are two types of modifiers in Java: access modifiers and non-access, modifiers., The access modifiers in Java specifies the accessibility or scope of a field, method,, constructor, or class. We can change the access level of fields, constructors, methods,, and class by applying the access modifier on it., There are four types of Java access modifiers:, 1. Private: The access level of a private modifier is only within the class. It cannot be, accessed from outside the class., 2. Default: The access level of a default modifier is only within the package. It cannot be, accessed from outside the package. If you do not specify any access level, it will be, the default., 3. Protected: The access level of a protected modifier is within the package and outside, the package through child class. If you do not make the child class, it cannot be, accessed from outside the package.
Page 9 :
4. Public: The access level of a public modifier is everywhere. It can be accessed from, within the class, outside the class, within the package and outside the package., , There are many non-access modifiers, such as static, abstract, synchronized, native,, volatile, transient, etc. Here, we are going to learn the access modifiers only., 26.3M, 623, , OOPs Concepts in Java, , Understanding Java Access Modifiers, Let's understand the access modifiers in Java by a simple table., , Access, Modifier, , within, class, , within, package, , outside package by, subclass only, , outside, package, , Private, , Y, , N, , N, , N, , Default, , Y, , Y, , N, , N, , Protected, , Y, , Y, , Y, , N, , Public, , Y, , Y, , Y, , Y, , 1) Private, The private access modifier is accessible only within the class., Simple example of private access modifier, In this example, we have created two classes A and Simple. A class contains private, data member and private method. We are accessing these private members from, outside the class, so there is a compile-time error.
Page 10 :
1. class A{, 2. private int data=40;, , 3. private void msg(){System.out.println("Hello java");}, 4. }, , 5., 6. public class Simple{, , 7., , public static void main(String args[]){, , 8., , A obj=new A();, , 9., , System.out.println(obj.data);//Compile Time Error, , 10., , obj.msg();//Compile Time Error, , 11., , }, , 12. }, , Role of Private Constructor, If you make any class constructor private, you cannot create the instance of that class, from outside the class. For example:, , 1. class A{, 2. private A(){}//private constructor, , 3. void msg(){System.out.println("Hello java");}, 4. }, , 5. public class Simple{, 6., , public static void main(String args[]){, , 7., 8., , A obj=new A();//Compile Time Error, }, , 9. }, Note: A class cannot be private or protected except nested class.
Page 11 :
2) Default, If you don't use any modifier, it is treated as default by default. The default modifier, is accessible only within package. It cannot be accessed from outside the package. It, provides more accessibility than private. But, it is more restrictive than protected, and, public., Example of default access modifier, In this example, we have created two packages pack and mypack. We are accessing, the A class from outside its package, since A class is not public, so it cannot be, accessed from outside the package., , 1. //save by A.java, 2. package pack;, , 3. class A{, 4., , void msg(){System.out.println("Hello");}, , 5. }, , 1. //save by B.java, 2. package mypack;, , 3. import pack.*;, 4. class B{, , 5., , public static void main(String args[]){, , 6., , A obj = new A();//Compile Time Error, , 7., , obj.msg();//Compile Time Error, , 8., , 9. }, , }
Page 12 :
In the above example, the scope of class A and its method msg() is default so it, cannot be accessed from outside the package., , 3) Protected, The protected access modifier is accessible within package and outside the, package but through inheritance only., The protected access modifier can be applied on the data member, method and, constructor. It can't be applied on the class., It provides more accessibility than the default modifer., Example of protected access modifier, In this example, we have created the two packages pack and mypack. The A class of, pack package is public, so can be accessed from outside the package. But msg, method of this package is declared as protected, so it can be accessed from outside, the class only through inheritance., , 1. //save by A.java, 2. package pack;, , 3. public class A{, 4. protected void msg(){System.out.println("Hello");}, , 5. }, , 1. //save by B.java, 2. package mypack;, , 3. import pack.*;, 4.
Page 13 :
5. class B extends A{, 6., , public static void main(String args[]){, , 7., , B obj = new B();, , 8., , obj.msg();, , 9., , }, , 10. }, Output:Hello, , 4) Public, The public access modifier is accessible everywhere. It has the widest scope among, all other modifiers., Example of public access modifier, , 1. //save by A.java, 2., , 3. package pack;, 4. public class A{, , 5. public void msg(){System.out.println("Hello");}, 6. }, , 1. //save by B.java, 2., , 3. package mypack;, 4. import pack.*;, , 5., 6. class B{, , 7., , public static void main(String args[]){
Page 14 :
8., , A obj = new A();, , 9., , obj.msg();, , 10. }, , 11. }, , Output:Hello, , Java Access Modifiers with Method Overriding, If you are overriding any method, overridden method (i.e. declared in subclass) must, not be more restrictive., , 1. class A{, 2. protected void msg(){System.out.println("Hello java");}, , 3. }, 4., , 5. public class Simple extends A{, 6. void msg(){System.out.println("Hello java");}//C.T.Error, , 7., , public static void main(String args[]){, , 8., , Simple obj=new Simple();, , 9., , obj.msg();, , 10., , }, , 11. }, The default modifier is more restrictive than protected. That is why, there is a, compile-time error.