Basics of Object Oriented Programming
Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class - A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
Computer Programming is a process of designing, writing, testing, debugging and maintaining the source code written in a programming language to solve a real life problem.This Process requires knowledge of application domain (task), formal logic for solution and knowledge of syntax of the programming language, in which source code to be written.
There are two main approaches (methodologies) of programming
Procedural Programming : In this approach, a Programming task is broken into smaller and easily manageable modules (procedures). The stress is given to functionalities rather than data. Basic, COBOL, Pascal and C Languages supports Procedural Programming approach.
Object Oriented Programming: In this approach, a programming task is broken into Objects, which contains data (properties) and its operating methods (behaviours).
The main focus is given on Data rater than functions for better security and portability of data. C++, Java and other modern languages follows this methodology of programming.
Object Oriented Programming (OOP) In general, Object Oriented Programming (OOP) refers ‘Programming with objects’ , in which programming task is broken into objects. The main features of OOP are:-
Encapsulation: It refers the binding of Data and its associated Methods together in a single unit which is called Object.
Polymorphism: A method can perform multiple functionalities (behaviour) depending on the input data.
It is performed by Method Overloading and Operator Overloading.
Inheritance: The process of deriving a new class (sub class) from existing classes (super class) is called Inheritance. The newly created class may contains properties and behaviour of its parent.
Components of Object Oriented Programming are as follows:
Class
Object
Data Members & Methods
Access Specifier and Visibility Modes Classes & Objects:
The basic unit of OOP is the Class.
A class represents a set of similar objects. It can be described as a blue print of Objects.
In other words, an Object is an instance of a class that holds actual data in memory.
JAVA is a pure Object Oriented Programming language, since each program in Java must contain at least one class definition.
A class in OOP is a template for objects. In general, a class is a specification of the data and the functions to be encapsulated with data.A class is composed by a set of Attributes (Properties) and Behavior.
Properties are represented by Data Members and Behavior is simulated by Method Members.
An Object is an entity having a unique Identity, characteristics (Properties) and Behavior (Methods). JAVA is enriched with various ready-to-use class definitions, which are used in the Application. Swing Controls are collection of such of classes.Objects in the real world can be represented by objects in the program.Each object contains data and code to manipulate data. For example, jButton control belongs to JButton Class of Swing Control Package. Each time, When you drag JButton on the Frame, an instance (Object) like jButton1, jButton2 etc. is created. Each object (jButton..) offers .setText(), .getText() etc. methods, which handles functionalities of the button.
A class itself does not occupy any space; it defines a blue-print for the objects only. When object is instantiated then it occupies space in the memory. In general, class works as user defined data type which encapsulates its data and methods. Let us a simple example to create a class City having name and Population data member and display() method to display its details when invoked.
// define a class public class city { String name; int population; void display() { System.out.println(“City Name: ”+name); System.out.println(“Population: ”+population); } } // create an object and access it private void jButton1ActionPerformed(…) { ……………. city x= new city(); // creates an object x of class city x.name=“Jaipur”; // Accessing data member of x x.population = 100000; System.out.println(“City Details”+’\n’); x.display(); // Accessing Method Member of x } Simply, If a class Students has been defined, you can create an object x which is Student type, as followsStudent x = new student(); In java a new operator is used to create an object. The new operator followed by a call to the constructor method (method having same name as class) is used to initialize and object.
Class & Sub- Class : In JAVA a new class (Sub-class) can derived from existing class (Super-class). A derived class may inherit all the data and method members from its parent. This principle is known as Inheritance. A class from which another class is inheriting its properties iscalled base class or Super Class and the class which inheriting properties is known as a sub class or derived class. E.g. If human class is defined, we can derivestudent class by inheriting all the members of human class, since students are human beings. The class Human is called Base class and Student is called Sub Class.
Local variables − Variables defined inside methods or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Package in Java: Class and its derived classes can be stored in a single unit for proper management. A group or collection of classes is called package in Java. Each package may contain various class definitions and is stored in a folder. Java offers some ready-to-use packages as extensive library of pre-written classes like java.io , java.lang , and java.net etc. We can write our own packages also.
We can use a package in a program by using import statement at top of the program.
import javax.swing.JOptionPane;
import mypackage.*;
//to import all members// import mypackage.myclass; // to import selected class// The java.lang package is imported by default i.e. no import statement is required.
For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io −
Concept of Inheritance:
In OOPs, Inheritance is the process of creating new class (Derived Class or sub-classes) from existing class (Base Class or Super class).
A Sub-class inherits all the properties (data members) and behaviour (method members) of the parent class (Super-class).
The level of Inheritance may be extended i.e. A Sub-class may have their own sub-classes.
In Real-life most of the things exhibit Inheritance relationship.
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
Syntax
Why Inheritance ?
Modelling of Real-world: By Inheritance, we can model the real-world inheritance relationships in easy way.
Reusability of codes: Inheritance allows the derivation of a new class from existing classes. We can add new features to derived class to make a new one with minimal efforts.
Transitive nature of Inheritance: If we modify the base class then the changes automatically inherit into derived classes. Since inheritance is transitive in nature. This offers faster and efficient way to maintain the large program.
Types of Inheritance:
In OOP approach, the Inheritance is many types.
Single Level Inheritance: When a sub-class inherits only one base-class.
Multi-level Inheritance: When a sub-class is derived from sub-class of another base-class.
Multiple Inheritance: When a sub-class derived from multiple base-classes.
Hierarchical Inheritance: When Multiple sub-classes are derived from a single base class.
Hybrid Inheritance: When a sub-class inherits from multiple base-classes and all its base classes inherits one or more super-classes.
A very important fact to remember is that Java does not support
multiple inheritance. This means that a class cannot extend more than
one class.
Access Specifiers: In General, when a Sub-Class is created from a base Class, then all data members and method members of Parent (base) class are inherited in the child class and hence they can be accessed in child class because they are visible. But some time, it is required to restrict some methods from being inherited in child class to maintain the privacy and security. Java provides some Access Specifiers, which can control the visibility/access of the Parent (base) Class from Child (sub) class. The members (Data and Methods) of the Parent Class may be defined as Private, Public, Protected, and Default, which may limit its accessibility or visibility in the derived (child) classes.
Private : Members are accessible only inside their own class and no where else.
Protected: Members are accessible in own class, all classes within package and all Sub-classes in different package.
Public: Members are accessible everywhere in all the classes. Package (default): Members without any specifier assumed package level scope i.e. accessible to all classes inside the package only.
Abstract Class: An Abstract class simply represents a concept for its sub-classes. An Abstract class works as template for its sub-classes which contains only data members and method prototype i.e. methods without code (concept). Sometimes, we need to define a super-class having general characteristics (data) and behaviour (generic methods) for its sub-classes.
A Sub-class may re-define the methods or overridden to perform a task since super-class contains only the prototype (method with empty body). Abstract classes are normally used as base class in inheritance for which no object is required
e.g. JOptionPane Class in Java is Abstract class because it requires no object.
Whereas JTextField, JLabel classes etc. are called Concrete classes because they requires an object like ‘jTextField1’ for using them.
Role of Final keywords in inheritance: The final keyword can be used with- Variable, Methods and Class names. The effect of final keywords is as follows.
final variables works as constant i.e. the value of a final variables can’t be changed. final methods can’t be overridden by sub- class.
final class can’t be extended. Concept of Polymorphism: In Simple term, Polymorphism means Multiple forms of behaviour. For example, a person may exhibit different behaviour in different places or situation.
In Object Oriented Programming, a Method or Operator may exhibit different behaviour for different sets of input given.
For example the ‘+’ operator in Java gives different result for different input (integer and string) 2+3 gives 5 but “Hello”+”Java” gives “HelloJava”
Here, + operators exhibits different behaviour for numbers and string values i.e. + operator is overloaded.
Same as Math.round() function exhibit different behaviour for float and double type values i.e. Method Overloading.
Polymorphism is implemented as Method Overloading and Operator Overloading i.e. overloaded with different functionalities. Polymorphism makes your program code compact, smarter and faster.
Objects in Java Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior. If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running. If you compare the software object with a real-world object, they have very similar characteristics. Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods. So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
Classes in Java A class is a blueprint from which individual objects are created. Following is a sample of a class. public class Dog{ String breed; int ageC String color; void barking(){ } void hungry(){ } void sleeping(){ } }
Java Package In simple words, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier.
Import Statements In Java if a fully qualified name, which includes the package and the class name is given, then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class. For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io: import java.io.*;
Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class - A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
Computer Programming is a process of designing, writing, testing, debugging and maintaining the source code written in a programming language to solve a real life problem.This Process requires knowledge of application domain (task), formal logic for solution and knowledge of syntax of the programming language, in which source code to be written.
There are two main approaches (methodologies) of programming
Procedural Programming : In this approach, a Programming task is broken into smaller and easily manageable modules (procedures). The stress is given to functionalities rather than data. Basic, COBOL, Pascal and C Languages supports Procedural Programming approach.
Object Oriented Programming: In this approach, a programming task is broken into Objects, which contains data (properties) and its operating methods (behaviours).
The main focus is given on Data rater than functions for better security and portability of data. C++, Java and other modern languages follows this methodology of programming.
Object Oriented Programming (OOP) In general, Object Oriented Programming (OOP) refers ‘Programming with objects’ , in which programming task is broken into objects. The main features of OOP are:-
Encapsulation: It refers the binding of Data and its associated Methods together in a single unit which is called Object.
Polymorphism: A method can perform multiple functionalities (behaviour) depending on the input data.
It is performed by Method Overloading and Operator Overloading.
Inheritance: The process of deriving a new class (sub class) from existing classes (super class) is called Inheritance. The newly created class may contains properties and behaviour of its parent.
Components of Object Oriented Programming are as follows:
Class
Object
Data Members & Methods
Access Specifier and Visibility Modes Classes & Objects:
The basic unit of OOP is the Class.
A class represents a set of similar objects. It can be described as a blue print of Objects.
In other words, an Object is an instance of a class that holds actual data in memory.
JAVA is a pure Object Oriented Programming language, since each program in Java must contain at least one class definition.
A class in OOP is a template for objects. In general, a class is a specification of the data and the functions to be encapsulated with data.A class is composed by a set of Attributes (Properties) and Behavior.
Properties are represented by Data Members and Behavior is simulated by Method Members.
An Object is an entity having a unique Identity, characteristics (Properties) and Behavior (Methods). JAVA is enriched with various ready-to-use class definitions, which are used in the Application. Swing Controls are collection of such of classes.Objects in the real world can be represented by objects in the program.Each object contains data and code to manipulate data. For example, jButton control belongs to JButton Class of Swing Control Package. Each time, When you drag JButton on the Frame, an instance (Object) like jButton1, jButton2 etc. is created. Each object (jButton..) offers .setText(), .getText() etc. methods, which handles functionalities of the button.
A class itself does not occupy any space; it defines a blue-print for the objects only. When object is instantiated then it occupies space in the memory. In general, class works as user defined data type which encapsulates its data and methods. Let us a simple example to create a class City having name and Population data member and display() method to display its details when invoked.
// define a class public class city { String name; int population; void display() { System.out.println(“City Name: ”+name); System.out.println(“Population: ”+population); } } // create an object and access it private void jButton1ActionPerformed(…) { ……………. city x= new city(); // creates an object x of class city x.name=“Jaipur”; // Accessing data member of x x.population = 100000; System.out.println(“City Details”+’\n’); x.display(); // Accessing Method Member of x } Simply, If a class Students has been defined, you can create an object x which is Student type, as followsStudent x = new student(); In java a new operator is used to create an object. The new operator followed by a call to the constructor method (method having same name as class) is used to initialize and object.
Class & Sub- Class : In JAVA a new class (Sub-class) can derived from existing class (Super-class). A derived class may inherit all the data and method members from its parent. This principle is known as Inheritance. A class from which another class is inheriting its properties iscalled base class or Super Class and the class which inheriting properties is known as a sub class or derived class. E.g. If human class is defined, we can derivestudent class by inheriting all the members of human class, since students are human beings. The class Human is called Base class and Student is called Sub Class.
Local variables − Variables defined inside methods or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Package in Java: Class and its derived classes can be stored in a single unit for proper management. A group or collection of classes is called package in Java. Each package may contain various class definitions and is stored in a folder. Java offers some ready-to-use packages as extensive library of pre-written classes like java.io , java.lang , and java.net etc. We can write our own packages also.
We can use a package in a program by using import statement at top of the program.
import javax.swing.JOptionPane;
import mypackage.*;
//to import all members// import mypackage.myclass; // to import selected class// The java.lang package is imported by default i.e. no import statement is required.
Import Statements
In Java if a fully qualified name, which includes the package and the class name is given, then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io −
import java.io.*;
Concept of Inheritance:
In OOPs, Inheritance is the process of creating new class (Derived Class or sub-classes) from existing class (Base Class or Super class).
A Sub-class inherits all the properties (data members) and behaviour (method members) of the parent class (Super-class).
The level of Inheritance may be extended i.e. A Sub-class may have their own sub-classes.
In Real-life most of the things exhibit Inheritance relationship.
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.Syntax
class Super { ..... ..... } class Sub extends Super { ..... ..... }
Note − A subclass inherits all the members (fields, methods, and nested classes) from its superclass.
Why Inheritance ?
Modelling of Real-world: By Inheritance, we can model the real-world inheritance relationships in easy way.
Reusability of codes: Inheritance allows the derivation of a new class from existing classes. We can add new features to derived class to make a new one with minimal efforts.
Transitive nature of Inheritance: If we modify the base class then the changes automatically inherit into derived classes. Since inheritance is transitive in nature. This offers faster and efficient way to maintain the large program.
Types of Inheritance:
In OOP approach, the Inheritance is many types.
Single Level Inheritance: When a sub-class inherits only one base-class.
Multi-level Inheritance: When a sub-class is derived from sub-class of another base-class.
Multiple Inheritance: When a sub-class derived from multiple base-classes.
Hierarchical Inheritance: When Multiple sub-classes are derived from a single base class.
Hybrid Inheritance: When a sub-class inherits from multiple base-classes and all its base classes inherits one or more super-classes.
Types of Inheritance
There are various types of inheritance as demonstrated below.
Access Specifiers: In General, when a Sub-Class is created from a base Class, then all data members and method members of Parent (base) class are inherited in the child class and hence they can be accessed in child class because they are visible. But some time, it is required to restrict some methods from being inherited in child class to maintain the privacy and security. Java provides some Access Specifiers, which can control the visibility/access of the Parent (base) Class from Child (sub) class. The members (Data and Methods) of the Parent Class may be defined as Private, Public, Protected, and Default, which may limit its accessibility or visibility in the derived (child) classes.
Private : Members are accessible only inside their own class and no where else.
Protected: Members are accessible in own class, all classes within package and all Sub-classes in different package.
Public: Members are accessible everywhere in all the classes. Package (default): Members without any specifier assumed package level scope i.e. accessible to all classes inside the package only.
Abstract Class: An Abstract class simply represents a concept for its sub-classes. An Abstract class works as template for its sub-classes which contains only data members and method prototype i.e. methods without code (concept). Sometimes, we need to define a super-class having general characteristics (data) and behaviour (generic methods) for its sub-classes.
A Sub-class may re-define the methods or overridden to perform a task since super-class contains only the prototype (method with empty body). Abstract classes are normally used as base class in inheritance for which no object is required
e.g. JOptionPane Class in Java is Abstract class because it requires no object.
Whereas JTextField, JLabel classes etc. are called Concrete classes because they requires an object like ‘jTextField1’ for using them.
Role of Final keywords in inheritance: The final keyword can be used with- Variable, Methods and Class names. The effect of final keywords is as follows.
final variables works as constant i.e. the value of a final variables can’t be changed. final methods can’t be overridden by sub- class.
final class can’t be extended. Concept of Polymorphism: In Simple term, Polymorphism means Multiple forms of behaviour. For example, a person may exhibit different behaviour in different places or situation.
In Object Oriented Programming, a Method or Operator may exhibit different behaviour for different sets of input given.
For example the ‘+’ operator in Java gives different result for different input (integer and string) 2+3 gives 5 but “Hello”+”Java” gives “HelloJava”
Here, + operators exhibits different behaviour for numbers and string values i.e. + operator is overloaded.
Same as Math.round() function exhibit different behaviour for float and double type values i.e. Method Overloading.
Polymorphism is implemented as Method Overloading and Operator Overloading i.e. overloaded with different functionalities. Polymorphism makes your program code compact, smarter and faster.
Objects in Java Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior. If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running. If you compare the software object with a real-world object, they have very similar characteristics. Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods. So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
Classes in Java A class is a blueprint from which individual objects are created. Following is a sample of a class. public class Dog{ String breed; int ageC String color; void barking(){ } void hungry(){ } void sleeping(){ } }
Java Package In simple words, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier.
Import Statements In Java if a fully qualified name, which includes the package and the class name is given, then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class. For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io: import java.io.*;
Java Modifiers
Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers −- Access Modifiers − default, public , protected, private
- Non-access Modifiers − final, abstract, strictfp
No comments:
Post a Comment