by Dinesh Thakur Category: Classes

If you omit implementation of a constructor within your class, a constructor added by the Java compiler, each class must have a constructor and the one used is called the default constructor. You won't see it listed anywhere in your Java code as the compiler adds at compilation time into the .class file. Should you implement a constructor in your code, the compiler not add the default one. It does not contain any parameters nor does it contain any statements in its body. Its only purpose is to enable you to create an object of class type. The default constructor looks like

 
by Dinesh Thakur Category: Classes

It is possible to have static methods in a class in the same way as we have static fields. The static method is similar to instance method) of a class but the only difference is that the static method can be called through its class name without creating any object of that class. A static method is also called class method as it is associated with a class and not with individual instance of the class. We have already seen the sqrt () method which is a static method within Java standard Math class. A method is declared static when its behavior is not dependent on the instance variable just like Math.sqrt (4.5).This method never uses instance variables so it does not need to know about the specific object.

 
by Dinesh Thakur Category: Classes

In each object of a class will have its own copy of all the fields of the class. However, in certain situations, it may be required to share a common copy of fields among all the objects of the same class. This is accomplished by declaring the field(s) to be static and such fields are known as static field(s). If a field is declared static then there is one field for the entire class instead of one per object. A static field of a class is often referred to as a class variable because static field is associated with a class and not with individual instances of the class. A static field gets memory only once for the whole class no matter how many objects of a class are created. To declare a static field, prefix the field declaration in the class with the static modifier. Its syntax is,

 
by Dinesh Thakur Category: Classes

In this example, we compute the electricity bill of a particular customer. The class ElectricityBil1 contains field customerNo, name and units. It also contains methods setData () show () and billcalculate ().The statement,

 
by Dinesh Thakur Category: Classes

This example is used to calculate the area and circumference of a circle with given radius. The class Circle contains the field radius and methods setData (), area ()and circumference ().The statement.

 
by Dinesh Thakur Category: Classes

When you compile this program, you will find that two class files have been created, one for Rectangle and one for RectangleArea. The Java compiler automatically puts each class into its own class file.

 
by Dinesh Thakur Category: Classes

Method Overloading: When multiple methods in the same class with same name, having different functions or types of parameters, it is known as Method Overloading. When an overloaded method is invoked, it is the responsibility of the compiler to select the appropriate overloaded method based on the number of argument(s) passed and if the numbers of argument(s) are same then depending upon the type of argument(s) passed to the method. Thus, the key to method overloading is a method's parameter list. A method's return type is not enough to distinguish between two overloaded methods. If the compiler detects two methods declarations with the same name and parameter list but different return types then it will generate an error.

 
by Dinesh Thakur Category: Classes

In Java, there is no such thing as a call by reference, only a call by value. If a method that passes value called, we are calling by value and any change in the method called do not get affected in the method used to call.  Where a call by value is used, there is no change to the original value. Have a look  at the Example below:

 
by Dinesh Thakur Category: Classes

In java, base class reference can be assigned objects of sub class. When methods of sub class object are called through base class’s reference.



 
by Dinesh Thakur Category: Classes

Interfaces and abstract classes both implements polymorphic behaviour and seem to be similar but they are different in the following ways:
1. An interface is purely abstract i.e. methods in an interface only have declarations no implementations. On the other hand, abstract class methods may or may not have an implementation.

 
by Dinesh Thakur Category: Classes

Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.



 
by Dinesh Thakur Category: Classes

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.

 
by Dinesh Thakur Category: Classes

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

 

Page 3 of 3



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles