Inheritance is very important in Java and PHP programming. Here I will discuss inheritance in Java and PHP language. I hope this will help you much.
Inheritance is the extension of a class. To inherit a class, you use the definition of one class into another by 'extends' keyword. It allows a class to inherit members from another class. However, without any example it is difficult to understand it. So let's start with one of those.
This is the code of inheritance.You can practice with this code.
-----------------------------------------------------------------------------------CODE------------
package javaapplication10;
/**
*
* @author juwel
*/
public class JavaApplication10 {
public static void main(String[] args) {
class A {
int i, j;
void showij(){
System.out.println("i and j" + i + ""+j);
// TODO co // TODO code application logic here
}
void sum(){
System.out.println("i and j:"+(i+j));
}
}
class B extends A{
int k;
void showk(){
System.out.println("k: "+k);
}
void sum(){
System.out.println("i+j+k:"+(i+j+k));
}
}
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.print("Contents of SuperOb: ");
superOb.showij();
System.out.println("Sum of SuperOb: ");
superOb.sum();
subOb.i=7;
subOb.j=8;
subOb.k=9;
System.out.println("Contents of subOb:" );
subOb.showij();
subOb.showk();
System.out.println("Sum of i, j and k in subOb: ");
subOb.sum();
}
}
---------------------------------------------Output..............................................................---------
Contents of SuperOb: i and j1020
Sum of SuperOb:
i and j:30
Contents of subOb:
i and j78
k: 9
Sum of i, j and k in subOb:
i+j+k:24
BUILD SUCCESSFUL (total time: 0 seconds)
--------------------------------------------------------End--------------------------------------------------
Inheritance is the extension of a class. To inherit a class, you use the definition of one class into another by 'extends' keyword. It allows a class to inherit members from another class. However, without any example it is difficult to understand it. So let's start with one of those.
This is the code of inheritance.You can practice with this code.
-----------------------------------------------------------------------------------CODE------------
package javaapplication10;
/**
*
* @author juwel
*/
public class JavaApplication10 {
public static void main(String[] args) {
class A {
int i, j;
void showij(){
System.out.println("i and j" + i + ""+j);
// TODO co // TODO code application logic here
}
void sum(){
System.out.println("i and j:"+(i+j));
}
}
class B extends A{
int k;
void showk(){
System.out.println("k: "+k);
}
void sum(){
System.out.println("i+j+k:"+(i+j+k));
}
}
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.print("Contents of SuperOb: ");
superOb.showij();
System.out.println("Sum of SuperOb: ");
superOb.sum();
subOb.i=7;
subOb.j=8;
subOb.k=9;
System.out.println("Contents of subOb:" );
subOb.showij();
subOb.showk();
System.out.println("Sum of i, j and k in subOb: ");
subOb.sum();
}
}
---------------------------------------------Output..............................................................---------
Contents of SuperOb: i and j1020
Sum of SuperOb:
i and j:30
Contents of subOb:
i and j78
k: 9
Sum of i, j and k in subOb:
i+j+k:24
BUILD SUCCESSFUL (total time: 0 seconds)
--------------------------------------------------------End--------------------------------------------------
No comments:
Post a Comment