objects Flashcards
Intro
So today we are gonna learn about classes, objects and constructors.
So let’s get started.
What is a class
in Java everything is enclosed inside a class. So what is a class? it is a code that describes our particular type of object. It specifies the data that an object can hold, so the objects field and the actions that an object can perform.
think of class as a blueprint that can be used to create as many objects as you want.
When you think about blueprint, the blueprint itself is not a house.
But you know, it’s a detailed description of a house.
So when we so and we can use the blueprint to build an actual house, right? So when we say we are building an instance of a House described by the blueprint, we are making an object and using the blueprint we can build several identical houses from the same blueprint and each house is can be considered as a separate instance of the House described by the blueprint.
you can think of class as the blueprint and when you actually build a houses, you can think of that as the instance of the class which is called an object.
Components
And there are like 2 main components of a class.
They are fields and methods
So we have an object, you know that can store data like variables, so the the data store in an objects are commonly called fields or you can also call them variables and then the objects need like you use methods to perform operations and the operation that an object can perform are called operations are called method.
Here are examples of the APIs that we have already used. Scanner Class are used to make Scanner objects that is then used to read values from the keyboard. Random Class is used to make random object.
So each time you’re doing scanner input equals to new scanner.’
You are creating an instance of the class so which is like the object.
Scanner is the class and the name is the object
Access Modifiers
This word is an is called an access modifier.
So when the public access specific modifier is applied to a class member, the Member can be asked by code inside the class or outside. When it’s public, everybody can see it.
Its like instagram if you profile is public everyone can see it
If its private, only your followers can see it.
So when the when the private access modifier is applied to a class member, the member cannot be accessed via code outside the class.
so it can be only accessed by things that are in the same class.
Talk about objects
So as I mentioned before, objects are the instance of a class.So usually when we are in software, when we are talking about object and object has two general capabilities. So an object can store data and the data stored in an objects are commonly called fields, and an object can perform operations and.
The operation that an object things are forms are called methods and objects are very important in Java.
Some of the examples of objects is that uh, you know, like that we’ve already used is that when you need to read input from the keyboard or from a file you can use the scanner object when you need to generate random numbers you can use the random random object and when you need to read.
How to make an object?
And when you create an object, it you typically follow 2 steps.
One you declare a reference variable.
So in this case the reference variable is an. I named it object and you created. You created the object in memory and assign its memory address to the reference variable. you in you make an object with the help of new.
Constructors
constructors is like a method but but it does not have a return type.it is a special method that enables an object to initialize itself when it is created. It is used to initialize the data members of a class and constructors get automatically called when an object is created.
Overloaded constructors
, when, when there’s a need for initializing an object in different ways.
abstraction
Abstraction is the methodology of hiding the implementation of internal details and showing the functionality to the users.
Let’s see an example of data abstraction in Selenium Automation Framework.
In Page Object Model design pattern, we write locators (such as id, name, xpath etc.,) and the methods in a Page Class. We utilize these locators in tests but we can’t see the implementation of the methods. Literally we hide the implementations of the locators from the tests.
Using interfaces, we can achieve 100% abstraction.
interfaces
WebDriver itself is an Interface. So based on the above statement WebDriver driver = new FirefoxDriver(); we are initializing Firefox browser using Selenium WebDriver. It means we are creating a reference variable (driver) of the interface (WebDriver) and creating an Object. Here WebDriver is an Interface as mentioned earlier and FirefoxDriver is a class.
An interface in Java looks similar to a class but both the interface and class are two different concepts. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract. We can achieve 100% abstraction and multiple inheritance in Java with Interface.