package testpk ; public class A { public void method1() { Sysem.out.prinln (“Method 1”); }protected void method2() { System.out.println (“method2”); } }
public class B extends A { public void callingderiveclass () { systen.out.println (“calling public and protected members from derived class”); method1(); method2(); } }
package testpk ; public class C { public void calling_fromindependentclass () { System.out.println (“calling all members from class from same package”); A a1=new A(); a1.method1(); a1.mehod2(); B b1=new B(); b1.method1(); b1.method2(); } }
import testpk.*; class TestingProtectedMembers { public static void main(String args []) { System.out.println (“main start”); B b1=new B(); b1. callingderiveclass (); C c1=new C(); C1. calling_fromindependentclass (); System.out.println (“main end”); } }
9.4 Relationship between Superclasses and Subclasses class Car{ public Car() { System.out.println ("Class Car"); } public void vehicleType () { System.out.println ("Vehicle Type: Car"); } }
class Maruti extends Car { public Maruti () { System.out.println ("Class Maruti "); } public void brand() { System.out.println ("Brand: Maruti "); } public void speed() { System.out.println ("Max: 90Kmph"); } }
public class Maruti800 extends Maruti { public Maruti800() { System.out.println (" Maruti Model: 800"); } public void speed() { System.out.println ("Max: 80Kmph"); } public static void main(String args []) { Maruti800 obj =new Maruti800(); obj.vehicleType (); obj.brand (); obj.speed (); } }
Output: Class Car Class Maruti Maruti Model: 800 Vehicle Type: Car Brand: Maruti Max: 80Kmph
class Shape { public void display() { System.out.println ("Inside display"); } } class Rectangle extends Shape { public void area() { System.out.println ("Inside area"); } }
class Cube extends Rectangle { public void volume() { System.out.println ("Inside volume"); } } public class Tester { public static void main(String[] arguments) { Cube cube = newCube (); cube.display (); cube.area (); cube.volume (); } }
Output: Inside display Inside area Inside volume
System.out.println (“Gross sales must be greater than0.0”);
System.out.println (Gross sales must be greater than0.0); System.out.println (Gross sales must be greater
System.out.println ( }
Constructor in Derived class: class Superclass { int num = 100; } class Subclass extends Superclass { int num = 110; void printNumber () { /* Note that instead of writing num we are * writing super.num in the print statement * this refers to the num variable of Superclass */ System.out.println (super.num); } public static void main(String args []) { Subclass obj = new Subclass(); obj.printNumber (); } } Output: 100
class Parentclass { //no- arg constructor Parentclass () { System.out.println ("no- arg constructor of parent class"); } // arg or parameterized constructor Parentclass (String str ) { System.out.println ("parameterized constructor of parent class"); } } class Subclass extends Parentclass { Subclass() { /* super() must be added to the first statement of constructor * otherwise you will get a compilation error. Another important * point to note is that when we explicitly use super in constructor * the compiler doesn't invoke the parent constructor automatically. */ super(" Hahaha "); System.out.println ("Constructor of child class"); } void display(){ System.out.println ("Hello"); } public static void main(String args []) { Subclass obj = new Subclass(); obj.display (); } } Output: parameterized constructor of parent class Constructor of child class Hello
Software Engineering with Inheritance We can modify the new class to meet our requirements by including additional members and by overriding super class members. Doing this, does not require the sub class programmer to change the super class’s source code. Java only requires access to super class’s. C lass file so it can compile and execute any program that uses or extends the super class. This powerful capability is attractive to independent s/w vendors. Who can develop proprietary classes for sell or license and make them available to user in bytecode format. User then can drive new classes from this library classes rapidly . without accessing the source code.
Class java.lang.Object Or Cosmic Super class:Object Or java is a Single rooted Hierarchy Java provides a library class Object in java. Lang package. This class is directly or indirectly super class of every java class. This means, Whether we extend it or not the class object is default super class of every java class the same is applied for every library class. Hence every class library derived from class object and hence java is a single rooted hierarchy and hence this class object is called as cosmic super class.
Class TestObjectClass { Public void receiveAnyObject (Object ref) { ------ ------ } } Above method receiveAnyOject () can receive any java class object passed as parameter to it. This is because a super class reference can refer/point to object of it sub class. This class defines total 11 methods and all these methods are available to all java classes.