1 / 7

Java Objects And Classes

https://firstcode.school/java-objects-and-classes/

Sudhanshi
Download Presentation

Java Objects And Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Objects And Classes In this tutorial, we will learn about Java objects and Classes .Objects are the basic building blocks of any object-oriented language program. Hence, the entire program of Java revolves around classes and objects. In this tutorial, let’s try to understand what classes and objects are and how they are implemented in Java. What is a Class in Java? A class is a blueprint of an object. Classes mainly consist of data members (variables) and member functions (methods). A class is considered a user-defined data type. By default, if no access specifier is mentioned, then members of the class have a Default access modifier. Let’s take a real-life example of a bus. Every bus performs the same basic functions, such as blowing the horn, speeding up and applying brakes. However, depending on the manufacturer and the bus model, different buses will have different colours, mileages, etc. However, the basic blueprint of the bus remains the same. Let’s compare the above example to object-oriented programming. First, a basic bus becomes the class, and specific models of buses, such as School buses, Double-decker buses and Single-decker buses, become objects of the bus class. All Java programs have at least one class enclosing the main() method. Components of Class in Java A Java class encapsulates many things: 1. Methods: A function declared inside a class is called a method.

  2. 2. Variables: Variables of a class are also known as data members. 3. Constructors: A constructor is used to initialise an object. 4. Blocks: Blocks are used to initialise data members. 5. Nested Classes: A class inside a class is called a nested class. Variable Types in Java Class Classes have three types of variables- local, instance and static. 1. Local variable: A variable declared inside a method is called a local variable. 2. Instance variable: A variable declared inside a class but outside a method is called an instance variable. Every object has its own copy of an instance variable. 3. Static variable: Static variables, also known as class variables, are initialised only once. Only one copy of a static variable is created, which all class instances share. Syntax of Class Declaration: [modifier] class ClassName{ //variable declarations //method declarations } If our class is implementing an interface or extending another class, the syntax would be as follows modifier class ClassName extends SuperClassName implements InterfaceName{

  3. //variable declarations //method declarations } Example of Class in Java: Let’s take the previous example of a bus. A bus will have variables such as the number of gears and speed and functions such as blowing the horn and applying breaks. So the class would be as follows: Class Bus{ //variable declaration int numberOfGears; double speed; //method declaration void applyBrakes(){ System.out.println(“Applying Brakes….”); } void blowHorn(){ System.out.println(“Blowing the Horn!”); } } Importance of Class in Java

  4. You may be thinking- but why do we need classes in Java? First, a class allows the reusability of code. As we saw earlier, the exact blueprint of buses is used to make different types of buses. It would be tiring and redundant to create the same basic design again and again. Similarly, in programming, the programmer can create different variations of objects with similar basic characteristics and functions without repeatedly writing the same methods and variables. Classes also allow code reusability through inheritance and abstraction, which we will see in further tutorials. Moreover, Classes facilitate Encapsulation – or data hiding, which is a significant pillar of object-oriented programming. It wraps up methods and variables into one unit. With the help of access modifiers, classes can also prevent unrestricted access to their members. We will learn about encapsulation and access modifiers in other tutorials. Object in java An object is an instance of a class and can be considered a variable of the user-defined data-type Class. Each object has three main properties: its id, state and behaviour. Let’s go back to the previous example of a bus. A bus has certain attributes such as mileage, engine capacity, speed, number of gears, number of wheels, etc. The state of the bus is the current value of attributes at that time. A bus also performs basic functions such as starting the engine, applying brakes, accelerating and blowing horns. These functions are the behaviours of the bus. Behaviours of the bus can change the state of the bus by altering the values of certain attributes.

  5. Similarly, each object has its own state and behaviours. It also has its own unique object ID, which JVM uses to identify an object. Syntax of object creation in Java An object in Java can be created using the new keyword, as follows. ClassName objectName = new ClassName(Constructor arguments); Example of Object in Java Let’s go back to the previous example of the class Bus. Suppose manufacturer FirstCode is making two bus models. The first bus has four gears and a speed of 60 km/hr, and the second has five gears and a speed of 72 km/hr. Now we will learn how to create these two objects of the Bus class, how to initialise their values and display the data. Class Bus{ //variable declaration int numberOfGears; double speed; //method declaration void applyBrakes(){ System.out.println(“Applying Brakes….”); } void blowHorn(){ System.out.println(“Blowing the Horn!”);

  6. } public static void main(String args[]){ //Creating objects of Bus class Bus busA = new Bus(); Bus busB = new Bus(); //Initialising the objects busA.numberOfGears = 4; busA.speed = 60.00; busB.numberOfGears = 5; busB.speed = 72.00; //Displaying values of objects System.out.println(“Bus A has “ + busA.numberOfGears + “ gears and a speed of “ + busA.speed + “ km/hr”); System.out.println(“Bus B has “ + busB.numberOfGears + “ gears and a speed of “ + busB.speed + “ km/hr”); } } Output: Bus A has a 4 gears and a speed of 60.00 km/hr; Bus B has a 5 gears and a speed of 72.00 km/hr; Importance of Objects in Java Objects are used to instantiate a class. Therefore, classes do not occupy memory till an object of the class is created.

  7. Objects also provide specific implementation of a class. For example, a single blueprint of a bus can be implemented in different forms (School Bus, Double-decker Bus) by providing different values to the variables. Summary This was all about objects and classes in java. Hope you liked it.

More Related