Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming in JAVA
By:Srwa Mohammad
2
The Java Oops Object Oriented Programming (OOP) is a programming concept used in several modern programming languages, like C++, Java and Python. Before Object Oriented Programming programs were viewed as procedures that accepted data and produced an output. What is Class: Java is a purly an Object Oriented Language. Everything in java is an object. A Class does not hold any value unless and until it is used for object creation. Definition: A class is a collection of data, stored in named fields, and code, organized into named methods, that operates on that data.
3
The Java Class with method and parameter
class rectangle { double l,w; void get() { l=5; w=5; } class Callmethod { public static void main(String args[]) rectangle r=new rectangle(); r.get(); r.area(); } void area() { double a; a=l*w; System.out.println("Area is:"+a); }}
4
The Java Constructor Constructor:- A Constructor is a special method that does not have a return type and a has the same name as that of the class name. It means that a constructor can act as a normal method or function but it cannot return any value. Every class must have at least one constructor. If there are no constructors for your class, the compiler will supply a default constructor (no-arg constructor). A constructor is used to construct an object. A constructor looks like a method and is sometimes called a constructor method. A constructor never returns a value
5
The Java Default Constructor Example
class box { int l,w; box() { l=5; w=5; } void show() System.out.println("Length is:"+l); System.out.println("width is:"+w); } } class GradeBook public static void main(String args[]) box b=new box(); b.show(); } }
6
Inheritance enables new objects to take on the properties of existing objects . A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass . Its done by using (extends) keyword
7
inheritance
8
Inheritance in Java class A { int i;int j; void showij(){
System.out.println("i and j:" +i+" "+j); }} class B extends A { int k; void showk(){ System.out.println("K:" +k); } void sum(){ System.out.println("i+j+k:" +(i+j+k)); 2/3/2015 Object Oriented Programming using JAVA 81
9
Inheritance in Java… class SimpleInheritanc{
public static void main(String args[]){ A objectA=new A(); B objectB=new B(); objectA.i=10 ; objectA.j=20; System.out.println("Contents of objectA :”); objectA.showij(); System.out.println(); objectB.i=7; objectB.j=8; objectB.k=9; System.out.println("contents of objectB“); objectB.showij(); objectB.showk(); System.out.println(); System.out.println("Sum of i,j and k in objectB :”); objectB.sum(); }} Object Oriented Programming using JAVA 82
10
Compiler determines which method has to execute automatically.
Method Overloading In java we can define many number of methods in a class with same name. Defining two or more method with same name is known as “Method overloading”. Compiler determines which method has to execute automatically. 2/3/2015 Object Oriented Programming using JAVA 72
11
Method Overloading Example
class Rectangle { void area(int length, int width ){ int a= length * width; System.out.println("Area of Rectangle : " + a) } void area(double length, double width) { double b= length * width; System.out.println("Area ofRectangle : " + b);}} class RectangleDemo { public static void main(String args[]) { Rectangle r1 = new Rectangle(); r1.area(10, 20); r1.area(10.50, 20.50); }} 2/3/2015 Object Oriented Programming using JAVA 73
12
Method Overriding Method overriding: refers to idea of having a subclass contain a method with same name and parameters as a method in super class Static methods can not be overriding
13
Overriding exmple class AA{ public void same(){
System.out.println(“this is class AA”); }} class BB extends AA{ System.out.println(“this is class BB”); public class Aga extends BB{ public static void main(String[]args){ Aga x=new Aga(); x.same()
14
overridden method using super keyword
If you write super.func() to call the method, it will call the method that was defined in the superclass. class AA{ public void same(){ System.out.println(“this is class AA”); }} class BB extends AA{ System.out.println(“this is class BB”); public class Aga extends BB{ super.same(); System.out.println(“this is class Aga”); } public static void main(String[]args){ Aga x=new Aga(); x.same()
15
polymorphism Polymorphism: "Poly" means "many" and "morph" means "form". Polymorphism(many forms) is the ability of an object to become many different forms of object. Example: overloading, overriding, virtual functions We use polymorphism with inheritance and, and the concept of it is “ one interface , multiple methods”.
16
2/3/2015 Object Oriented Programming using JAVA 109
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.