You are on page 1of 7

Static Variables and Blocks"

Static blocks gets executed prior to constructors as static variables and methods
gets loaded first into memory.
To access the static variables, we simply need the class name nothing else.
Static Methods
They are public to class.
Static methods can change the values of static variables.
Static methods cannot use this and super keyword
static methods cannot call non static methods and variables directly without
objects.
Make a method as static if it doesn't use any instance variables and methods.
Normal Methods can access static variables directly.

Inheritence
Subclass cannot access private variables and methods of superclass.

Method overriding means same method present in subclass as well as in superclass.


The methods are exactly and completely same.
Access Modifiers :
Public, Private, Default, Protected
Outer Class can be only Public or default.
Inner class can have any access type mentioned above.
Private Method :
A private method can only be accessed in the class and not from any other class.
If we make a constructor as private, then noone can create object of the class from
other class.
Default Method :
Default methods can be accessed from other classes of the same package.
However the default method cannot be accessed outside the package in any other
class.
Public method :
They can be accessed from any class in the same package or different package.
Protected Method :
They can be accessed from class present in the same package or different pakage
but only via inheritence.

Imp Concept in case of method overriding(Inheritence) :


Precedence order
Type

Counter

Private

Default

Protected

Public

The counter value of method in child class must always be higher than that of its
parent.
Eg: If a method is default in Parent, the counter value is 2. The child can have type
default,Protected,Public as their counter value is >=2.
It cant be private as it is lesser than 2.

Super Keyword: This is basically used to access the methods of the parent class.
Syntax:
super() This calls parent class constructor
super.methodname() This calls parent class methodname.
Imp: Super and this can't be used in static methods

SuperClass Subclass Typecasting :

Imp formulae to remember:


Person P3 = new Student();
here the reference variable must have all variables mentioned in the object
student.
That means RHS must have all components present in itself which are present in
LHS.

Instanceof Operator() :
the syntax is
(reference variable instanceof ClassName)
result is true/false

1. class Simple1{
2.

public static void main(String args[]){

3.

Simple1 s=new Simple1();

4.

System.out.println(s instanceof Simple1);//true

5.

6. }

Object of subclass is an instance of parent class.

class Animal{}
class Dog1 extends Animal{//Dog inherits Animal

public static void main(String args[]){


Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}

Abstract Class:

Use abstract keyword before Class to make it abstract.


Object for abstract class cannot be created.
They can be only used as superclass which extends subclass
Presence of abstract method makes the class abstract.
It's not mandatory that abstract class must have abstract methods.
Abstract method:
abstract void method(); // doesn't have any body
If a class inherits a abstract class having abstract method, then that method must
have implementation that is body in the subclass . If not done then the inherited
class must also be declared as abstract.

Wrapper Class :