CS8392 -OOPS Unit-1 PDF

Title CS8392 -OOPS Unit-1
Author LOGANATHAN K
Course Object oriented programming
Institution Anna University
Pages 76
File Size 2.3 MB
File Type PDF
Total Downloads 70
Total Views 147

Summary

wewe...


Description

MAILAM ENGINEERING COLLEGE UNIT I

-

OOP- CS8392 – UNIT - I

INTRODUCTION TO OOP AND JAVA FUNDAMENTALS

Object Oriented Programming - Abstraction – objects and classes - Encapsulation- Inheritance - PolymorphismOOP in Java – Characteristics of Java – The Java Environment - Java Source File -Structure – Compilation. Fundamental Programming Structures in Java – Defining classes in Java – constructors, methods -access specifiers - static members -Comments, Data Types, Variables, Operators, Control Flow, Arrays , Packages JavaDoc comments. Basic Concepts of OOP Definition Objects Objects are basic run- time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. Each object has the data and code to manipulate the data and theses objects interact with each other. Class The entire set of data and code of an object can be made a user-defined data type with the help of a class. Once a class has been defined, we can create any number of objects belonging to the classes. Classes are user-defined data types and behave like built- in types of the programming language. Data Abstraction Abstraction refers to the act of representing essential features without including the background details or explanations. Encapsulation Wrapping up of data and function within the structure is called as encapsulation. Inheritance Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. It provides the idea of reusability. We can add additional features to an existing class without modifying it by deriving a new class from it. Polymorphism Polymorphism a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. Dynamic binding or late binding Binding refers to the linking of a procedure to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at the run-time. Information Hiding (or Data Hiding) The insulation of the data from direct access by the program is called data hiding or information hiding. Abstract class: Abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built.

PREPARED BY: K.Loganathan, AP/IT

1

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

PART – A 1) What is meant by Object Oriented Programming? Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic. A program has been viewed as a logical procedure that takes input data, processes it, and produces output data. 2) What is a Class?(Nov/Dec-18) A class is a collection of objects that have common properties, operations and behaviours. A class is a combination of state (data) and behaviour (methods). A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. 3) What is an Object? ?(Nov/Dec-18) • Object is an instance of a class. • An object is a location in memory having a value and referenced by an identifier. • An object can be a variable, function, or data structure. • It has state, behaviour and identity. 4) What are the key characteristics of objects? Three properties characterize objects: o Identity: the property of an object that distinguishes it from other objects o State: describes the data stored in the object o Behavior: describes the methods in the object's interface by which the object can be used 5) What is an Instance? An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object. 6) What are the core OOP’s concepts? Abstraction, Encapsulation, Inheritance and Polymorphism are the core OOP‘s concepts. 7) What is meant by abstraction? Abstraction refers the act of representing essential features without including background details. 8) What is meant by Encapsulation? Encapsulation is the process of combining data & functions together into a single entity. 9) What are Inheritance and Polymorphism? Inheritance is the process by which one object acquires the properties of another object. Polymorphism is the feature that allows one interface to be used for general class actions.

PREPARED BY: K.Loganathan, AP/IT

2

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

10) What are methods and how are they defined? Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes. Method definition has four parts. They are name of the method, type of object or primitive type the method returns, a list of parameters and the body of the method. A method‘s signature is a combination of the first three parts mentioned above. 11) What are different types of access modifiers (Access specifiers)? Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. o public: Any thing declared as public can be accessed from anywhere. o private:Any thing declared as private can‘t be seen outside of its class. o protected:Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages. o default modifier :Can be accessed only to classes in the same package. 12) What is an Object and how do you allocate memory to it? Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it. 13) Explain the usage of Java packages. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes. 14) What is method overloading and method overriding(runtime polymorphism)? Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding: When a method in a class having the same method name with same arguments is said to be method overriding. 15) What gives java it’s “write once and run anywhere” nature? All Java programs are compiled into class files that contain byte codes. These byte codes can be run in any platform and hence java is said to be platform independent. 16) What is a Constructor and Destructor? • Constructor is a special type of method that can be used to initialize an object. • Constructor will be automatically invoked when an object is created • The name of the constructor name is same as the class name. PREPARED BY: K.Loganathan, AP/IT

3

MAILAM ENGINEERING COLLEGE • •

OOP- CS8392 – UNIT - I

Constructor do not have any return type. Destructor is a special type of method that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM.

17) What is the difference between Constructor and Method? • Constructor will be automatically invoked when an object is created • Whereas method has to be called explicitly. 18) What are Static member classes? Static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class. 19) What is Garbage Collection and how to call it explicitly? When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection. System. gc() method may be used to call it explicitly. 20) In Java, How to make an object completely encapsulated? All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables. 21) What are the functions of a constructor? • It can be used to initialize the fields. • It can be used to allocate a memory to the field. 22) What is a default constructor? Default constructor is ac constructor with no arguments. If no constructor is defined in a class then a default constructor is automatically provided by the compiler. This constructor is assigned default values to all fields in class. 23) What is static variable and static method? • Static variable is a class variable which value remains constant for the entire class. • Static method is the one which can be called with the class itself and can hold only the static variable. 24) What is finalize() method? finalize () method is used just before an object is destroyed and can be called just prior to garbage collection. 25) What is a package? A package is a collection of classes and interfaces that provides a high-level layer of access protection and name space management.

PREPARED BY: K.Loganathan, AP/IT

4

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

