Repository navigation
Expand file tree
/
Copy pathInheritance_Concepts
More file actions
65 lines (61 loc) · 2.98 KB
/
Copy pathInheritance_Concepts
File metadata and controls
65 lines (61 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* ################## Inheritance Concepts ############################### */
/*
protected to private in subclass -- not possible weaker access privileges in base class
-- Cannot go from public -> protected or protected -> private in Base Class
baseclass typecasted to subclass -- Possible.. All the variables and methods referring to Base will now refer to
-- Derive class as if everything is initialised by new Derive()
-- Example: Base b = new Derive();
-- Derive d = (Derive)b;
-- d.overridenMeth() points to Derive class method -> output : Derive
overiding static method -- can be overriden and new Derive().overridenMeth() points to base class method
-- if static specifier is used and reference variable is of Base class
-- Example : Base b = new Derive().overridenMeth() -> output : Base
accessing baseclass attribute by initialising subclass() in obj variable
-- Base class variable can be and will only(as it is Class level variable) be accessed if or protected
-- variable has public access specifier and only if the reference variable is of Base class
*/
/*###########################################################################*/
// Weaker access privileges
// Cannot go from public -> protected or protected -> private in Base Class
// Static Concept : if methods are static Class Method value will be
// assigned to new Object at the time of creation
// Base b = new Derive() -> Base... in case of static Otherwise -> Derive
// In case of varibles value will be specific to the class only
// in which it is defined as scope of variable is for class
// and no overriding happens for variables
/*##############################################################################*/
class Base {
protected static String name = "ankit";
public int val = 5;
protected static void kat(){
System.out.println("Base");
}
protected void meth2(){
System.out.println("Base Method 2");
}
}
class Derive extends Base {
public static String name = "singh";
protected int val = 10;
protected static void kat(){
System.out.println("Derive");
}
public void meth2(){
System.out.println("Derive Method 2");
}
}
public class HelloWorld{
public static void main(String []args){
Base b = new Derive();
b.kat(); // Base .../as it is static method
b.meth2(); // Derive Meth 2 ../as it is non-static method
System.out.println(b.name); // ankit ../as local class level variable
System.out.println(b.val); //
Derive d = (Derive)b;
d.kat(); // Derive ../as it is now initialized by derive class variable
// works for static keyword as well as if new Derive() is called
d.meth2(); // Derive Method 2 ../as it is non-static local method
System.out.println(d.name); // singh ../as everything now is of Derive class
System.out.println(d.val); // 10 ../no explanation needed I guess
}
}