In the main() class we have created an object of the car class and then assign it to the reference variable of the vehicle class.Įven though we have assigned a Car object to a Vehicle reference variable, the drive() method of the Car class is invoked because the Car class overrides the drive() method of the Vehicle class. The Car class extends the Vehicle class and overrides the drive() method. In the above example we have a parent class Vehicle and a child class with the name Car. Output Driving a car… when a reference variable of a superclass is assigned to a reference variable of The syntax of upcasting in java is given below: Parent p= new Child() Upcasting can also be referred to as Widening and Generalization. We can only access some specified methods and variables of the child class. But in upcasting we cannot access all the variables and methods of the child class. The upcasting gives us the benefit of accessing all the methods and variables of the parent class. The upcasting is done implicitly there is no need to explicitly refer to the parent class. In simple words upcasting in java is a type of object typecasting where we are typecasting a child object to a parent object. There are two types of object typecasting available in java and they are mentioned below: You can refer to Typecasting in Java for further knowledge on typecasting. Like the data type, we can also typecast the objects. As it deals with the conversion of one data type into another data type explicitly or implicitly. It is one of the most important concepts of java. The process of converting one data type into another is known as typecasting in java. While moving further in this article we will learn all about upcasting and downcasting in java along with why we use them, examples of both and followed by applications of them. Upcasting and downcasting in java are important aspects of inheritance. Among the 4 pillars of oops inheritance is one of the most used ones which allows the child classes to inherit the methods and attributes of the parent class. Java is one of the languages that support OOPs. There are many programming languages available some of them support object-oriented programming whereas some of them do not.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |