• No results found

1015) Abstract class

In document Core_java (Page 43-54)

1016) An abstract class is a class that is declared with the abstract key word, it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed. Cant create the object of abstract class

1017) public abstract class Game 1018) {

1019) //abstract class without any methods 1020) }

1021)

1022) public abstract class Cade 1023) {

1024)

1025) abstract void draw(); 1026)

1027) //abstract class with abstract methods 1028) }

1029)

1030) abstract class Plan

1031) {

1032) void costEstimation()

1033) {

1034) //some code to tell about cost to build

1035) }

1036) abstract void baseMent(); 1037) abstract void build(); 1038)

1039) } //abstract class with concreate and abstract methods 1040)

1041) Whenever you inherit the abstract class you need to override the abstract methods of the abstract(PARENT) class in the inherited (CHILD) class. OR else you should declare the child class also has abstract class. See the example below

1042) Example : 1043) class Volvo 1044) { 1045) void body() 1046) { 1047) //code 1048) } 1049) void ac(); 1050) void music(); 1051) void seats(); 1052) } 1053)

1054) abstract class ElectricWork extends Volvo

1055) {

1056) void ac()

1057) {

1058) //code

1060) void music()

1061) {

1062) //code

1063) }

1064) }

1065) class KSRTC extends ElectricWork

1066) { 1067) void seats() 1068) { 1069) //code 1070) } 1071) } 1072)

Hear ElectricWork class is also abstract because all the abstract methods of Volvo class has not been overridden in this class, i.e seats() method is not overridden.

1073) KSRTC class has overridden all the parents class abstract methods so it need not to be abstract class

1074) Important Points about Abstract class

 abstract keyword is used to make a class abstract.

 Abstract class can’t be instantiated.

 We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.

 If a class have abstract methods, then the class also needs to be made abstract using abstract keyword, else it will not compile.

 Its not necessary to have abstract classes to have abstract method.

 If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.

 The subclass of abstract class must implement all the abstract methods unless the subclass is also an abstract class.

 Abstract classes can implement interfaces without even providing the implementation of interface methods.

 Abstract classes are used to provide common method implementation to all the subclasses or to provide default implementation.

 Abstract class can have a constructor but it can be called from the child class using supper statement

 We can run abstract class like any other class if it has main() method.

 In interview give some good examples like bank or google example (refer class notes) 1075) Example1:

1076) {

1077) void playSong(String songName)

1078) {

1079) System.out.println(songName+" is playing in the player");

1080) }

1081) abstract void pauseControle(); 1082) abstract void screenControler();

1083) }

1084)

1085) class Windows extends MediaPlayer

1086) {

1087) void pauseControle()

1088) {

1089) System.out.println("Press P to pause the song");

1090) }

1091) void screenControler()

1092) {

1093) System.out.println("Do changes using menu button");

1094) }

1095) }

1096)

1097) class VLC extends MediaPlayer

1098) {

1099) void pauseControle()

1100) {

1101) System.out.println("Press space-bar to pause the song");

1102) }

1103) void screenControler()

1104) {

1105) System.out.println("Press A to change the screen size");

1106) }

1107) }

1108)

1109) Interface

 Interface in java is one of the way to achieve 100% abstraction in Java .

 Interface in java is declared using keyword interface. It is also just like normal class. For interface also the .class file is going to be generated.

 All methods inside interface are public by default

 All variables declared inside interface is implicitly public final variable or constants. 1110) interface Office

1111) {

1112) int employee;//Error because only final variables so they need to be initialised has donoted in below example. No other types are allowed inside interface

1113) }

1114) interface Office

1116) int employee=100;

1117) }

 All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword

 Cant create the Object of interface

 In Java its legal for an interface to extend multiple interface. for example following code will run without any compilation error

1118) package com.vikas.jspiders; 1119) interface Animal

1120) {

1121) public void eat();

1122) public void walk();

1123) }

1124) interface Human

1125) {

1126) public void talk();

1127) public void work();

1128) }

1129) interface Livingbeing extends Animal,Human // extend multiple interface

1130) {

1131) public void enjoyWithLover();

1132) }

1133) Whenever we implement interface we need to provide implementation to all the abstract methods of it, or else we need to make the inherited class also has abstract.

1134) interface Office

1135) {

1136) void work();

1137) public void enjoy();

1138) }

1139) abstract class Manager implements Office

1140) {

1141) //implementation is not give for work() and enjoy() so class should be abstract 1142) } 1143) 1144) Example:- 1145) interface Animal 1146) {

1147) public void eat();

1148) public void walk();

1149) }

1150) interface Human

1151) {

1152) public void talk();

1153) public void work();

1154) }

1155) interface Livingbeing extends Animal,Human

