Classes and Objects.pptx
- Количество слайдов: 10
Classes and Objects Class is a template for an object(contains the data fields and methods) public class Employee{ double salary; Data fields double bonus; method Void calculate. Total. Pay(){ double totalpay=salary+bonus; System. out. println(“Totalpay is” +totalpay); } } Object is a copy of a class(instance of a class) public class Test. Employee{ public static void main (String[] args){ Employee alex =new Employee(); Employee linda =new Employee(); alex. salary=90000; alex. bonus=20000; alex. calculate. Total. Pay(); } }
Classes and Objects public class Test. Box{ public static void main (String[] public class Box{ args){ int length; Box obj 1 =new Box(); int width; Box obj 2 =new Box(); obj 1. length=5; Void calculate. Area(){ obj 1. width=10; obj 1. calculate. Area; int area=length*width; System. out. println(“Area is” +area); obj 2. length=15; obj 2. width=2; obj 2. calculate. Area; } }
Advantage of return method calculating sum of areas public class Box{ int length; int width; Int calculate. Area(){ int area=length*width; return area; } } public class Test. Box{ public static void main (String[] args){ Box obj 1 =new Box(); Box obj 2 =new Box(); obj 1. length=5; obj 1. width=10; Int area 1=obj 1. calculate. Area; obj 2. length=15; obj 2. width=2; Int area 2=obj 2. calculate. Area; System. out, println(area 1+area 2); } }
Methods with arguments public class Box{ int length; int width; Int calculate. Area(int x){ int area=length*width*x; return area; } } public class Test. Box{ public static void main (String[] args){ Box obj 1 =new Box(); Box obj 2 =new Box(); obj 1. length=5; obj 1. width=10; Int area 1=obj 1. calculate. Area(4); System. out. println(area 1); } }
Methods with arguments public class Box{ int length; int width; Int calculate. Area(int length, int width){ int area=length*width; return area; } } public class Test. Box{ public static void main (String[] args){ Box obj 1 =new Box(); Box obj 2 =new Box(); obj 1. length=5; obj 1. width=10; Int area 1=obj 1. calculate. Area(4, 3); System. out. println(area 1); } } Output 12
Methods with arguments public class Box{ int length; int width; Int calculate. Area( int length, int width){ int area=this. length*this. width; return area; } } public class Test. Box{ public static void main (String[] args){ Box obj 1 =new Box(); Box obj 2 =new Box(); obj 1. length=5; obj 1. width=10; Int area 1=obj 1. calculate. Area(4, 3); System. out. println(area 1); } } Output 50
Constructors public class Small. Box{ int length; int width; Small. Box(int length, int width) { this. length = length; this. width=width; } public class Test. Small. Box{ public static void main (String[] args){ Small. Box obj 1 =new Small. Box(3, 4); obj 1. calculate. Area(); } } void calculate. Area( ){ Output ? System. out. println(“Area is” +(length+width)); } }
Constructors public class Test. Box{ public class Small. Box{ public static void main (String[] int length; args){ int width; Small. Box obj 1 =new Employee(); Small. Box( ){ obj 1. calculate. Area(); } this. length=5; } this. width=6; } Output ? void calculate. Area(){ System. out. println(“Area”+(length+width) ); } } if you want some defalt value you can use a constructor(initial defalt values) Concructors have the same name as class Doesn’t’t return anything including void
Constructors with parameters public class Test. Box{ public class Small. Box{ public static void main (String[] int length; args){ int width; Box obj 1 =new Employee(); Small. Box(int length, int width ){ obj 1. calculate. Area(); } this. length=length; } this. width=width; } Output ? void calculate. Area(){ System. out. println(“Area”+(length+width) ); } } if you want some defalt value you can use a constructor(initial defalt values) Concructors have the same name as class Doesn’t’t return anything including void
Classes and Objects.pptx