Method Overriding and Object as a class Member.

In this article,Method Overriding and Object as a class Member are explained in detail with example.
Method overriding is related to inheritance, Here we re-define method of a base class in a subclass with the same name and same signature i.e, the number of arguments, the order of arguments, the data type of arguments, all should be same.
Ex
//program related to Method Overriding and Object as a class Member--method overriding
class ParentClass
{
void parentMethod() //over ridden method---
{
System.out.println("Parent class Method");
}
}
class ChildClass extends ParentClass
{
void ChildMethod()
{
System.out.println("Child Class Method");
}
void parentMethod() //overriding method
{
System.out.println("Parent class method in child class");
super.parentMethod();
}
}
class Methodoverriding
{
public static void main(String args[])
{
ChildClass ch=new ChildClass();
ch.ChildMethod();
ch.parentMethod(); //sub-class overriding method.
}
}
Output
MethodOverriding-output-javaform
                              To get the code file use me.

Note
Super is a keyword, using which we can call base class overridden method from sub-class overriding method only. (From topic Method Overriding and Object as a class Member)

Object as a Class Member.
An object of another class as a data member in your class.This concept is known as "aggregating" or "composition".
Ex
class A
{
data-member;
member-methods();
}
class B
{
A obj; /***/
data-members;
member-methods();
}
Here we can use properties and behaviour of class A in class B using the object of class A.
The advantage is "code-reusability".
This is known as "has-a-relationship" i.e, class B is having an object of class A as a member.
Here class B is known as a "container-class" and also relation is "containership".
In c++,this concept is referred as "delegation". (From topic Method Overriding and Object as a class Member)
Ex
//program related to Method Overriding and Object as a class Member--object as a class member.
class ObjectasMemberVariable
{
public static void main(String args[])
{
Exampleclass1 obj1=new Exampleclass1();
Exampleclass1 obj2=new Exampleclass1("Digital Marketing");
obj1.exampleMethod();
obj2.exampleMethod();
}
}
class Exampleclass1
{
String ss; //object of String class as a Data-member!
Exampleclass1()
{
ss="Hello World";
}
Exampleclass1(String s)
{
ss=s;
}
void exampleMethod()
{
System.out.println("Given String is: "+ss);
System.out.println("Length is: "+ss.length());
System.out.println("Lower-Case: "+ss.toLowerCase());
System.out.println("Upper-Case: "+ss.toUpperCase());
System.out.println("Replace with:  "+ss.replace('l','!'));
System.out.println("Sub-string is: "+ss.substring(1,4));
System.out.println("character at is: "+ss.charAt(6));
System.out.println();
}
}
Output
ObjectasMemberVariable-output-javaform
                               To get the code file use me.

Continue to next topic Method calling Mechanisms in Java.

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