Showing posts with label object oriented programming. Show all posts
Showing posts with label object oriented programming. Show all posts

Wednesday 28 August 2013

Top Most 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 is 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, Inheritanceand Polymorphism, this is also one of the most fundamental of Object oriented programming (OOPS) which needs to be clearly understood before proceeding into serious application programming. Without clear understanding of Class and Object you are more prone to make errors, not able to comprehend an already written program and it would be pretty hard for you to find bugs or fix errors or exceptions in Java code. By the way difference between class and object is also a popular programming interview question, which is quite frequently asked at fresher level interviews.  In this article we will look this on different angles to differentiate Class and object in Java. Once you get hold of key OOPS concepts, I would recommend to read 10 OOPS and SOLID design principles for Java programmer, which is tried and tested design pattern for writing better code.
Read more »

Monday 26 August 2013

Top Most 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 both Inheritance and Polymorphism, It’s easy that Super class reference variable is pointing to Sub Class object but catch here is that there is no way for Java compiler to know that a Super class variable is pointing to Sub Class object. Which means you can not call method which is declared on sub class. In order to do that, you first need to cast the Object back into its original type. This is called type-casting in Java. This will be more clear when we see an example of type casting in next section. Type casting comes with risk of ClassCastException in Java, which is quite common with method which accept Object type and later type cast into more specific type. we will see when ClassCastException comes during type casting and How to avoid it in coming section of  this article. Another worth noting point here is that from Java 5 onwards you can use Generics to write type-safe codeto reduce amount of type casting in Java which also reduces risk of java.lang.ClassCastException at runtime.
Read more »

Monday 19 August 2013

Top Most What is polymorphism in Java? Method overloading or overriding?

What is Polymorphism in Java
Polymorphism is an important Object oriented concept and widely used in Java and other programming language.  Polymorphism in java is supported along with other concept like Abstraction, Encapsulation and Inheritance. Few words on historical side; Polymorphism word comes from ancient Greek where poly means many so polymorphic are something which can take many form. In this Java Polymorphism tutorial we will see what is Polymorphism in Java , How Polymorphism is implemented in Java e.g method overloading and overriding, why should we use Polymorphism and how can we take advantage of polymorphism while writing code in Java. Along the way we will also see a real world example of using Polymorphism in Java
Read more »

Sunday 18 August 2013

Top Most Can we Overload and Override Static methods in Java - Interview Question

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 methods should be accessed by class name rather than object. In short Static method can be overloaded, but can not be overridden in Java. If you declare,  another static method with same signature in derived class than static method of super class will be hidden, and any call to that static method in sub class will go to static method declared in that class itself. This is known as method hiding in Java.
Read more »

Friday 16 August 2013

Top Most 10 Abstract Class and Interface Interview Questions Answers in Java

Abstract class and interface is very popular in any object oriented programming language or Java interview, and there are always one or more questions from this. Interface is more common, because of its popularity among designers but questions from abstract class also pops up now and than. Interview questions from abstract class is more common on junior level or you say under 2 years experience of  Java programmers, while interface related questions are mostly asked on senior level Java interview e.g. 4 or 6 years of experience. They are mostly asked along with other Java design pattern questions, e.g. Decorator pattern or Factory pattern . Any way, in this article we will see mix of these interview questions from abstract class and interface. All questions has been asked in various Java interviews and difficulty level for these question is easy for most of Java developer. It’s mostly fact based questions, but some questions like difference between abstract class and interface in Java, and when to prefer abstract class over interfacecan be really tricky.
Read more »

Wednesday 14 August 2013

Top Most Difference between Abstract class vs Interface in Java and When to use them

When to use interface and abstract class is one of the most popular object oriented design questions and almost always asked in Java, C# and C++ interviews. In this article, we will mostly talk in context of Java programming language, but it equally applies to other languages as well. Question usually starts with difference between abstract class and interface in Java, which is rather easy to answer, especially if you are familiar with syntax of Java interface and abstract class.Things start getting difficult when interviewer ask about when to use abstract class and interface in Java, which is mostly based upon solid understanding of popular OOPS concept like Polymorphism, Encapsulation, Abstraction, Inheritance and Composition. Many programmer fumbles here, which is natural because most of them haven't gone through real system design process and haven’t seen the impact of choosing one over other. Repercussion of design decisions are best known during maintenance phase, a good design allows seamless evolution while maintaining a fragile design is nightmare. As I have said previously, some time object oriented design interview questions also helps to understand a topic better, but only if you are willing to do some research and not just mugging the answer. Questions like when to use abstract class and interface falls under same category. In order to best understand this topic, you need to work out some scenarios, examples etc. It's best to get this kind of knowledge as part of your work but even if you don't get there, you can supplement them by reading some good books like Head First design pattern and doing some object-oriented software design exercises. In this article, we will learn difference between abstract class and interface in Java programming language and based upon our understanding of those differences, we will try to find out some tips and guidelines to decide when its better to use abstract class over interface or vice-versa.
Read more »

Tuesday 6 August 2013

Top Most What is Object in Java or 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. Classand Object along with Inheritance, Polymorphism, Abstractionand Encapsulationforms 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 behaviour 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 Object e.g. Car class is a blue print of car which may specify that car should have 4 gears, 4 seats an engine etc and every single car is an Object or instance of Car class. If you like to learn more on OOPS concepts and designs, I suggest to read 10 OOPS and SOLID design principles in Java, That will also help to improve your OOPS programming.
Read more »

Top Most 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, Polymorphismand 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 classwhich 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 Inheritance in Object oriented world e.g. Child inherit properties from parent, Dog inherit properties of Animal etc. In this Java and OOPS tutorial we will learn What is Inheritance in Java, How to use Inheritance in Java, Simple Example of Java Inheritance and some important points about Inheritance in Java.
Read more »

Friday 2 August 2013

Top Most Fundamentals of Object Oriented Programming in Java

I remember when I was doing my engineering , OOPS concept was in first semester. Back then it looks like some alien things to me, because I have never done programming and even the smallest program we get to write e.g. checking whether number is palindrome or not , or printing Fibonacci series , we never really needed to use OOPS. When needed, I just memorize things and written down in exam :). That worked, I passed the exam but effectively I didn't learn OOPS or Object Oriented Programming on those days.

It's when, I started learning java (an almost truly object oriented programming language) and introduced to concept like abstract classes, interface, and method overriding; some thing started to going in my mind. This blog post is very basic in nature, but I really hope it could help beginners on understanding things like class, objects, abstraction , polymorphism, inheritance ,encapsulation , composition , cohesion , coupling , interface programming etc.
Read more »

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt