Java-Final to Prevent Inheritance

Final to Prevent Inheritance A class that is declared with the final modifier cannot be extended. Sometimes we may like to prevent a class being further subclasses for security reasons. A class that cannot be subclassed is called a final class. Any attempt to inherit these classes will cause an error and the compiler will … Read more…

Java-Instanceof

Instanceof The instanceof is used to knowing the type of an object during run time. The instanceof operator has this general form : objref instanceof type Here, objref is a reference to an instance of a class, and type is a class type. If objref is of the specified type or can be cast into the … Read more…

Java-Applying Runtime Polymorphism

Applying Runtime Polymorphism The following program creates a superclass called Car that stores the speed of a vehicle object. It also defines a method called topSpeed( ) that print the speed of an object. The program derives two subclasses from Car. The first is Ferrari and the second is Ford. Each of these subclass overrides topSpeed( ) so that it print the speed of a … Read more…

Java-Superclass Reference to Subclass Reference

Superclass Reference to Subclass Reference To create a conversion from superclass reference(that pointing to subclass object) to subclass reference, you must use a cast. A cast is simply an explicit type conversion. Program Program Source class Data { int data1; int data2; } class NewData extends Data{ int data3; int data4; } public class Javaapp … Read more…