SlideShare a Scribd company logo
Classes, Methods and
                      Inheritance
           SCJP / OCJP objectives : 1.1, 1.2, 1.3,
                         1.4, 5.5




                                                 By,
www.JAVA9S.com                            Srinivas Reddy.S
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
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 files


www.JAVA9S.com
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
Creating Objects

    Car c = new Car();

                        Instantiation
      Declaration


  C is the reference which holds the           Car
                                            Speed=50
  memory address of the Car object
                                        C
www.JAVA9S.com
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) ;-> 60

www.JAVA9S.com
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 - void



www.JAVA9S.com
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
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
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
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
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
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
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 Ford
www.JAVA9S.com
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
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
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
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
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/java9s




www.JAVA9S.com
Ad

Recommended

Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Edureka!
 
Inheritance and its types In Java
Inheritance and its types In Java
MD SALEEM QAISAR
 
Core Java Introduction | Basics
Core Java Introduction | Basics
Hùng Nguyễn Huy
 
javainheritance
javainheritance
Arjun Shanka
 
Object Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
java-06inheritance
java-06inheritance
Arjun Shanka
 
Dacj 2-1 a
Dacj 2-1 a
Niit Care
 
Java inheritance
Java inheritance
BHUVIJAYAVELU
 
Java: Inheritance
Java: Inheritance
Tareq Hasan
 
inheritance
inheritance
Mohit Patodia
 
Inheritance in java
Inheritance in java
Tech_MX
 
Inheritance in java
Inheritance in java
Lovely Professional University
 
Inheritance in Java
Inheritance in Java
Elizabeth alexander
 
Access modifiers
Access modifiers
Kasun Ranga Wijeweera
 
Introduction to Java
Introduction to Java
Professional Guru
 
Inheritance in Java
Inheritance in Java
Tamanna Akter
 
Inheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
Inheritance in java
Inheritance in java
yash jain
 
Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java Inheritance
Java Inheritance
VINOTH R
 
Java non access modifiers
Java non access modifiers
Srinivas Reddy
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Sagar Verma
 
Unit 3 Java
Unit 3 Java
arnold 7490
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 
Java Inheritance
Java Inheritance
Rosie Jane Enomar
 
Classes, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

More Related Content

What's hot (20)

Java: Inheritance
Java: Inheritance
Tareq Hasan
 
inheritance
inheritance
Mohit Patodia
 
Inheritance in java
Inheritance in java
Tech_MX
 
Inheritance in java
Inheritance in java
Lovely Professional University
 
Inheritance in Java
Inheritance in Java
Elizabeth alexander
 
Access modifiers
Access modifiers
Kasun Ranga Wijeweera
 
Introduction to Java
Introduction to Java
Professional Guru
 
Inheritance in Java
Inheritance in Java
Tamanna Akter
 
Inheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
Inheritance in java
Inheritance in java
yash jain
 
Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java Inheritance
Java Inheritance
VINOTH R
 
Java non access modifiers
Java non access modifiers
Srinivas Reddy
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Sagar Verma
 
Unit 3 Java
Unit 3 Java
arnold 7490
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 
Java: Inheritance
Java: Inheritance
Tareq Hasan
 
Inheritance in java
Inheritance in java
Tech_MX
 
Inheritance in java
Inheritance in java
yash jain
 
Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java Inheritance
Java Inheritance
VINOTH R
 
Java non access modifiers
Java non access modifiers
Srinivas Reddy
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Sagar Verma
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 

Viewers also liked (20)

Java Inheritance
Java Inheritance
Rosie Jane Enomar
 
Classes, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
JDK 8, lambdas, streams, collectors - Bretagne Tour
JDK 8, lambdas, streams, collectors - Bretagne Tour
José Paumard
 
No more loops with lambdaj
No more loops with lambdaj
Mario Fusco
 
Mobile based Bus Ticketing System
Mobile based Bus Ticketing System
LogixGRID Technologies Pvt Ltd
 
Spring framework core
Spring framework core
Taemon Piya-Lumyong
 
Java 8 Workshop
Java 8 Workshop
Mario Fusco
 
Autumn collection JavaOne 2014
Autumn collection JavaOne 2014
José Paumard
 
50 new things you can do with java 8
50 new things you can do with java 8
José Paumard
 
Linked to ArrayList: the full story
Linked to ArrayList: the full story
José Paumard
 
