Method Overriding
When a method in a subclass has the same name and type signature as a method in its superclass , then the method in the subclass is said to override the method in the superclass.
When an overriden method is called from within the subclass , it will always refer to the version of that method defined by the subclass.The version defined by superclass will be hidden.
Program: //Method overriding
class A
{
int i,j ;
A(int a , int b)
{
i=a;
j=b;
}
void show()
{
System.out.println(" i and j=" + i +" " + j);
}
}
class B extends A
{
int k;
B( int a , int b , int c)
{
super(a,b);
k=c;
}
void show( )
{
System.out.println("k=" + k);
} Output:
} k=9
class OverrideDemo
{
public static void main ( String [] args )
{
B b= new B ( 2,3,4);
b.show( );
}
}
Abstract Classes
Abstract keyword can be used with both class_name and method_name. An abstract class will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.
The subclass should override all necessary methods.We can also specify the method of superclass as abstract. Such methods must be overriden by the following subclass's.
The general form is :
abstract type name ( parameters_list);
The members of abstract class cannot be accessed via new operator but are instead accessed via reference variables.
Program: //abstract demonstration
abstract class A
{
abstract void callme ( );
void callMetoo ( )
{
System.out.println(" This is a concrete method");
}
}
class B extends A
{
void callme ( )
{
System.out.println ( " B's implementation of callme");
}
}
class AbstractDemo
{
public static void main ( String [] args )
{
B b = new B ( );
b.callme () ;
b.callMetoo();
}
}
In order to prevent method overriding , we use final keyword before the declaration.
By: Knowledge Bits
There are basically 4 types of Inheritance in Java. These are :
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Java does not support Multiple inheritance. In java , multiple inheritance is applied via interfaces.
Single Inheritance
This is a simple format , in which A class is inherited by a single base class and so on.
Ex- class A
{
//Body of class
}
class B Extends A
{
//Body of class
}
Multilevel Inheritance
In Multilevel inheritance , a single base class is inherited by 2 or more derived classes.
Ex- class A
{
//Body of class A
}
class B extends A
{
//Body of class B
}
class C extends A
{
//Body of class C
}
Hierarchical Inheritance
In this , the class inherits other classes such that it forms a hierarchy , like a tree structure.
Ex- class A
{
//Body of A
}
class B extends A
{
//Body of B
}
class C extends A
{
//Body of C
}
class D extends B
{
//Body of D
}
.
.
.
and so on.
Hybrid Inheritance
Hybrid as the name suggests is the combination of 2 or more inheritance types put together.
Ex- class A
{
//Body of A
}
class B extends A //Single Inheritance
{
//Body of B
}
class C extends B
{
//Body of C
}
class D extends B // Multilevel Inheritance
{
//Body of D
}
By: Knowledge Bits
Inheritance
Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification.
To inherit a class , you simply incorporate the definition of one class into another by using extends keyword.
Program:
//To illustrate inheritance
Class A
{
int i=10,j=20;
void showij( )
{
System.out.println(" i and j" + i + " " + j);
}
}
Class B extends A
{
int k=30;
void showk ( )
{
System.out.println( " k=" + k);
}
void sum ( )
{
System.out.println ("i+j+k =" + (i+j+k));
}
}
class InheritanceDemo
{
public static void main ( String args [] ) Output:
{ i and j = 10 20
B b1 = new B( ); j = 30
b1.showij (); i+j+k = 60
b1.showk();
b1.sum ( );
}
}
Super Keyword
Whenever a subclass needs to refer to its immediate superclass , it can do so by the keyword Super.
It has 2 general forms:
First , it is used to call the base class constructor. This can be done by following form :
super( parameters_list);
Second , It is used to access a member of the superclass that has been hidden by a member of a subclass. This can be done by :
Super.member;
Where member can be either a method or an instance variable.
By: Knowledge Bits
Access Specifiers
Access control is one of the most important attribute of Encapsulation. Through this we can control what parts of program can access the members of a class.
Access specifiers modifies the declaration of members.There are 3 basic access specifiers : public , private , protected. Protected apples only when inheritance is involved.
When the members are modifies as public , then that member can be accessed by any other code. When a member of a class is specified as private, then that member can only be accessedd by other members of its class.
When no access specifier is used , then by default the member of a class is public within its own package , but cannot be accessed outside of its package.
Static keyword
When a member is declared as static , it can be accessed before any objects of its class are created and without reference to any object. Both member and method can be declared as static.
That is why main ( ) is declared as static as it must be called before any object exist. When object of class are declared , no copy of static variable is made and all the instances of the class share same static variable.
Final Keyword
When a variable is declared as final , it prevents its contents from being modified. So , the final variable must be intialized when it is declared.
Variable declared as final do not occupy memory on a per instance basis.So , a final variable is essentially a constant.
Final is also used to prevent method overriding. To disallow a method from being overridden , we specify final as a modifier at the start of its declaration.The method declared as final canot be overridden.
Final is also used to prevent inheritance. when we precede the class declaration with the final keyword , we prevent it from being inherited.
Declaring a class final implicitly declares all of its method as final.
By: Knowledge Bits
From the Previous Blog we have discussed some of the components of java. Here are some of the other features of java.
Method Overloading
When a single class has multiple methods with same name , this is concept is called as Method Overloading . The compiler differenciate b/w the 2 methods based upon the number and type of arguments.
Program:
// To illustrate method overloading
class SumDemo
{
void sum ( int a , int b)
{
System.out.println(" Sum of 2 number =" + (a+b));
}
void sum ( float a , float b)
{
System.out.println(" Sum of 2 floating point number =" +(a +b));
}
void sum ( int a ,int b , int c )
{
System.out.println(" Sum of 3 numbers =" + ( a+b+c));
}
}
class MethodOverloadDemo
{
public static void main ( String [] args )
{
SumDemo s = new SumDemo( );
s.sum(12 ,13,15);
s.sum( 2.5F , 6.789F);
s.sum( 33, 678 );
}
}
Constructor Overloading
Like method overloading , constructor can be overloaded in the same manner. The compiler differenciates based on the number and type of arguments.
Program:
// To illustrate constructor overload
class Sum
{
Sum ( int a , int b)
{
System.out.println (" Sum of 2 number is =" + (a+b));
}
Sum ( float a , float b )
{
System.out.println ( " Sum of 2 float number is =" + (a+b));
}
Sum ( int a , int b , int c )
{
System.out.println (" Sum of 3 numbers is =" + (a+b+c));
}
}
class ConstOverloadDemo
{
public static void main ( String [] args )
{
Sum s = new Sum( 12 ,12,223);
Sum s = new Sum (23.4F , 34.8F);
Sum s = new Sum ( 25 , 34 );
}
}
Recursion
Recursion is a process in which a method calls itself again and again.
Program:
// Program to find factorial of a number
class Factorial
{
int fact ( int n )
{
int r ;
if ( n==1 )
return 1;
r = fact ( n-1) * n;
return r ;
}
} Output:
class recursion Factorial of 4 = 24
{
public static void main ( String [] args )
{
Factorial f = new Factorial ();
System.out.println (" Factorial of 4 =" + f.fact(4));
}
}
There are various other components of a class that affect it functioning actively or passively. These are described below.
Garbage Collection
Since objects are dynamically allocated by using the new operator , java handles the deallocation automatically. The technique used is called as Garbage Collection.
In this , when no reference to an object exist , that object is assumed to be no longer needed , and the memory occupied by the object can be reclaimed.
The garbage collection occurs sporadically during the execution of the execution of the program. It won't occur simply because 1 or more objects exist that are no longer used.
The finalize ( ) method
Sometimes an object will need to perform some action when it is destroyed. If an object is holding some non-java resource such as file handler or window character font , then you might want to make sure these resources are freed before an object is destroyed.
To handle such situations , java provide a mechanism called finalization. Through this , you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.
To add finalizer , we define finalize ( ) method.
syntax:
protected void finalize ( )
{
//finalization code
}
Constructor
Constructor is a special member function that has same name as that of class name. A constructor initializes an object immediately upon creation.
It has same name as that of class name in which it resides and is syntactically similar to method. It is automatically called when the objects of the class are instantiated.
Its does not has any return type , not even void. This is because , the implicit return type of a class' constructor is the class type itself.
Syntax-
class class_name {
class_name ( parameters list ) // constructor
{
// body of constructor
}
.
.
.
}
Types of Constructor
There are 3 types of constructor :
- Default constructor
- Parameterized constructor
- Default Constructor
Default Constructor
The default constructor does not has any arguments passed with it.
Program:
class BoxDemo
{
BoxDemo ( )
{
int a=10 ;
int b= 20 ;
}
void display ()
{
System.out.println (" Area of box =" + (a*b));
}
}
class DefaultConst
{
public static void main ( String args [] )
{
BoxDemo b = new BoxDemo ( );
b.display () ;
}
}
Output- Area of box = 200
Parameterized Constructor
Parameterized constructor has the parameters passed with it when the objects are created using new operator.
Program:
class BoxDemo
{
int length ;
int breadth ;
BoxDemo ( int l , int b )
{
length=l ;
breadth = b;
}
void display ( )
{
System.out.println( " Area of Box= "+ ( length * breadth ));
}
}
class ParamDemo
{
public static void main ( String args [] )
{
BoxDemo b = new BoxDemo (10,20 );
b.display ( );
}
}
Output: Area of box=200
Copy Constructor
In copy constructor , a copy of constructor is created which takes the object of the original constructor as arguments.
It is used when you do not want to alter the original contents of the constructor.
Program:
class BoxDemo
{
int length ;
int breadth ;
BoxDemo ( int a , int b )
{
length = a;
breadth = b;
}
BoxDemo ( BoxDemo obj )
{
System.out.println (" Copy Constructor Invoked " );
length = obj.length ;
breadth= obj.breadth ;
}
int area ()
{
return ( length * breadth ) ;
}
}
class CopyDemo
{
public static void main ( String [] args )
{
BoxDemo b1 = new BoxDemo ( 10,20) ;
BoxDemo b2 = new BoxDemo ( b1 ) ;
System.out.println ( " The area is = " + b1.area () ) ;
System.out.println ( " The area is = " + b2.area () ) ;
}
}
Output: The area is = 200
Copy Constructor invoked
The area is = 200
this keyword
this keyword can be used inside any method to refer to the current object , i.e , this is always a reference to the object on which the method was invoked.
Ex-
// To demonstrate this
Box ( double w , double h , double d )
{
this.width= w ;
this.height= h ;
this.depth = d ;
}
By: Knowledge Bits