26) What is the difference between this() and super()? this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor. 27) Explain working of Java Virtual Machine (JVM)? JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes. 28) What is casting? Conversion of one type of data to another when appropriate. Casting makes explicitly converting of data. 29) What is an Abstract Class? Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour, typically by implementing its abstract operations. 30) What is an Interface? Interface is an outside view of a class or object which emphaizes its abstraction while hiding its structure and secrets of its behaviour. 31) What's the difference between an interface and an abstract class? An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritances. On the other hand, you can implement multiple interfaces in your class. 32) What's the difference between constructors and other methods? Constructors must have the same name as the class and cannot return a value. They are only called once while regular methods could be called many times. 33) Write the characteristics of java? • Simple , small and Familiar • Object oriented • Distributed • Robust • Secure • Platform independent • Portable • Compiled and interpreted • High performance • Multithreaded • Dynamic and interactive.

PREPARED BY: K.Loganathan, AP/IT

5

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

34) How does one import a single package. (April/May-2019) Import a single package .it can be achieved by Import packagename . classname Or Import packagename.* Example: Import java.lang.thread; Import java.lang.*; 35) What is instance variable? • Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. • Instance variables can be accessed from inside any method, constructor or blocks of that particular class. 36) Write the types of constructors. • Default constructor • Parameterized constructor • Copy constructor 37) What is command line parameters in java? • A Java application can accept any number of arguments from the command line. • This allows the user to specify configuration information when the application is launched. • The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. 38) Write the types of variables. • Local variables . variables defined inside methods, constructors 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. • Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. • Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword. 39) What is Overloaded Constructors? Constructors can also be overloaded. Since the constructors in a class all have the same name as the class, their signatures are differentiated by their parameter lists. 40) What are the advantages of using packages? 1. class can be easily reused 2. Two classes in different packages can have same name 3. it provides a way to hide a classes.

PREPARED BY: K.Loganathan, AP/IT

6

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

41) what is meant by array and how will you declare? The array, which stores a fixed-size sequential collection of variables (or) elements of the same type. Declaring Array Variables To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Syntax: dataType[] arrayRefVar; //preferred way (or) dataType arrayRefVar[]; //works but not preferred way. 42.Define Access Specifier.(Nov/Dec-18) Java Access Specifiers (also known as Visibility Specifiers ) regulate access to classes, fields and methods in Java. These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access.

43. Can a java source file be saved using a name other than the class name?(April/May-2019) Yes, you can save your java source code file with any other name, not same as your main class name but when you compile it than byte code file name will be same as your main class name. ... And you have compile and run a class with different name other than the filename, If you don't have any public methods in this class. 44. What are inline function? Give examples. (April/May-2019) The keyword inline can be used to hint to the compiler to perform inline expansion of the body of a member or nonmember function. To replace a function call with a copy of the function's code during compilation. For example it is written something like : When a method is final, it may be inlined.

PREPARED BY: K.Loganathan, AP/IT

7

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

PART – B 1.Explain the basic concepts of object oriented programming in detail. (or) . Explain the characteristics (or) Principles of OOPs.(Nov/Dec-18)(April/May-19) BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING

      

Objects Classes Data abstraction and encapsulation Inheritance Polymorphism Dynamic binding Abstract Class

Objects: Objects are the basic run-time entities in an object oriented programming. They may represent a person, a place, a bank account or any item that the program has to handle. They may represent user-defined data such as vectors, time and lists. Programming problem is analyzed in terms of objects and the nature of communication between them. Program objects should be chosen such that they match closely with the real world objects. When a program is executed, the objects interact by sending messages to another. Each object contains data and code to manipulate the data. Objects can interact without having to know the details of each others data or code. It is sufficient to know the type of messageaccepted, and the type of message accepted and the type of response returned by the objects. Classes: The entire set of data and code of an object can made a user defined data type with the help of a class. In fact the objects are variable of the type class. Once class has been defined we can create any number of objects belonging to that class. A Class is thus a collection of objects of similar type. For example, mango, apple and orange are members of the class fruit. Classes are user defined data type and behave like the built-in types of a programming language. In short, a class serves as a blueprint or a plan or a template. It specifies what data and what functions will be included in objects of that class. Defining the class doesn‘t create any objects, just as the mere existence of a type int doesn‘t create any variables. Data Abstraction and Encapsulation: The wrapping up of data and functions into a single unit is known as encapsulation. It is the most striking feature of the class. The data is not accessible to the outside world and only those functions which are wrapped in the class can access it. These functions provide interface between the object‘s data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding. Abstraction represents the act of representing the essential features without including the background PREPARED BY: K.Loganathan, AP/IT

8

MAILAM ENGINEERING COLLEGE

OOP- CS8392 – UNIT - I

details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes. The attributes are called data members and functions are called member functions or methods. Since the classes use the concept of dataabstraction, they are known as Abstract Data Types (ADT). Inheritance: Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes has all the features of the base class with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

It is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, the bird ‗robin‘ is a part of the class ‗flying bird‘ which is again a part of the class ‗bird‘. This concept provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by a deriving a new class from an existing one. The new class will have the combined features of both the classes. Polymorphism: It means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. For example the operation addition will generate sum if the operands are numbers whereas if the operands are strings then the operation would produce a third string by concatenation. There are two types of polymorphism, they are 1.Compile time polymorphism 2.Runtime polymorphism i) Compile time polymorphism – The compiler selects the appropriate function...


Similar Free PDFs