What
is Java:-
Java
is an object-oriented programming language developed initially by James Gosling
and colleagues at Sun Microsystems. The language, initially called Oak (named
after the oak trees outside Gosling's office), was intended to replace C++,
although the feature set better resembles that of Objective C. Java should not
be confused with JavaScript, which shares only the name and a similar C-like
syntax. Sun Microsystems currently maintains and updates Java regularly.
What
is Class:-
A class defines the properties and behaviour (variables
and methods) that is shared by all its objects. In this world all the things
are an object and all the objects are categorized in a special group
that group are termed as a class. Object is the feature of a class which is used for
the working on the particular properties of the class or its group.
What is Objects:-
Object is the basic run time entity of OOP
language. Class itself does nothing but the real functionality is achieved
through their objects. Object is an instance of the class. It takes the
properties (variables) and uses the behaviour (methods) defined in the
class. An object is a software bundle of variables and related methods of
the special class. In real-world objects share two characteristics: They have
all state and behaviour. For example, the squares have state such as: sides and
behaviours such as its areas and perimeters. A method is a function
(subroutine) associated with an object.
What is Constructor:-
Every class has at least one its own constructor.
Constructor creates an instance for the class. Constructor initiates
(initialize) something related to the class's methods. Constructor is the
method which name is same to the class. But there are many differences between
the method (function) and the Constructor. They have no return
type, Not
even void.
What is Constructor Overloading:
A
default constructor with no arguments will be called automatically by the Java
Virtual Machine (JVM). Constructor is always called by new operator. Constructors don’t have
any return type. JVM differentiates overloaded constructors on the basis of
arguments passed in the constructor.
Java as an Object Oriented Language:-
Introduction: This
is a technique used to develop programs revolving around the real world
entities. In OOPs programming model, programs are developed around data rather
than actions and logics. In OOPs, every real life object has properties and
behaviour which is achieved through the class and object creation. OOPs provide
a better flexibility and compatibility for developing large applications.
There are four main pillars of an Object Oriented
Programming Language:
· Inheritance: Inheritance
allows a class (subclass) to acquire the properties and behaviour of another class
(super class) and adding the additional features as you need. A class can
inherit only one class (super class) at a time but a class can have any number
of subclasses. It helps to reuse, customize and enhance the existing code. So
it helps to write a code accurately and reduce the development time.
Inheritance is the process by which objects of one class acquire the properties
of objects of another class. It is one of the most important features of OOP.
It is the concept of hierarchical classification. To derive a class in java the
keyword extends is used.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + "
" + j);
}
}
class B extends A
{
// Create a subclass by extending class A.
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
superOb.i = 10; // The
superclass may be used by itself.
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
subOb.i = 7; /* The subclass has access to
all public members of its superclass. */
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in
subOb:");
subOb.sum();
}
}
|
The output from this program is shown here:
Contents of
superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k
in subOb:
i+j+k: 24
The concept of
inheritance is used to make the things from general to more specific e.g. When
we hear the word vehicle then we got an image in our mind that it moves from
one place to another place it is used for travelling or carrying goods but the
word vehicle does not specify whether it is two or three or four wheeler
because it is a general word. But the word car makes a more specific image in
mind than vehicle, that the car has four wheels. It concludes from the example
that car is a specific word and vehicle is the general word. If we think
technically to this example then vehicle is the super class and car is
the subclass or child class because every car has the features of its parent
(in this case vehicle) class.
The following kinds
of inheritance are there in java.
- Simple Inheritance
- Multilevel Inheritance
Simple Inheritance: When
a subclass is derived simply from its parent class then this mechanism is
known as simple inheritance. In case of simple inheritance there is only a sub
class and its parent class. It is also called single inheritance or one level
inheritance.
class A {
int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System.out.println(x); } } class B extends A{ public static void main(String args[]){ A a = new A(); a.get(5,6); a.Show(); } void display(){ System.out.println("B"); } } |
Multilevel
Inheritance: It is the enhancement of the concept of
inheritance. When a subclass is derived from a derived class then this
mechanism is known as the multilevel inheritance. The derived class is called
the subclass or child class for its parent class and this parent class works as
the child class for it's just above (parent) class. Multilevel
inheritance can go up to any number of levels. e.g.
Pictorial
Representation Multilevel Inheritance
class A {
int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System.out.println(x); } } class B extends A{ void Showb(){ System.out.println("B"); } } class C extends B{ void display(){ System.out.println("C"); } public static void main(String args[]){ A a = new A(); a.get(5,6); a.Show(); }
}
|
|
Java does not
support multiple Inheritance
Multiple
Inheritances: The mechanism of inheriting the features of more
than one base class into a single class is known as multiple inheritances. Java
does not support multiple inheritances but the multiple inheritances can be
achieved by using the interface by implementing more than one interface in a
class.
super keyword: The super is
java keyword. As the name suggest super is used to access the members of the
super class. It is used for two purposes in java.
The first use of keyword super is to access the hidden
data variables of the super class hidden by the sub class. e.g. Suppose class A
is the super class that has two instance variables as int a and float b.
class B is the subclass that also contains its own data members named a and b.
then we can access the super class (class A) variables a and b inside the
subclass class B just by calling the following command.
super.member: Here member
can either be an instance variable or a method. This form of super most
useful to handle situations where the local members of a subclass hide the
members of a super class having the same name. The examples clarify all the
confusions.
class A{
int a; float b; void Show(){ System.out.println("b in super class: " + b); } } class B extends A{ int a; float b; B( int p, float q){ a = p; super.b = q; } void Show(){ super.Show(); System.out.println("b in super class:"+super.b); System.out.println("a in sub class: " + a); } public static void main(String[] args){ B subobj = new B(1, 5); subobj.Show(); } } |
||
Use of super to
call super class constructor: The second use of the keyword
super in java is to call super class constructor in the subclass. This
functionality can be achieved just by using the command: super(param-list);
Here parameter list
is the list of the parameter requires by the constructor in the super class.
super must be the first statement executed inside a super class constructor. If
we want to call the default constructor then we pass the empty parameter list. Example:
class A{
int a; int b; int c; A(int p, int q, int r){ a=p; b=q; c=r; } } class B extends A{ int d; B(int l, int m, int n, int o){ super(l,m,n); d=o; } void Show(){ System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } public static void main(String args[]){ B b = new B(4,3,8,7); b.Show(); } } |
· Encapsulation: Encapsulation
is the ability to bundle the property and method of the object and also operate
them. It is the mechanism of combining the information and providing the
abstraction as well.
Encapsulation is
the process of binding together the methods and data variables as a single
entity. It keeps both the data and functionality code safe from the
outside world. It hides the data within the class and makes it available only
through the methods. Java provides different accessibility
scopes (public, protected, private, default) to hide the data from outside.
Here we provide an example in which we create a class "Check" which
has a variable "amount" to store the current amount.
Now to manipulate this variable we create a methods and to set the value
of amount we create setAmount () method and to get the value
of amount we create getAmount () method.
Here is the code
for "Mainclass" class :
class Check{
private int amount=0; public int getAmount(){ return amount; } public void setAmount(int amt){ amount=amt; } }
public class Mainclass{
public static void main(String[] args){ int amt=0; Check obj= new Check(); obj.setAmount(200); amt=obj.getAmount(); System.out.println("Your current amount is :"+amt); } } |
Here the data
variable "amount" and methods setAmount() and getAmount() are
enclosed together with in a single entity called the "Check" class.
These two methods are used to manipulate this variable i.e. set and get
the current value of amount.
·
· * Polymorphism: “One name
multiple form”, it is the way that provide the different functionality by the
functions having the same name based on the signatures of the methods.
Polymorphism allows one interface to be used for a set of actions i.e. one name
may refer to different functionality. Polymorphism allows an object to accept
different requests of a client and responds according to the current state
of the runtime system.
There are two types
of polymorphism:
1. Compile-time
polymorphism
2. Runtime
Polymorphism
In compile
time Polymorphism, method to be invoked is determined at the compile
time. Compile time polymorphism is supported through the method
overloading concept in java.
Method overloading
means having multiple methods with same name but with different signature
(number, type and order of parameters).Example:
class A{
public void fun1(int x){ System.out.println("The value of class A is:"+x); } public void fun1(int x,int y){ System.out.println("The value of class B is : " + x + " and " + y); } } public class polyone{ public static void main(String[] args){ A obj=new A(); obj.fun1(2); obj.fun1(2,3); } } |
In runtime polymorphism,
the method to be invoked is determined at the run time. The example of run time
polymorphism is method overriding. When a subclass contains a
method with the same name and signature as in the super class then it is called
as method overriding.
class A{
public void fun1(int x){ System.out.println("int in Class A is : "+ x); } } class B extends A{ public void fun1(int x){ System.out.println("int in Class B is : "+ x); } } public class polytwo{ public static void main(String[] args){ A obj; obj= new A(); // line 1 obj.fun1(2); // line 2 (prints "int in Class A is : 2") obj=new B(); // line 3 obj.fun1(5); // line 4 (prints ""int in Class B is : 5") } } |
|
In the above
program, obj has been declared as A type. In line 1, object of
class A is assigned. Now in the next line, fun1 (int) of class A will be
called. In line 3, obj has been assigned the object of class C so fun1 (int) of
class C will be invoked in line 4. Now we can understand that same name of the
method invokes different functions, defined in different classes, according to
the current type of variable "obj". This binding
of method code to the method call is decided at run time.
Method Overloading in java:
Polymorphism is the
capability of an object to respond uniformly to achieve specific behaviour to
the method calls of the same name but with different implementations.
In java the concept
of Polymorphism is achieved in two ways:
1. Method
Overloading.
2. Method Overriding.
Concept of Method Overloading:
-- In a class, the
concept of method overloading does not allow the external user to be aware
about the internal processing of the system
-- It just allows to
user to use the different implementations of same name collected together and
react appropriately to the supplied parameters to get the desired output.
-- Method Overloading
allows the user to achieve the compile time polymorphism.
-- Overloaded methods
are always the part of the same class. These methods have the same name,
but they may take different input parameters.
-- The arguments
passed to overloaded method may differ in type or in number, or both.
-- Overloaded methods may have the same or different
return types.
Java method Overriding: Method overriding in java means a subclass method overriding a super
class method. Superclass method should be non-static. Subclass uses extends
keyword to extend the super class. In the example class B is the sub class and
class A is the super class. In overriding methods of both subclass and
superclass possess same signatures. Overriding is used in modifying the
methods of the super class. In overriding return types and constructor
parameters of methods should match.
class A {
int i; A(int a, int b) { i = a+b; } void add() { System.out.println("Sum of a and b is: " + i); } } class B extends A { int j; B(int a, int b, int c) { super(a, b); j = a+b+c; } void add() { super.add(); System.out.println("Sum of a, b and c is: " + j); } } class MethodOverriding { public static void main(String args[]) { B b = new B(10, 20, 30); b.add(); } |
No comments:
Post a Comment