Inheritance in java


Inheritance

Java Class එකක තියෙන දේවල් තවත් Class එකකට ලබා දීම Inheritance ලෙසින් හදුන්වයි. ඒ සදහා Class Name එකට ඉදිරියෙන් extends යන Keyword එකද,  Inherit කිරිමට අවශ්‍ය Class එකෙහි Name එකද ලබා දේ. තවද Inheritance එක ලබා දෙන Class එක Super Class / Parent Class යන නම් වලින් හදුන්වන අතර Inheritance එක් ලබා ගන්නා Class එක  Child Class / Sub Class යන නම් වලන් හදන්වයි. 

එනම් Super Class එකකට අයත් දේවල්  Sub Class එකකට ලබා දීම Inheritance නම් වේ. 

Ex :-


 public class Employee {
    double Salary = 276889.89;
}

class Programmer extends Employee{

int bonus = 45000;
public static void main(String[] args) {

Programmer p = new Programmer();

System.out.println("Programmer Salary = " + p.Salary);
System.out.println("Programmer Bonus = " + p.bonus);
}
}

 මෙහි Programmer Class එකෙහි Salary නැමැති Variable එක නැති නමුත් Employee Class එක Inherit කර ඇති Code එක Run වේ.

Comments

Popular posts from this blog

What are the 'Java Standards' ???? 🤔

What are the 'Java Data Types' and 'Access Modifiers' ???? 🤔

Java Main Method and Construtor

Java Method Overloading

Encapsulation

Abstraction in Java

Polymorphism