Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 28
JAVA Programming
Fundamental & OOP
Eng. Nada Mohamed Sarhan nada0sarhan@gmail.com OUTLINES • Quiz • Exercise • Encapsulation • Exercise • Abstraction • Interfaces • Exercise • Assignment Quiz • Is Java Platform Independent if then how? • Yes, Java is a Platform Independent language. Unlike many programming languages javac compiler compiles the program to form a bytecode or .class file. This file is independent of the software or hardware running but needs a JVM(Java Virtual Machine) file preinstalled in the operating system for further execution of the bytecode. • Although JVM is platform dependent, the bytecode can be created on any System and can be executed in any other system despite hardware or software being used which makes Java platform independent. Quiz • What is JVM? • JVM stands for Java Virtual Machine it is a Java interpreter. It is responsible for loading, verifying, and executing the bytecode created in Java. • Although it is platform dependent which means the software of JVM is different for different Operating Systems it plays a vital role in making Java platform Independent. Quiz • Explain public static void main(String args[]) in Java. • public: the public is the access modifier responsible for mentioning who can access the element or the method and what is the limit. It is responsible for making the main function globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class. • static: static is a keyword used so that we can use the element without initiating the class so to avoid the unnecessary allocation of the memory. • void: void is a keyword and is used to specify that a method doesn’t return anything. As the main function doesn’t return anything we use void. • main: main represents that the function declared is the main function. It helps JVM to identify that the declared function is the main function. • String args[]: It stores Java command-line arguments and is an array of type java.lang.String class. Quiz • Explain different data types in Java. 1. Primitive Data Type 2. Non-Primitive Data Type or Object Data type • Primitive Data Type: Primitive data are single values with no special capabilities. There are 8 primitive data types • Non-Primitive Data Type: Reference Data types will contain a memory address of the variable’s values because it is not able to directly store the values in the memory. Quiz • Differentiate between instance and local variables. Quiz • What is the default value stored in Local Variables? • There is no default value stored with local variables. Also, primitive variables and objects don’t have any default values. Quiz • Disadvantages of Arrays are? • Arrays are created with a predetermined size that is chosen at that moment. This means that if the array’s size needs to be extended, a new array will need to be made, and the data will need to be copied from the old array to the new array, which can take a lot of time and memory. • There may be unused memory space in an array’s memory space if the array is not completely occupied. If you have poor recall, this can be a problem. • Compared to other data structures like linked lists and trees, arrays might be rigid due to their fixed size and limited support for sophisticated data types. • Because an array’s elements must all be of the same data type, it does not support complex data types like objects and structures. Quiz • What are the main concepts of OOPs in Java? 1. Inheritance 2. Polymorphism 3. Abstraction 4. Encapsulation Quiz • How many types of constructors are used in Java? 1. Default Constructor 2. Parameterized Constructor Exercise • Write a Java program to create a class called "Event" with attributes for event name, date, and location. Create subclasses "Seminar" and "MusicalPerformance" that add specific attributes like number of speakers for seminars. Implement methods to display event details. Encapsulation • Data encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data and that keep both safe from outside interference and misuse. Encapsulation • To achieve encapsulation in Java − • Declare the variables of a class as private. • Provide public setter and getter methods to modify and view the variables values. Exercise • Write a Java program to create a class called Car with private instance variables company_name, model_name, year, and mileage. Provide public getter and setter methods to access and modify the company_name, model_name, and year variables. However, only provide a getter method for the mileage variable. Abstraction • Data abstraction or information hiding refers to providing only essential information to the outside world and hiding their background details. Abstraction • Adv. : • Simplification of software development. • Testing and debugging. • Reusability. • Security. • Modifications to the representation of data type. Interfaces • An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. • Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. • Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. Interfaces • Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. • An interface is similar to a class in the following ways − • An interface can contain any number of methods. • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. • The byte code of an interface appears in a .class file. • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name. Interfaces • However, an interface is different from a class in several ways, including − • You cannot instantiate an interface. • An interface does not contain any constructors. • All of the methods in an interface are abstract. • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces. Interfaces Implementing Interfaces • When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. • A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration. Implementing Interfaces Implementing Interfaces • Example:
• What is the Output ??
Implementing Interfaces • When overriding methods defined in interfaces, there are several rules to be followed − • Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method. • The signature of the interface method and the same return type or subtype should be maintained when overriding the methods. • An implementation class itself can be abstract and if so, interface methods need not be implemented Extending Interfaces • An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. Exercise • Write a Java program to create an interface Shape with the getArea() method. Create 3 classes Rectangle, Circle, and Triangle that implement the Shape interface. Implement the getArea() method for each of the three classes. Assignment • Write a Java program to create an interface Resizable with methods resizeWidth(int width) and resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that implements the Resizable interface and implements the resize methods. • Write a Java program to create a Animal interface with a method called bark() that takes no arguments and returns void. Create a Dog class that implements Animal and overrides speak() to print "Dog is barking"