Posts

Showing posts with the label object oriented programming

Can You Overload or Override Static methods in Java

Can static method be overridden in Java, or can you override and overload static method in Java , is a common Java interview question, mostly asked to 2 years experienced Java programmers. Answer is, No, you can not override static method in Java , though you can declare method with same signature in sub class. It won't be overridden in exact sense, instead that is called method hiding . But at same time, you can overload static methods in Java, there is nothing wrong declaring static methods with same name, but different arguments. Some time interviewer also ask, Why you can not override static methods in Java ? Answer of this question lies on time of resolution. As I said in difference between static and dynamic binding , static method are bonded during compile time using Type of reference variable, and not Object. If you have using IDE like Netbeans and Eclipse, and If you try to access static methods using an object, you will see warnings. As per Java coding convention, static...

Difference between Class and Object in Java and OOPS with Example

Class and Object are two most important concept of Object oriented programming language (OOPS) e.g. Java. Main difference between a Class and an Object in Java i s that class is a blueprint to create different objects of same type . This may looks simple to many of you but if you are beginner or just heard term Object Oriented Programming language it might not be that simple. I have met many students, beginners and programmers who don�t know difference between class and object and often used them interchangeably. Also Java API having classes like java.lang.Object and java.lang.Class also adds more confusion in beginners mind. Both of them are totally different things, class and object in OOPS are concepts and applicable to all Object oriented programming language e.g. C++ or Scala. On the other hand java.lang.Class and java.lang.Object are part of Java API. Along with other OOPS concepts like Abstraction , Encapsulation , Inheritance and Polymorphism , this is also one of the ...

What is Type Casting in Java - Casting one Class to other class or interface Example

Type casting in Java is to cast one type, a class or interface, into another type i.e. another class or interface. Since Java is an Object oriented programming language and supports b oth Inheritance and Polymorphism , It� s easy that Super class reference variable is pointing to SubClass object but the catch here is that there is no way for Java compiler to know that a Superclass variable is pointing to SubClass object. Which means you can not call a method which is declared in the subclass. In order to do that, you first need to cast t he Object back into its original type. This is called type casting in Java . You can type cast both primitive and reference type in Java. The concept of casting will be clearer when you will see an example of type casting in next section. Read more �

What is Object in Java Programming and OOPS - Example Tutorial

Object in Java Object in Java programming language or any other Object oriented programming language like C++ are core of OOPS concept and that's why the name. Class and Object along with Inheritance , Polymorphism , Abstraction and Encapsulation forms basis of any Object oriented programming language e.g. Java. Objects are instances of Class, Class define blue prints and Objects are thing which are created based upon that blueprint. Object are also known as instance in Java, e.g. When we say an instance of String class, we actually mean an Object of String class. Object has state and behavior in Java. State is represented using instance variable and static variable in Java class and behaviors are implemented using methods in Java. What differentiate two Objects of same class are there state e.g. if we take two instance of String class "A" and "B" there contents are different which is there state. In OOPS programming, we model real world things into Class and...

What is Inheritance in Java and OOPS Tutorial - Example

Inheritance in Java is an Object oriented or OOPS concepts , which allows to emulate real world Inheritance behavior, Inheritance allows code reuse in Object oriented programming language e.g. Java. Along with Abstraction , Polymorphism and Encapsulation , Inheritance forms basis of Object oriented programming. Inheritance is implemented using extends keyword in Java and When one Class extends another Class it inherit all non private members including fields and methods. Inheritance in Java can be best understand in terms of Parent and Child class, also known as Super class and Sub class in Java programming language. The class which extends another class becomes Child of the class it extends and inherit all its functionality which is not private or package-private given where Child class is created. Inheritance is the most easy way to reuse already written and tested code but not always best way to do so, which we will see in this article. There are lot of example of Inherita...