Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Local Variables and Access Modifiers

Can access modifiers be applied to local variables? NO!


There is never a case where an access modifier can be applied to a local variable,
so watch out for code like the following:
class Foo {
void doStuff() {
private int x = 7;
this.doMore(x);
}
}
You can be certain that any local variable declared with an access modifier will
not compile. In fact, there is only one modifier that can ever be applied to local
variables—final.
Inheritance:-
1) Single Inheritance
Single inheritance is damn easy to understand. When a class extends another one class only then we call it a single inheritance. The below flow
diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.

Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}

Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
2) Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple
Inheritance is supported in C++.

3) Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class,
thereby making this derived class the base class for the new class. As you can see in below flow diagram C
is subclass or child class of B and B is a child class of A.
Class A {
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}
4) Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example class B,C
and D inherits the same class A. A is parent class (or base class) of B,C & D.

5) Hybrid Inheritance
In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple
inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in
the java in a same way as multiple inheritance can be!! Using interfaces. Yes you heard it right. By
using interfaces you can have multiple as well as hybrid inheritance in Java.
Polymorphism
Polymorphism is the capability of a method to do different things.
1) Method Overloading 2) Method Overriding
1)Method Overloading:
In Java, it is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This
concept is known as Method Overloading.
class Overload
{
void show(int a)
{
System.out.println ("a: " + a);
}
void show(int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double show(double a) {
System.out.println("double a: " + a);
return a*a;
}}
• A reference variable can be of only one type, and once declared, that type
can never be changed (although the object it references can change).
• A reference is a variable, so it can be reassigned to other objects, (unless the
reference is declared final).
• A reference variable's type determines the methods that can be invoked on
the object the variable is referencing.
• A reference variable can refer to any object of the same type as the declared
reference, or—this is the big one—it can refer to any subtype of the
declared type!
• A reference variable can be declared as a class type or an interface type. If
the variable is declared as an interface type, it can reference any object of any
class that implements the interface.
Rules for Method Overloading
• Overloading can take place in the same or in its sub-class.
• Constructor in Java can be overloaded
• Overloaded methods must have a different argument list.
• The parameters may differ in their type or number, or in both.
• They may have the same or different return types.
• It is also known as compile time polymorphism, compile time binding, early binding.
2) Method Overriding
Child class has the same method as of base class. In such cases child class overrides the parent class method without
even touching the source code of the base class. This feature is known as method overriding.
class Human{
public void eat()
{
System.out.println("Human is eating");
}
}
class Ram extends Human{
public void eat(){
System.out.println(“Ram is eating vag.");
}
public static void main( String args[]) {
Ram obj = new Ram();
obj.eat();
}
}
Advantage of method overriding
The main advantage of method overriding is that the class can give its own specific implementation to a
inherited method without even modifying the parent class(base class).
Method Overriding in dynamic method dispatch
Dynamic method dispatch is a technique which enables us to assign the base class reference to a child class object. As you can see
in the below example that the base class reference is assigned to child class object.
class Parent{
public void disp()
{
System.out.println("disp() method of parent class");
}
public void abc()
{
System.out.println("abc() method of parent class");
}
}
class Child extends Parent{
public void disp(){
System.out.println("disp() method of Child class");
}
public void xyz(){
System.out.println("xyz() method of Child class");
}
public static void main( String args[]) {
//Parent class reference to child class object
Parent obj = new Child();
obj.disp(); //disp() method of Child class abc() method of parent class
obj.abc();
}
}
Note: In dynamic method dispatch the object can call the overriding methods of child class and all the non-overridden
methods of base class but it cannot call the methods which are newly declared in the child class.
obj.xyz();//Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method xyz() is undefined
for the type Parent.
Rules of method overriding in Java
• Argument list: The argument list of overriding method must be same as that of the method in parent class. The
data types of the arguments and their sequence should be maintained as it is in the overriding method.
• Modifiers : The Modifiers of the overriding method (method of subclass) cannot be more restrictive than the same
method of parent class. For e.g. if the Modifiers of base class method is public then the overridden method (child
class method ) cannot have private, protected and default Modifiers as these all three are more restrictive than
public.
• private, static and final methods cannot be overridden as they are local to the class. However static methods can
be re-declared in the sub class this is called method hiding.
• Overriding method (method of child class) can throw any unchecked exceptions, regardless of whether the
overridden method(method of parent class) throws any exception or not. However the overriding method should
not throw checked exceptions .
• Binding of overridden methods happen at runtime which is known as dynamic binding , runtime binding or late
binding.
• If a class is extending an abstract class or implementing an interface then it has to override all the abstract
methods unless the class itself is a abstract class.
Super keyword in Overriding
super keyword is used for calling the parent class members/constructor. super.methodname() calling the specified
method of base class while super() calls the constructor of base class.
class ABC{
public void mymethod()
{
System.out.println("Class ABC: mymethod()");
}
}
class Test extends ABC{
public void mymethod(){
//This will call the mymethod() of parent class
super.mymethod();
System.out.println("Class Test: mymethod()");
}
public static void main( String args[]) {
Test obj = new Test();
obj.mymethod();
}
}

You might also like