API Asynchrones en Java 8
API Asynchrones en Java 8
José Paumard
 
Object and Classes in Java
Object and Classes in Java
backdoor
 
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Classes And Objects
Classes And Objects
rahulsahay19
 
Inheritance in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
Bus Booking Management System
Bus Booking Management System
Mike Marshall
 
Bus Ticket Management System Documentation
Bus Ticket Management System Documentation
muzammil siddiq
 
Online Bus Reservatiom System
Online Bus Reservatiom System
Nikhil Vyas
 
Online Bus Ticket Reservation System
Online Bus Ticket Reservation System
Tuvshinbayar Davaa
 
Classes, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
JDK 8, lambdas, streams, collectors - Bretagne Tour
JDK 8, lambdas, streams, collectors - Bretagne Tour
José Paumard
 
No more loops with lambdaj
No more loops with lambdaj
Mario Fusco
 
Autumn collection JavaOne 2014
Autumn collection JavaOne 2014
José Paumard
 
50 new things you can do with java 8
50 new things you can do with java 8
José Paumard
 
Linked to ArrayList: the full story
Linked to ArrayList: the full story
José Paumard
 
API Asynchrones en Java 8
API Asynchrones en Java 8
José Paumard
 
Object and Classes in Java
Object and Classes in Java
backdoor
 
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Classes And Objects
Classes And Objects
rahulsahay19
 
Inheritance in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
Bus Booking Management System
Bus Booking Management System
Mike Marshall
 
Bus Ticket Management System Documentation
Bus Ticket Management System Documentation
muzammil siddiq
 
Online Bus Reservatiom System
Online Bus Reservatiom System
Nikhil Vyas
 
Online Bus Ticket Reservation System
Online Bus Ticket Reservation System
Tuvshinbayar Davaa
 
Ad

Similar to Java Classes methods and inheritance (20)

inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
OOP with Java - Part 3
OOP with Java - Part 3
RatnaJava
 
Learn java
Learn java
Palahuja
 
OOP with Java - Part 3
OOP with Java - Part 3
Hitesh-Java
 
Inheritance in Java
Inheritance in Java
Ganesh kumar reddy
 
Advance java kvr -satya
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Adv kvr -satya
Jyothsna Sree
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
PawanMM
 
Xopus Application Framework
Xopus Application Framework
Jady Yang
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
3java Advanced Oop
3java Advanced Oop
Adil Jafri
 
Do you really get class loaders?
Do you really get class loaders?
guestd56374
 
Object Oriented Programming Concepts
Object Oriented Programming Concepts
Bhushan Nagaraj
 
Inheritance1
Inheritance1
Daman Toor
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
OOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
Basics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
Basics of Java
Basics of Java
Sherihan Anver
 
Java Interview Questions Answers Guide
Java Interview Questions Answers Guide
DaisyWatson5
 
Framework prototype
Framework prototype
DevMix
 
inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
OOP with Java - Part 3
OOP with Java - Part 3
RatnaJava
 
Learn java
Learn java
Palahuja
 
OOP with Java - Part 3
OOP with Java - Part 3
Hitesh-Java
 
Advance java kvr -satya
Advance java kvr -satya
Satya Johnny
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
PawanMM
 
Xopus Application Framework
Xopus Application Framework
Jady Yang
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
3java Advanced Oop
3java Advanced Oop
Adil Jafri
 
Do you really get class loaders?
Do you really get class loaders?
guestd56374
 
Object Oriented Programming Concepts
Object Oriented Programming Concepts
Bhushan Nagaraj
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
OOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
Basics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
Java Interview Questions Answers Guide
Java Interview Questions Answers Guide
DaisyWatson5
 
Framework prototype
Framework prototype
DevMix
 
Ad

Recently uploaded (20)

Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 

Java Classes methods and inheritance

  • 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. 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. 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 files www.JAVA9S.com
  • 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. Creating Objects Car c = new Car(); Instantiation Declaration C is the reference which holds the Car Speed=50 memory address of the Car object C www.JAVA9S.com
  • 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) ;-> 60 www.JAVA9S.com
  • 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 - void www.JAVA9S.com
  • 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. 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. 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. 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. 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. 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. 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 Ford www.JAVA9S.com
  • 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. 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. 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. 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. 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/java9s www.JAVA9S.com