Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
35 views
46 slides
May 13, 2024
Slide 1 of 46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
About This Presentation
Classes, Inheritance ,Packages & Interfaces in JAVA
Size: 9.08 MB
Language: en
Added: May 13, 2024
Slides: 46 pages
Slide Content
Unit 2 Classes, Inheritance ,Packages & Interfaces
procedure Oriented programming Object Oriented Programming Divided Into In POP, program is divided into small parts called f unctions . In OOP, program is divided into parts called objects. Importance In POP,Importance is not given to data but to functions as well as sequence of actions to be done. (Algorithm) In OOP, Importance is given to the data rather than procedures or functions because it works as a real world . Approach POP follows Top Down approach. OOP follows Bottom Up approach. Access Specifiers POP does not have any access specifier . OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system. In OOP, objects can move and communicate with each other through member functions. Data Access In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system. In OOP, data can not move easily from function to function, it can be kept public or private so we can control the access of data. Data Hiding POP does not have any proper way for hiding data so it is less secure. OOP provides Data Hiding so provides more security . Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of Function Overloading and Operator Overloading . Examples Example of POP are : C, VB, FORTRAN , Pascal. Example of OOP are : C++, JAVA , VB.NET, C#.NET.
Class & Object CLASS : Represents the group of similar objects. A class binds the data & its function together. It has 2 parts. 1.Class Definition –Member Data 2.Class function definitions- Member Functions An object is defined by a class A class is the blueprint of an object The class uses methods to define the behaviors of the object The class that contains the main method of a Java program represents the entire program A class represents a concept, and an object represents the embodiment of that concept Multiple objects can be created from the same class
Objects An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it) The state of a bank account includes its account number and its current balance The behaviors associated with a bank account include the ability to make deposits and withdrawals Note that the behavior of an object might change its state. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
DIFFERENCE BETWEEN STRUCTURE AND CLASS STRUCTURE CLASS Hold only one data contains both function & data Data members are PUBLIC by default Members are PRIVATE by default Members are easily accessible Not easy to access Donot support inheritance Supports inheritance no such things Members of a class can be Private, public & private also
Constructors are used to set initial values for the object's instance variables, and to perform other actions that need to be done before the object is used
How to Create Object in Java Using new Keyword Using clone() method Using newInstance () method of the Class class Using newInstance () method of the Constructor class Using Deserialization
SYNTAX ClassName object = new ClassName ();
public class Hello { int x = 5; public static void main(String[] args ) { Hello myObj = new Hello(); System.out.println ( myObj.x ); } }
public class Hello { int x = 5; public static void main(String[] args ) { Hello myObj1= new Hello(); Hello myObj2= new Hello(); System.out.println ( myObj.x ); } }
Naming conventions
ACCESS SPECIFIERS CLASS MEMBER VISIBILITY: 1. PRIVATE : The class members(functions & data) as private can be access only within the class.(By default its private only) 2 . PUBLIC : Can be accessed from outside the class 3. PROTECTED: Used in inheritance .
Syntax to declare a class: class < class_name >{ data member; method; } Simple Example of Object and Class class Student1{ int id; //data member (also instance variable) String name; //data member(also instance variable) void getId (); //member function public static void main(String args []){ Student1 s1=new Student1(); //creating an object of Student System.out.println (s1.id); System.out.println (s1.name); s1.getId(); } }
The new keyword is used to allocate memory at runtime. The program shows an example to insert values in a record. Advantage of Method Code Reusability Code Optimization new keyword
class Box { double width; double height; double depth; } class BoxDemo2 { public static void main(String args []) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol ; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9 ; // compute volume of first box vol = mybox1.width * mybox1. height * mybox1.depth; System.out.println ("Volume is " + vol ); // compute volume of second box vol = mybox2.width * mybox2 .height * mybox2.depth; System.out.println ("Volume is " + vol ); } }
// This program includes a method inside the box class . class Box { double width; double height; double depth; // display volume of a box double volume() { System.out.print ("Volume is "); return (width * height * depth); } } class BoxDemo3 { public static void main(String args []) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol ; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // display volume of first box vol =mybox1.volume(); System.out.println ( vol ); // display volume of second box vol =mybox2.volume(); System.out.println ( vol ); }}
Constructors Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation . It constructs the values i.e. provides data for the object that is why it is known as constructor. Rules for creating java constructor Constructor name must be same as its class name Constructor must have no explicit return type, not even void.
Types of java constructors There are 3 types of constructors: Default constructor (no- arg constructor) Parameterized constructor Copy constructor (object clone)
Default constructors class Box { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println ("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo6 { public static void main(String args []) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol ; // get volume of first box vol = mybox1.volume(); System.out.println ("Volume is " + vol ); // get volume of second box vol = mybox2.volume(); System.out.println ("Volume is "+ vol ); } }
Parameterized constructor class Box { double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo { public static void main(String args []) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 5); Box mybox2 = new Box(3, 6, 9); double vol ; // get volume of first box vol = mybox1.volume(); System.out.println ("Volume is " + vol ); // get volume of second box vol = mybox2.volume(); System.out.println ("Volume is " + vol ); } }
Copy constructor Previous program can be extended with passing object of the class to the constructor itself. Eg : Box (Box b) { System.out.println (“cloning object”); } Public static void main(String args []) { Box mybox1 = new Box(10, 20, 5); Box mybox2 = new Box(mybox1); //mybox2 is cloned from mybox1 object } } Constructor with different arguments / different number of arguments are called constructor overloading .
Java program to illustrate Constructor Overloading class Box { double width, height, depth; // constructor used when no dimensions specified Box() { width = height = depth = 0; } // constructor used when cube is created Box(double len ) { width = height = depth = len ; } // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } } // Main code public class Test { public static void main(String args []) { // create boxes using the various constructors Box mybox2 = new Box(); Box mycube = new Box(7); Box mybox1 = new Box(10, 20, 15); double vol ; // get volume of first box vol = mybox1.volume(); System.out.println (" Volume of mybox1 is " + vol ); // get volume of second box vol = mybox2.volume(); System.out.println (" Volume of mybox2 is " + vol ); // get volume of cube vol = mycube.volume (); System.out.println (" Volume of mycube is " + vol ); } }
Method Overloading If a class have multiple methods by same name but different parameters, it is known as Method Overloading. Method overloading increases the readability of the program. 2 ways to differentiate the methods which are overloaded 1.by number of arguments 2.by data types
1)Example of Method Overloading by changing the no. of arguments class Calculation { void sum( int a,int b) { System.out.println ( a+b );} void sum( int a,int b,int c) { System.out.println ( a+b+c );} public static void main(String args []) { Calculation obj =new Calculation(); obj.sum (10,10,10); obj.sum (20,20); } }
2)Example of Method Overloading by changing data type of argument class Calculation2 { void sum( int a,int b) { System.out.println ( a+b );} void sum(double a,double b) { System.out.println ( a+b ); } public static void main(String args []) { Calculation2 obj =new Calculation2(); obj.sum (10.5,10.5); obj.sum (20,20); } }
//Write a program to find area of geometrical figures using method overloading.
import java.util .*; class geofig { double area(double r) { return(3.14*r*r); } float area(float s) { return(s*s); } float area(float l,float b) { return(l*b); } double area(double b,double h) { return(0.5*b*h); } public static void main(String arg []) { Scanner sc =new Scanner(System.in); geofig g = new geofig (); System.out.println ("enter the value for radius of circle"); double r = sc.nextDouble (); System.out.println ("area of circle="+ g.area (r)); System.out.println ("enter the value for side of a square"); float s = sc.nextFloat (); System.out.println ("area of square="+ g.area (s)); System.out.println ("enter the value for length and breadth of rectangle"); float l = sc.nextFloat (); float b = sc.nextFloat (); System.out.println ("area of rectangle="+ g.area ( l,b )); System.out.println ("enter the value for base & height of triangle"); double b1 = sc.nextDouble (); double h = sc.nextDouble (); System.out.println ("area of triangle="+ g.area (b1,h)); } }
Lab 8: import java.util .*; class GarbageCollection { public static void main(String s[]) throws Exception { Runtime rs = Runtime.getRuntime (); System.out.println ("Total memory is: " + rs.totalMemory ()); System.out.println ("Free memory in JVM before Garbage Collection = "+ rs.freeMemory ()); rs.gc (); System.out.println ("Free memory in JVM after Garbage Collection = "+ rs.freeMemory ()); } }
Access Specifier
Java Command Line Arguments The java command-line argument is an argument i.e. passed at the time of running the java program. The arguments passed from the console can be received in the java program and it can be used as an input.
class CommandLineExample { public static void main(String args []){ System.out.println ("Your first argument is: "+ args [0]); } }
keyword
final keyword The final keyword in java is used to restrict the user. The java final keyword can be used in many context For example, when a variable is declared with the "final" keyword, its value cannot be changed once it is set: final int a = 10; a = 20; // this will result in a compile-time error
the keyword "abstract" is used to declare an abstract class or method. Here's an example of an abstract class: abstract class Shape { abstract double area(); } An abstract method is a method that is declared in an abstract class, but does not have a body. The purpose of an abstract method is to specify a method signature that must be implemented by any concrete subclasses of the abstract class.
Finalize Keyword in java : The finalize method in java is called by the garbage collector before an object is garbage collected.