![]() ![]() Let us think about the generic makeCall() method in a Samsung mobile which is inherited in a Samsung S5.Įncapsulation – Encapsulation is defined as the process of protecting information details from the outside world. This will help us to reuse the functionality which is defined before. Inheritance – Inheritance is the ability to extend the functionality and information from a base class to a new class. It is very important to recognize the difference. New Mobile() is the actual object while mbl1 is a reference variable pointing to the actual object. Object – Mobile companies create new phones every day, they can be treated as Objects. Using the example of mobile, a mobile class can be designed using IMEI Number, Software, Storage as states and makeCall(), receiveMessage(), clickPhoto() as behaviors. A class consists of state (variables) and behaviors (methods). They are:Ĭlass - A Class is a plan which describes the object. There are 4 main features of OO that JAVA uses. What are the different object-oriented features of JAVA? Explain with real life examples. Normally we use private constructors in the following cases: The reason we need it is to prevent the instantiation of that class (which has a private constructor) outside of the class. But when an actual object is created, it depends on how many arguments are passed, to determine which constructor will be used. Reproduced courtesy of Bruce Eckel, MindView, Inc.OverloadDemo class has 3 constructors defined. As you’ll see in Chapter 9, this also prevents a derived-class constructor from catching any exceptions that come from a base class. This means nothing else can appear before it. (The compiler will remind you if you get it wrong.)Īs just noted, the compiler forces you to place the base-class constructor call first in the body of the derived-class constructor. In addition, the call to the base-class constructor must be the first thing you do in the derived-class constructor. If you don’t call the base-class constructor in BoardGame( ), the compiler will complain that it can’t find a constructor of the form Game( ). If your class doesn’t have default arguments, or if you want to call a base-class constructor that has an argument, you must explicitly write the calls to the base-class constructor using the super keyword and the appropriate argument list: //: c06:Chess.java // Inheritance, constructors and arguments. It’s easy for the compiler to call these because there’s no question about what arguments to pass. ![]() The preceding example has default constructors that is, they don’t have any arguments. Even if you don’t create a constructor for Cartoon( ), the compiler will synthesize a default constructor for you that calls the base class constructor. You can see that the construction happens from the base “outward,” so the base class is initialized before the derived-class constructors can access it. Private static Test monitor = new Test() : c06:Cartoon.java // Constructor calls during inheritance. The following example shows this working with three levels of inheritance: Java automatically inserts calls to the base-class constructor in the derived-class constructor. Of course, it’s essential that the base-class subobject be initialized correctly, and there’s only one way to guarantee this: perform the initialization in the constructor by calling the base-class constructor, which has all the appropriate knowledge and privileges to perform the base-class initialization. It’s just that from the outside, the subobject of the base class is wrapped within the derived-class object. This subobject is the same as if you had created an object of the base class by itself. When you create an object of the derived class, it contains within it a subobject of the base class. But inheritance doesn’t just copy the interface of the base class. From the outside, it looks like the new class has the same interface as the base class and maybe some additional methods and fields. Since there are now two classes involved-the base class and the derived class-instead of just one, it can be a bit confusing to try to imagine the resulting object produced by a derived class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |