Java Classes methods and inheritance


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
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
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, 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  If there are multiple classes in a file, the file name should be the name of the class with public access
  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
  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
  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
  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) ;->
  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 -
  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; }
  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.. }
  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)); }
  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
  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”); } }
  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
  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
  15. 15. super and this keywords • super is used to access the super class members. • this is used to access the currently executing objects
  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) } }
  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
  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
  19. 19. Thank you Follow me on to get more updates on latest video posts Subscribe on Twitter : @java9s facebook: