Posts

Polymorphism

Image
  Polymorphism After overriding a super class method in a sub class, casting and running the super class method through the super class reference is polymorphism. Simply put, the process of calling the sub class method from the super class reference using the 3 concepts of overriding, inheritance, and casting can be described as polymorphism. Figure 1 : Polymorphism Example As shown in this simple example, the subclass method is called by both upcasting and downcasting. Figure 2 : Polymorphism Example This code creates an " Employee " superclass and two subclasses, " Manager " and " Programmer ". The " Employee " superclass has a single method called " work " that simply prints out "The employee works". Both " Manager " and " Programmer " are subclasses of " Employee " and they override the " work " method with their respective tasks. The " Manager " subclass has an additional

Casting

Image
Casting Upcasting In upcasting, we cannot access from bottom level to top level, but we can access from top level to bottom level. Upcasting is used to convert an object, method or variable of the super class to an object, method or variable of the sub class. Upcasting simply means giving a sub class object to a super class variable. Figure 1: Up casting Example In inheritance, the things in the super class are also owned by the sub class. Therefore, the constructor of the super class also runs in the sub class. So the sub class can access the constructor of the super class. Also, the constructor is called when the object is created. Therefore, when the object of the super class is created, the constructor of the super class is called. Since the sub class can access the constructor of the super class, the sub class can be accessed at the time of creating the object of the super class. Downcasting Turning the sub class object into a super class variable back into a sub class object is d

Super Keyword

Image
  Super Keyword The super keyword is used to call the constructor of the super class from a sub class. The super keyword always calls the super class. All the methods and variables of the super class can be accessed from the sub class using the super keyword. Figure 1: Super keyword   Example As Figure 1 shows, the super keyword can be used to retrieve the output of all methods or only one method from the super class. When the super keyword is called to the constructor, the object is created for it and the corresponding output is executed.

Method Overriding

Image
Method Overriding Changing the body that inherits from a super class to a sub class happens in Method Overriding.  Only the method related to the created object is always executed.  When the object is created, the object inherits the same method from the superclass but gives a different output than the output given by inheritance. Figure 1: Method Overriding  Example As shown in Figure 1, object "M" is created for class "Man". Therefore, when the method called "climb" is called, only the method of the "Man" class will be executed. Therefore, the output is "using ladder". Method Overloading vs Method Overriding High school students are taught by teachers in a professional manner, but Kindergarten students are taught by teachers in a different way. Therefore, the same action "teach" will have different behaviors in different situations with different inputs. The same will happen in "Method Overloading". Simply put, a c

Inheritance

Image
  Inheritance Figure 1:  Inheritance example There is a company called "A". Some of the features and functions present in its older car models have been inherited by the new car models currently in the market. Both the car models are different but the new car model has some features and functions which are included in the old car model. Therefore, the new car model inherits the features and functions of the old car model. That is an example for inheritance. Inheriting things that belonged to one class to another class is called inheritance in Java.  The first class is called super class (parent) and the new class is called sub class (child). To inherit , there must be a relationship between the two classes. For that, Java uses the keyword "extends" with super class and sub class to make.   Also, a class can inherit many classes. Figure 2:  Inheritance Java example

Method Overloading

Image
Method Overloading Figure 1 : Method Overloading The concept of method overloading is used when creating the same method repeatedly in the same class. This can be done by changing the parameter, which is called the method signature. Figure 2 : Method Overloading Example Access Modifiers The security which is used to access classes, methods and variables, is called modifiers. there are 4 types of modifiers. Public Project, class in the project and other related classes can access. Protected Class, package and sub classes can access. Default Class and package can access. Private Only the class can access. Static Static is a keyword that cannot be changed but used as a modifier. Variables, methods, classes in a class can be accessed from anywhere in it using the "static" keyword. Figure 3 : static keyword Example

Parameters, Arguments & Constructors

Image
Parameters, Arguments & Constructors Generally Java program is executed line by line. Therefore, in order to run any code, if there are other codes required for that, they should be mentioned beforehand. Generally Java program is executed line by line. Therefore, in order to run any code, if there are other codes required for that, they should be mentioned beforehand. Also, there are 4 essential elements to run a Java code. They are,  Class Constructor Main method Object Figure 1:  Simple java code Parameters & Arguments A method requires 2 main elements. They are, return type and method name with a parameter list. The return type is used to return the output values and has 2 types. Void is Java's default return type. It can be used with any data type. String or any other data type can also be used as a return type with the "return" keyword. The "return" keyword is used to indicate that the return type of the code is a type. It can only return values of