JAVA 2.0 NOTES
4. Inheritance
Inheritance is the method of creating new class based on the already existing class. The new class derived is called as sub class or derived class which has all features of the existing class and its own. The existing class is called as super class or base class.
Adv : reusability of code, accessibility of variables and methods of the base class
by the derived class.
If the class is derived from more than one class , then it is called as multiple inheritance. This is not available in Java. To overcome this problem use interface.
Method Overriding
Whenever there are methods of the same name both in the base class as well as in the derived class and if that method is called by main, the method in the derived class is always executed. This is called overriding. See example11.
Example 11:
class A
{
void m( )
{
System.out.println("This is super class");}
}
class B extends A
{
void m( ) // it will override super class's method
{
System.out.println ("This is sub class");
}
}
Super
Super has two forms. First it calls the superclass’s constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass.
Example 12:
class A
{
int a = 10;
void A(int k)
{
System.out.printn("k is " + k);
}
void m( )
{
System.out.println("This is super class");
}
}
class B extends A
{
int a = 20;
void m( ) // it will override super class's method
{
System.out.println("This is sub class");
}
void call( )
{
super(5); // to call super class's constructor
m( ) ; // it will call the method in the derived class
super.m( ); // to call super class's method
int c = a ;
int d = super.a ; // to assign super class's varaible.
System.out.println("c = " + c + " d = " + d );}
}
class inh
{
public static void main(String args[ ])
{
B ob = new B( );
ob.call( ): // output is c = 20 d = 10}
}
Final
final variables - to create constant final float pi =3.14f;
final int a = 40
final is used to create constant variable.
b) final modifiers - to prevent overriding
Final modifiers are those to which no further alteration can be made. We can not override the method using final
Example 12:
class A
{
final void meth( )
{
System.out.println("this is the final method can not overridden");
}
}
class B extends A
{
void method( ) //this line will show error.
{
System.out.println("Illegal");}
}
c) final class - to prevent inheirtance
If the class is declared as final , we cannot inherit it. All methods in the final class are final
final class A
{
}
class B extends A // error cant subclass of A
{
}
Abstract
Sometimes we will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
To declare abstract class
abstract type name(parameters);
We must override the abstract method. An abstract class cannot be directly instantiated with the new operator. We cannot declare abstract constructors or abstract static methods.
abstract class A
{
abstract void callme( );
void display( )
{
System.out.println("This is example for abstract class"):
}
}
class B
{
void callme( )
{
System.out.println("B's implementation of callme");
}
}
class abst
{
public static void main(String a[])
{
B b = new B( );
b.callme( );
b.disp( );
}
}
No comments:
Post a Comment