1156) {

1157) public void enjoyWithLover();

1158) }

1159) public class Test implements Livingbeing{ 1160)

1161) public void eat() 1162) {

1163) // TODO Auto-generated method stub

1164) }

1165) public void walk()

1166) {

1167) // TODO Auto-generated method stub

1168) }

1169) public void talk() 1170) {

1171) // TODO Auto-generated method stub

1172) }

1173) public void work()

1174) {

1175) // TODO Auto-generated method stub

1176) }

1177) public void enjoyWithLover() 1178) {

1179) // TODO Auto-generated method stub

1180) }

1181)

1182) }

1183)

1184) The Interface reference can be give to its child class object. It behave same has the inheritance. Using the interface reference we cant accesses the child class members. But we can accesses the overrided methods.

1185)

1186) Example: 1187)

1188) interface Animal

1189) {

1190) public void eat();

1191) public void walk();

1192) }

1193)

1194) class Cow implements Animal

1195) {

1196)

1197) public void eat()

1198) {

1199) System.out.println("Cow eat hullu");

1200) }

1201)

1202) public void walk()

1203) {

1204) System.out.println("Cow walk in four legs");

1205) }

1206) public void giveMilk()

1207) {

1208) System.out.println("Cow give white milk");

1209) }

1210)

1211) }

1212) class Test1

1213) {

1215)

1216) Animal a = new Cow();

1217) a.eat();

1218) a.walk();

1219) a.giveMilk();//Error because parent reference cant accesses child members 1220) } 1221) } 1222) 1223) 1224) Abstract 1225) Interface 1226) Multiple Inheritance 1227) A class can inherit only one Abstract Class 1228) A class can implement multiple Interfaces 1229) Concrete methods (Default Behavior) 1230) In abstract class you can provide default behavior of a method, so that even if child class does not provide its own behavior, you do have a default behavior to work with

1231) You cannot provide a default behavior in interfaces. Interfaces only allow you to provide

signature of the method

1232) Access Modifiers

1233) You can provide access

modifiers to methods in abstract classes (static , final, etc) 1234) You cannot provide access modifiers methods in Interfaces. 1235)

1236) Methods and variables

1237) The methods or variables can be declared with any accesses specifiers and modifiers. (Note : but abstract methods should not be static , final, private) 1238)

1239) All methods are public by default, all the variables are static final by default. 1240)

1241)

1242) Abstraction

1243) abstraction in Java is used to hide certain details and only show the essential features of the object.

1244) Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.

1245) Abstraction lets you focus on what the object does instead of how it does it.

1246)We are going to achieve abstraction using

1247) Abstract class

1248) Interface

1249)

1251)

1252) Encapsulation is the packing of data and functions into a single component.

Encapsulation is the ability to package data, related behaviour in an object bundle and control/restrict access to them (both data and function) from other objects. It is all about packaging related stuff together and hide them from external elements.

1253) Encapsulation can be achieved by declaring fields in a class as private, while

providing access to these fields via public, typically, getter and setter methods. Or else we can tell that the encapsulation can be achieved by java beans.

1254) package com.vikas.jspiders; 1255) 1256) import java.util.Scanner; 1257) 1258) class Register 1259) {

1260) private String name,email,password;

1261) private int age;

1262) private long phone;

1263) private double hight;

1264) private char gender;

1265) public String getName() {

1266) return name;

1267) }

1268) public void setName(String name) {

1269) this.name = name;

1270) }

1271) public String getEmail() {

1272) return email;

1273) }

1274) public void setEmail(String email) {

1275) this.email = email;

1276) }

1277) public String getPassword() {

1278) return password;

1279) }

1280) public void setPassword(String password) {

1281) this.password = password;

1282) }

1283) public int getAge() {

1284) return age;

1285) }

1286) public void setAge(int age) {

1287) this.age = age;

1288) }

1289) public long getPhone() {

1290) return phone;

1291) }

1292) public void setPhone(long phone) {

1293) this.phone = phone;

1294) }

1295) public double getHight() {

1296) return hight;

1297) }

1298) public void setHight(double hight) {

1300) }

1301) public char getGender() {

1302) return gender;

1303) }

1304) public void setGender(char gender) {

1305) this.gender = gender; 1306) } 1307) 1308) } 1309) class RegisterDetails 1310) { 1311) void insertDetails(Register r) 1312) { 1313) System.out.println("Name is "+r.getName()); 1314) System.out.println("Email is "+r.getEmail()); 1315) System.out.println("Password is "+r.getPassword()); 1316) System.out.println("Age is "+r.getAge()); 1317) System.out.println("Gender is "+r.getGender()); 1318) System.out.println("Phone number "+r.getPhone()); 1319) System.out.println("Height is "+r.getHight()); 1320)

