Skip to main content

Posts

Inheritance

Inheritance Inheritance in java   is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also. Inheritance represents the   IS-A relationship , also known as   parent-child   relationship. Why use inheritance in java Ø   For Method Overriding (so runtime polymorphism can be achieved). Ø   For Code Reusability. Syntax of Java Inheritance class Subclass-name extends Superclass-name   {      //methods and fields   }   The   extends keyword   indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. In the terminology of Java, a class which is inherited is called parent or super class and the new class

Polymorphism

Polymorphism Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Polymorphism is derived from 2 greek words:   poly   and   morphs . The word "poly" means many and "morphs" means forms. So polymorphism means many forms. Real life example of polymorphism Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors. How to achieve Polymorphism in Java ? In java programming the Polymorphism principal is implemented with method overriding concept of java. Polymorphism principal is divided into two sub principal they are: Ø   Static or Compile time polymorphism Ø   Dynamic or Runtime polymorphism Let us consider the f

Encapsulation and Abstraction

Encapsulation in Java  The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. That’s why encapsulation is known as   data hiding.  Abstracton in java   Abstraction   is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send. Likewise in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does

Object and Class in JAVA

Object and Class in JAVA Java is an Object-Oriented Language. Object in Java An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of intangible object is banking system. An object has three characteristics: Ø   state:   represents data (value) of an object. Ø   behavior:   represents the behavior (functionality) of an object such as deposit, withdraw etc. Ø   identity:   Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely. For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior. Object is an instance of a class.   Class is a template or blueprint from which objects are created. So object is the instance(result) of a class. Ob

JAVA Strings Methods

JAVA Strings Methods   Ø   Java and String   Ø   Upper and LowerCase   Ø   The compare Method   Ø   The indexOf Method   Ø   The substring Method   Ø   The equals Method   Ø   The charAt Method   Ø   The replace Method Java and String There's more to strings than meets the eye. Unlike int variables, or double variables, strings are objects. What this means in practice is that you can do things with strings of text that you can't do with int or double variables. (The same applies to the primitive data types boolean, byte, single, char, float, long and short: they are not objects like strings are.) Before we get to manipulating strings of text, here's some basic information on what strings actually are. How Java Stores Strings A string is a series of Unicode characters held under a variable name. Take the following string: String someText = "Bill"; This tells Java to set up a string object with the four characters "B&qu