Deriving a class and Polymorphism in java.

In this article,Deriving a class  and Polymorphism in java are explained in detail with examples.
This concept is related to inheritance.Hence we inherit properties and methods of existing class into a new class.
Deriving a class in Java-Javaform
Here, inheritance is done as follows (Using extends keyword).
Ex
class A
{
-----
-----}
class B extends A
{
-----
-----
}
class A members can be used directly in class B hence, the advantage is "re-usability".
Base class
      |
      |
      |
Derived class
Note
Using the object of derived class we can access base class members, this is re-usability.
Ex
//program related to Deriving a class  and Polymorphism in java--derived class
class ParentClass
{
int pi;
ParentClass()
{
pi=100;
}
void ParentMethod()
{
System.out.println("Parent class Method");
}
}
class ChildClass extends ParentClass
{
int ci;
ChildClass()
{
ci=200;
}
void ChildMethod()
{
System.out.println("Child class Method");
}
}
class DerivingClass
{
public static void main(String args[])
{
ChildClass ch=new ChildClass();
System.out.println("childclass ci: "+ch.ci);
System.out.println("Parent class pi: "+ch.pi);
ch.ChildMethod();
ch.ParentMethod();
}
}
Output
DerivingClass-Output-Javaform
                         To get the code file use me.

Polymorphism in Java.
The existence or Behaviour of a single product in many ways depending on some condition is known as polymorphism.
Poly means manyMorphism means forms.
Ex 
Here water can be used in many forms (Liquid,Solid,Gaseous) according to the temperature condition.
Polymorphism Example-Javaform
Polymorphism is done through methods in java.They are implemented in two ways.
Polymorphism types-Javaform
Static polymorphism
It is known as compilation time polymorphism because, during compilation of java program, JVM knows which method should be executed at a particular line in a program.
Constructor overloading
Two or More constructors in a single class with same name and atleast one difference in "signature" is known as constructor overloading. (From topic Deriving a class  and Polymorphism in java)
Signature means a number of arguments, order of arguments and data type of arguments.
Ex
class A
{
A(){---}                // default constrcutor with 0 arguments (i.e, no variable in between ())
A(int,int){---}       //Parameterized-constructor with 2 arguments. 
A(float,float){---} //Parameterized-constructor.
A(int,float){---}    //Parameterized-constructor.
A(float,int){---}   //Parameterized-constructor and datatype of arguments are different in the sequence for this parameter and its above parameter. (From topic Deriving a class  and Polymorphism in java)
}

Ex
//program related to Deriving a class  and Polymorphism in java--constructor overloading.
class ConstructorOverloading
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student(1002,"Ram",21);
s1.showData();
System.out.println();
s2.showData();
}
}
class Student
{
int rollno;
String name;
int age;
Student() //default constructor
{
rollno=1001;
name="Sai";
age=20;
}
Student(int r,String s,int a) //parameterized constructor 
{
rollno=r;
name=s;
age=a;
}
void showData()
{
System.out.println("RollNo is: "+rollno);
System.out.println("Name is: "+name);
System.out.println("Age is: "+age);
}
}
Output
Constructor Overloading-javaform
                           To get the code file use me.

Continue to next topic Method Overloading.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required