You are on page 1of 7

Ways to declare and initialize …

(declare + assign values = initialize)

Array →

Boolean method
Class
Attributes/ properties/ variable/ field → variable declared in a class
Constructor →
- a special method that is used to initialize objects
- The constructor is called when an object of a class is created.
- Can be used to set initial values for object attributes → these values can be changed after creating an object
- Take parameters, which is used to initialize attributes.
Modifier →
- Public → access modifier → can be accessed from other classes
- Private → access modifier → cannot be accessed from other classes, can only be accessed within the same
class
Get and set method →
- Get and set method are used to access private variable from other classes
How to use this. Keyword
How to use toString();

Access the private attributes within the same class


1 – private attributes

2 – private attributes + constructor


Access the private attributes from another class

1 – private attributes + getter and setter

2 – private attributes + setter and getter + print() method


Inheritance
In java,
Inherit attributes and methods from one class to another.
We can group the “inheritance concept” into two categories:
- Superclass (parent)
- Subclass (child)
Use “extend” keyword to inherit. → child extends parent → subclass extends superclass
“protected” modifier → can be accessed within the same class and from subclasses

Polymorphism
Polymorphism means “many forms”.
It occurs when we have many classes that are related to each other by inheritance.
Inheritance let us inherit attributes and methods from a parent class to a child class.
Polymorphism let us use those methods to perform different tasks (allow us to perform a single action in different
ways).
Abstraction and Interfaces
Data abstraction is the process of hiding certain details and showing only essential information to the user.
“abstract” keyword → non-access modifier → used for classes and methods
Abstract class – a restricted class that cannot be used to create objects (to access it, it must be inherited from
another class)
Abstract method – can only be used in an abstract class and it does not have a body. The body is provided by the
subclass (inherited form).
Instanceof operator
It is used to test whether the object is an instance or the specified type (class or subclass or interface).
If we apply instanceof operator with any variable that has null value, it returns false.

Upcasting and down casting of instanceof

Down casting
It is when child class refers to the object of parent class
If you perform down casting directly, it will show compile error. So we use type casting way to perform down
casting.
toString() method
it is used
- If you want to represent any object as a String → it returns the String representation of the object

You might also like