References stored in STACK

References are also, stored in HEAP

SWAP
Below code will swap the references.
Below code will swap the properties of p1 and p2.
Static and Non static data members
a2.roi ne a1.roi ki value bhi change kr de?
becoz. roi is static variable.
Also, a1.roi=5 or a2.roi=6 is a wrong way, we should declare the static variables with class name.
BankAccount.roi=5;
Putting data members and functions into an entity (same class) is called encapsulation
OOPS: Real-world entities:
- does activities -> functions
- has features -> data members
Static Data Members
- are shared by all objects.
- belongs to classes, not objects.
Static Data Functions
such functions which don't require object for their invocation, can be called directly via class name.
These are used as follows:
- used in utility stateless [sara input parameters me mngwa lete hain] functions, eg. Math.min(), Arrays.sort()
- stateless - nothing is saved b/w 2 function calls, each call is independent and takes all input as parameters
Implications
- this keyword can't be used in static functions
- becoz static functions can be called by class name, in that case "this" won't know which instance to point to
- non-static data members can't be used in static functions
- becoz it can be called by class name, and in that case we don't know if any object exists
- if they do, we don't know which objects non-static data members to use.
- non-static function can't be used in static functions
- can be used, by making an object inside static function, and calling non-static function on it.
- can't be called directly
static void myStatic(){
// not allowed
nonStatic();
// allowed
MyClass myClass = new MyClass();
myClass.nonStatic();
}
- Can non-static function, use static data member? Why?
- yes, becoz, we have extra information available (not less)
- Can non-static function, use static function? Why?
==========================================================================
Polymorphism and Inheritance
Compiler watches left hand side i.e. reference
Runtime watches RHS i.e. instance
CASE 1: P obj = new P();
===========================================================================
CASE 2: C obj = new C();
Kya kya allowed hoga object par woh depend hoga reference par
Kya actually chalet woh depend hoga instances par
Compiler watches LHS i.e. reference
Runtime watches RHS i.e. instance
In case of conflict, resolved by reference.
Imp:
1. obj.d
2. ((P)obj).d
===========================================================================
CASE 3: P obj = new C();
obj.d2 and obj.fun2() are not allowed, as compiler will see the reference, and during compile time d2 and fun2() are not present in P
Also, when we do typecasting, it is applicable on reference type. Becoz of which ((C)obj).d2 and ((C)obj).fun2() are allowed, as during compile time we see reference of C which has both d2 and fun2().
===========================================================================
CASE 4: C obj = new P();
*************************************************************************************
*************************************************************************************
OBJECT CREATION
Static Block
Static Block is called after parsing and before default constructor.
NON STATIC INNER CLASS
Here we have non static inner class Person in Main class. And its object is created by creating object of Main class. hence, its not a good practice.
*************************************************************************************
*************************************************************************************
Inheritance and Polymorphism
Runtime Polymorphism: same reference se do karma ho rhe hain.
1st P obj = new P();
2nd P obj = new C();
cons value call Hong yeh depend krega runtime pr konsa object hai
Compile Time Polymorphism: same name methods with diff parameters
Copy Constructor
to create the copy constructor
Student s2 = new Student(s1);
its line se done ke apne apne contructor call ho jaenge.
*************************************************************************************
*************************************************************************************
Final
Final Variable: reassignment is not allowed
Final Data member: either assigned in parsing or constructor/ static block
Final Function:
- can't override the function in derived class
- overloading is allowed in same class and derived class
Final Class:
- can't derive the final class
*************************************************************************************
*************************************************************************************
Abstract
Abstract Class:
- can't make object of abstract class
Employee class
-> HR_Employee
-> Software_Employee
instance of Employee class derived into HR_Employee or Software_Employee. Hence, Employee classs should be abstract,
- as it is created only to give functionality
- not a type in itself
- only created to absorb common functions of HR_Employee and Software_Employee
Abstract function:
- which don't have code (only API)
- has ony parameters and return type
Why required?
to absorb common code of deriving classes
If function is abstract, will containing class be necessarily abstract?
yes, agr iss class ka object bnn gya and uske object ne vhi function call kr diya, toh kya chalega??
Hence, class will be abstract.
If class is abstract, will it necessarily have abstract functions?
No
OBJECT CREATION USING REFLECTION: without using new keyword

Questions |
Q1. What is final variable? |
Q2. What is final data member? |
Q3. When are they initialized? |
Q4. What is final fn? -> Can't override them in derived class |
Q5. What is a final class? -> Can't derive the final class |
Q6. What is abstract class? -> Can't make object |
Q7. What is abstract fn? -> A fn with no body. |
Q8. Can an abstract class have 0 abstract fns? -> Yes |
Q9. Can a class having abstract fns be marked non-abstract? -> False. Have to mark the class abstract or provide implementations for all abstract fns |
Q10. Ctor in abstract class? -> allowed |
Q11. Why abstract fn? -> TO enforce the deriving classes to provide implementations |
Q12. Why abstract class? -> To represent the common concept and absorb common functionality or data members.
************************************************************************************* ************************************************************************************* Interfaces
- When all fns are abstract and there are no common instance data members to share
| - Similar to abstract classes
| - An extreme version of abstract classes
| - when all fns are abstract
| - no common instance data members
| - If a class implements an interface but doesn't provide body for all functions, then it will become abstract
| - Can we make an object of interface? No
| - Can an interface provide body for any function? Yes, since Java8, using the default keyword it is possible (although advised to not use). Java provided for this backword compatibility. Not to be used often.
| - Can an interface have data members? Yes, but not as instance data members, can be used as static data members
| ABSTRACT CLASS vs INTERFACE
| - Abstract classes can have implementations for some functions, interfaces are supposed to have all functions with no implementations
| - Abstract classes can have instance data members, interfaces can only have static data members
| - A class can't derive two abstract classes, but can implement any number of interfaces
| - Interface data members are final and static
|
|