Java For Android Tutorial #18: Inheritance
Code:
Inheritance provides important insights into the modular approach to programming. In Android programming, we do things by inheriting properties of activities, services and fragments. This increase the re-usability of the code as well as increase the simplicity of the code. In this program, I have used 3 classes: parent, child and one main class. After creating the instance, I am able to access everything from the parent class (as the method and variable are public and protected). In the end, I have made the method private and shown that parent private methods can't be accessed by the child class.
package com.example;
public class java {
public static void main(String[] str) {
Child oChild = new Child();
oChild.show();
System.out.println(oChild.dumy);
}
}
class Parent{
String dumy = "Hello";
public void show(){
System.out.println("Message from the parent class");
}
}
class Child extends Parent{
}Inheritance provides important insights into the modular approach to programming. In Android programming, we do things by inheriting properties of activities, services and fragments. This increase the re-usability of the code as well as increase the simplicity of the code. In this program, I have used 3 classes: parent, child and one main class. After creating the instance, I am able to access everything from the parent class (as the method and variable are public and protected). In the end, I have made the method private and shown that parent private methods can't be accessed by the child class.
Comments
Post a Comment