à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

OOPS in JAVA 11


OOPS in JAVA 11

Inheritance

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The class whose properties are inherited is known as superclass (base class, parent class) and the class which inherits the properties of other is known as subclass (derived class, child class).

Inheritance represents the IS-A relationship, also known as parent-child relationship.


extends Keyword

extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

class Super
{
    String name;
}
class Sub extends Super     
{            
    //methods and fields
    String name;  
    void show()
    {
          super.name = "Super Class";   //name of Super class
          name = "Sub Class";    //name of Sub class
    }
} 

super Keyword

In Java, super keyword is used by a subclass whenever it need to refer to its immediate super class.

1. It is used to differentiate the members of superclass from the members of subclass, if they have same names.

2. It is used to invoke the superclass constructor from subclass.

super.variable
super.method();


Purpose of Inheritance

1. To promote Code Reusability.

2. To use Polymorphism.

 

Types of Inheritance

1. Single Inheritance

When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.

class A {

}
class  B  extends  A {

}

2. Multilevel Inheritance

When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.

class A {

}
class  B  extends  A {

}
class  C  extends B {

}

3. Heirarchical Inheritance

In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.

class A {

}
class  B  extends  A {

}
class  C  extends A {

}
class  D  extends A {

}

NOTE : Multiple inheritance is not supported in java.

 

Q. Why multiple inheritance is not supported in java???

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.

Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.

 

Example:
class A
{  
	void msg()
	{
		System.out.println("Hello");
	}  
}  
class B
{  
	void msg()	
	{
		System.out.println("Welcome");
	}  
}  
class C extends A,B	//suppose if it were  
{     
   
	Public Static void main(String args[])
	{  
  		C obj=new C();  
   		obj.msg();            
		//Now which msg() method would be invoked?  
	}  
}  
 
Output:

Compile Time Error

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website