Java-Simple Inheritance

Simple Inheritance Reusability is yet another aspect of object oriented programming. It is always nice if we could reuse something that already exists rather than creating the same all over again. Java supports this concept. Java classes can be reused in several ways. This is basically done by creating new classes, reusing the properties of … Read more…

Java-Superclass with Private Variables

Superclass with Private Variables Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private. When a members of a class is specified as private, then that member can only be accessed by other members of its class. This is the … Read more…

Java-Constructor Execution

Constructor Execution A super class constructor should be executed before a subclass constructor is executed. Only then, an object of a subclass will be properly initialized. Program Program Source class Super { Super() { System.out.println(“Super class constructor”); } } class Sub extends Super{ Sub() { System.out.println(“Sub class constructor”); } } public class Javaapp { public … Read more…

Java-Super Keyword

Super Keyword In some cases, a variable in a subclass may have the same name as a super class variable. In such a case, the newly defined subclass variable hide the super class variable. In the following program, the super class Data has a variable named data1 and its value is 10. The subclass NewData … Read more…

Java-Super to Call Superclass Method

Program Program Source class Super { void print() { System.out.println(“SuperPrint”); } } class Sub extends Super{ void print() { super.print(); System.out.println(“SubPrint”); } } public class Javaapp { public static void main(String[] args) { Sub obj = new Sub(); obj.print(); } }

Java-Super to Call Superclass Constructor

Super to Call Superclass Constructor We shall execute the constructor of a super class by using the super keyword in a simple command of the following form : Here, the arguments represent an optional list of arguments for the constructor of the super class. The above statement should appear as the very first statement of … Read more…