Java Classes methods and inheritance

2,500 views

Published on

This presentation explains about the Java classes, methods and inheritance concepts. This also explains about the usage of super and this keywords.

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,500
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
98
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Java Classes methods and inheritance

  1. 1. Classes, Methods and Inheritance SCJP / OCJP objectives : 1.1, 1.2, 1.3, 1.4, 5.5 By,www.JAVA9S.com Srinivas Reddy.S
  2. 2. Declaring Classes Syntax: package com.java9s.ocjp; import com.sun.sample; class Car{ int speed;  State void move(){  behaviour //code related to move } } Save the file with Car.java.  If there are multiple classes in a file, the file name should be the name of the class with public access modifier.www.JAVA9S.com
  3. 3. Declaring classes - Rules • The package statement should be the first statement in a file if the class belongs to a package. • Import statements comes next to package statement. • The order packageimportclass should be maintained. • Comments can come anywhere in the java file. • A java file can have any number of non public class fileswww.JAVA9S.com
  4. 4. Declaring classes - Rules • package and import statements declared in a file apply to all the classes defined in the file. • A separate .class file will be generated for each class defined in the java file. • Any name can be given to a file when there is no public class declared in it.www.JAVA9S.com
  5. 5. Creating Objects Car c = new Car(); Instantiation Declaration C is the reference which holds the Car Speed=50 memory address of the Car object Cwww.JAVA9S.com
  6. 6. Creating Objects Car a = new Car(); a Car b = new Car(); b Car c = new Car(); c Car d = new Car(); d Car e = d; e d.speed =60; System.out.println(e.speed) ;-> 60www.JAVA9S.com
  7. 7. Methods • Methods are members of a class. • Methods have the behavior of an object. • Methods can be declared with or without arguments. • Two variants for a method: – Methods that return something – Methods that don’t return anything - voidwww.JAVA9S.com
  8. 8. Methods – Return type Syntax: type methodName(arguments){ //code that decides the methods return x; } E.g., int addition(int a, int b){ int c = a+b; return c; }www.JAVA9S.com
  9. 9. Methods – void type Syntax: void methodName(arguments){ //Method code.. No need to return anything. } E.g., void saveToFile(String message){ //Code related to saving message to file.. }www.JAVA9S.com
  10. 10. Method – without argument Method with No arguments and with a return type: Date getCurrentDate(){ return Calender.get(Calender.DAY_OF_MONTH); } Method with no argument and no return type void printCurrentDate(){ System.out.println(Calendar.get(Calender.DAY_OF_MONTH)); }www.JAVA9S.com
  11. 11. Inheritance • Inheritance is a way to reuse code from already existing types or objects. • Inheritance is implemented between two classes using extends keyword. • When a class extends another class, extending class is called subclass and extended class is super class.www.JAVA9S.com
  12. 12. Inheritance class Car{ void move(){ •Car is super class System.out.println(“Moves”); •Ford is subclass. } } class Ford extends Car{ Ford f = new Ford(); f.moveFast(); void moveFast(){ f.move(); System.out.println(“Moves Fast”); } }www.JAVA9S.com
  13. 13. Inheritance • With inheritance, all the members of super class are available to the subclass objects. • When a class extends another class, it has an IS-A relation with its super class. • instanceof keyword can be used to confirm IS-A relationship.www.JAVA9S.com
  14. 14. Right or Wrong?? class Car{ } class Ford extends Car{ } class BMW extends Car{ } f instanceOf Car Ford f = new Ford(); f instanceOf BMW BMW b = new BMW(); Car c = new Car(); b instanceOf Ford b instanceOf Car c instanceOf Fordwww.JAVA9S.com
  15. 15. super and this keywords • super is used to access the super class members. • this is used to access the currently executing objects members.www.JAVA9S.com
  16. 16. super - example class Car{ int speed; } class Ford extends Car{ int speed; void move(){ System.out.println(“Moving with car speed:”+super.speed) } }www.JAVA9S.com
  17. 17. this - example class Ford{ int price; int price; setFordPrice(int price){ setFordPrice(int price){ this.price = price; this.price = price; } } } a Ford a = new Ford(); a.setFordPrice(3000); ‘this’ is not mandatory. But if you have local variables declared inside a method, to avoid confusion, this can be used to refer to members of the object.www.JAVA9S.com
  18. 18. HAS-A relationship class Student{ Pen p = new Pen(); } class Pen{ } Student HAS-A pen. HAS – A relationship helps to reduce the complexity of the classes by the composition of the other classes.www.JAVA9S.com
  19. 19. Thank you Follow me on to get more updates on latest video posts Subscribe on http://www.youtube.com/user/java9s Twitter : @java9s facebook: www.facebook.com/java9swww.JAVA9S.com

×