1321) }

1322) }

1323) public class Demo {

1324) public static void main(String[] args) { 1325)

1326)

1327) Scanner scan = new Scanner(System.in); 1328) System.out.println("Enter the name"); 1329) String name = scan.next();

1330) System.out.println("Enter the Email"); 1331) String email = scan.next();

1332) System.out.println("Enter the password"); 1333) String password = scan.next();

1334) System.out.println("Enter the age"); 1335) int age=scan.nextInt();

1336) System.out.println("Enter phone number"); 1337) long l = scan.nextLong();

1338) System.out.println("Enter height"); 1339) double height = scan.nextDouble(); 1340) System.out.println("Enter gender");

1341) char gender =(char) scan.next().charAt(0); 1342)

1343) Register r = new Register();

1344) r.setName(name); 1345) r.setEmail(email); 1346) r.setPassword(password); 1347) r.setHight(hight); 1348) r.setAge(age); 1349) r.setGender(gender); 1350)

1351) RegisterDetails rd = new RegisterDetails();

1352) rd.insertDetails(r);// hear the bean object is sent to the insertDetails method in RegisterDetails class

1353) }

1355) } 1356)

1357) Output

1358) Enter the name

1359) vikas

1360) Enter the Email

1361) [email protected] 1362) Enter the password

1363) 12345

1364) Enter the age

1365) 25

1366) Enter phone number 1367) 8147214063 1368) Enter height 1369) 5.5 1370) Enter gender 1371) M 1372)

1373) Out put after taking input 1374) 1375) Name is vikas 1376) Email is [email protected] 1377) Password is 12345 1378) Age is 25 1379) Gender is M 1380) Phone number 8147214063 1381) Height is 5.5

1382)

1383) Polymorphism

1384)

1385) Polymorphism is the ability by which, we can create functions or reference variables which behaves differently in different programmatic context. Polymorphism is a Greek word which means many(poly) forms(morphism).

1386) Polymorphism is tightly coupled to inheritance and is one of the most powerful advantages to object-oriented technologies.

1387) Polymorphism is essentially considered into two versions. 1. Compile time polymorphism (static binding or method overloading) 2. Runtime polymorphism (dynamic binding or method overriding)

1388) Note : see class notes for overload and overriding examples

1389) Static binding 1390) Dynamic binding

1391) When type of the object is determined at compiled time(by the compiler), it is known as static binding

1392) When type of the object is determined at run-time, it is known as dynamic binding

1393) If there is any private, final or static method in a class, there is static binding

1394) Other methods than private , final , static

1395) Static binding uses

Type(Class in Java) information for binding

1396) Dynamic binding uses Object to resolve binding.

1397) Overloaded methods are bonded using static binding

1398) Overridden methods are bonded using dynamic binding at runtime.

1399) Compile time polymorphism

1400) Runtime polymorphism

1401)

1402)

1403) What Is a Package?

1404) Packages are nothing but folder structure. It groups different class and files according to their relations which performs corresponding relation. (Eg : How you keep the photos separately in the folders, and also sometimes u group different photos into different folders)

1405) Steps to create package –  Create New Java Project

 Select src folder and right click on it and select the package option

 Than give the package name has you like but it is good practice to start the package name with com (eg : com.vikas.jspider) com means commercial Or you can right click on src and select the new and than u can also select

folder name and type the name and create the package 1406)

1407) Note – Package name should be the 1st line of the code 1408)

1409) What is import?

1410) We use import statement to indicate for our JVM to import the particular class from the package

1411) Note –

 Import must be immediate line after the package and before the class  We cannot import different class which has the same name from multiple

packages

 It imports class which r in same package but it does not import the class which are present in sub packages. So we need to import them

 The java.lang package is imported by the compiler, and we need not to do it explicitly. The java.lang package is the default package

1412) Structure of code 1413)

1414) Package Name 1415) 1416) Import statements 1417) 1418) class <className> 1419) { 1420) / /code 1421) } 1422) 1423)

1424) Accesses specifiers

PublicProtectedDefaultPrivate

 The accesses specifiers will define the scope of every member

 In java accessibility of any member can be controlled by using accesses modifiers. In picture the accessibility can be given has

1425)

PUBLIC is the high visible access specifier in java which can be accessed every were

PRIVATE is the highly restricted access specifier in java that it can be accessed form only inside the class. Such that the members which are declared has private cannot be accessed from outside the class

DEFAULT the scope of default members in only inside the package. They can be accessed within package but not from outside the package. If we don’t specify any access specifier than the member would be taken has default access specifier

PROTECTED the protected members can be accessed within the package, but they can be accessed outside the package also only in the case of inheritance

1426)

In document Core_java (Page 43-54)

Related documents