Showing posts with label inheritance in java. Show all posts
Showing posts with label inheritance in java. Show all posts

Wednesday, 29 February 2012

What is inheritance


This is always  nice if we could reuse something that already exist rather than creating the same all over again.  This  is done by creating a new classes , reusing the properties of existing ones. This mechanism of deriving a new class from an old one is called inheritance . The old class is known as base class or parent class or super class and new class is known as derived class or child class.
 Inheritance facilitates a class to acquire the properties and functionality of another class . the new class depicts the acquired properties  and behavior of the existing class as well as its own unique properties and behavior.
The syntax to define subclass
 Class sub_class extends super_class
{
//variables and methods declaration
}
Types of inheritance :-
1)      Single inheritance
inheritance in java, single inheritance







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
2)      Hierarchical  inheritance

inheritance in java







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class1
{
 //variables and methods declaration
}
3)      Multilevel Inheritance

inheritance in java







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class2
{
 //variables and methods declaration
}
4)      Multiple Inheritance

inheritance in java

3





class Class1
{
//variables and methods declaration
}
class Class2
{
//variables and methods declaration
}
class Class3 extends Class2 extends Class1

{
 //variables and methods declaration
}

Wednesday, 15 February 2012

interface in java

there are some conditions when it is important that whenever a particular entity is created it must have some methods . Means that methods are mandatory for that entity.
Like if we created a bike then it must have a break , race, petrol tank etc. these things are mandatory for a bike.

So if we say that a class must have some particular methods in it then we used interface.
interface is like a class similar to a class, that can contain only constants, method signatures, and nested types.
There are no method bodies. interface cannot be instantiated they can only be implemented by classes or extended by other interface.
which class impement the interface, all the methods which declared in interface will be define in that class .

Example of interface

interface is declared similer as class only interface keyword is used insted of class
no braces for methods and method is terminated by semicolon(;)

public interface TestInterface {
void frontBreak(String bikename);
void backBreak(String bikename);
}

//class implements interface
To use an interface, you write a class that implements the interface.
When class implements an interface, it provides a method body for each of the methods declared in the interface.

public class bikeClass implements TestInterface {

public void frontBreak(String bikename) {
//write method body here

}

public void backBreak(String bikename) {
//write method body here

}
}
Related Posts Plugin for WordPress, Blogger...